blob: 23f27516f633c2178f2b00b24e13aec5c9c2d357 [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Rob Landleyd921b2e2006-08-03 15:41:12 +00006 * Copyright (C) 2006 Rob Landley
Denis Vlasenkod18f52b2008-03-02 12:53:15 +00007 * Copyright (C) 2006 Denys Vlasenko
Eric Andersenaad1a882001-03-16 22:47:14 +00008 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2, see file LICENSE in this source tree.
Eric Andersenaad1a882001-03-16 22:47:14 +000010 */
11
Denis Vlasenkob12b1c82008-04-09 00:33:23 +000012/* We need to have separate xfuncs.c and xfuncs_printf.c because
13 * with current linkers, even with section garbage collection,
14 * if *.o module references any of XXXprintf functions, you pull in
15 * entire printf machinery. Even if you do not use the function
16 * which uses XXXprintf.
17 *
18 * xfuncs.c contains functions (not necessarily xfuncs)
19 * which do not pull in printf, directly or indirectly.
20 * xfunc_printf.c contains those which do.
21 *
22 * TODO: move xmalloc() and xatonum() here.
23 */
24
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000025#include "libbb.h"
Eric Andersenaad1a882001-03-16 22:47:14 +000026
Denis Vlasenkob12b1c82008-04-09 00:33:23 +000027/* Turn on nonblocking I/O on a fd */
Denys Vlasenkoe9a40e32011-01-24 00:29:55 +010028void FAST_FUNC ndelay_on(int fd)
Denis Vlasenko75f8d082006-11-22 15:54:52 +000029{
Denys Vlasenkoe9a40e32011-01-24 00:29:55 +010030 int flags = fcntl(fd, F_GETFL);
31 if (flags & O_NONBLOCK)
32 return;
33 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
Denis Vlasenko75f8d082006-11-22 15:54:52 +000034}
35
Denys Vlasenkoe9a40e32011-01-24 00:29:55 +010036void FAST_FUNC ndelay_off(int fd)
Denis Vlasenkoa5b3e762006-12-24 07:15:50 +000037{
Denys Vlasenkoe9a40e32011-01-24 00:29:55 +010038 int flags = fcntl(fd, F_GETFL);
39 if (!(flags & O_NONBLOCK))
40 return;
41 fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
Denis Vlasenkoa5b3e762006-12-24 07:15:50 +000042}
43
Denys Vlasenkoe9a40e32011-01-24 00:29:55 +010044void FAST_FUNC close_on_exec_on(int fd)
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +000045{
Denys Vlasenkoe9a40e32011-01-24 00:29:55 +010046 fcntl(fd, F_SETFD, FD_CLOEXEC);
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +000047}
48
Denis Vlasenko360d9662008-12-02 18:18:50 +000049char* FAST_FUNC strncpy_IFNAMSIZ(char *dst, const char *src)
50{
51#ifndef IFNAMSIZ
52 enum { IFNAMSIZ = 16 };
53#endif
54 return strncpy(dst, src, IFNAMSIZ);
55}
56
Denis Vlasenko56ea65c2008-01-06 03:26:53 +000057
Denys Vlasenkoab60cd12010-06-10 10:54:44 +020058/* Convert unsigned integer to ascii, writing into supplied buffer.
59 * A truncated result contains the first few digits of the result ala strncpy.
60 * Returns a pointer past last generated digit, does _not_ store NUL.
61 */
62void BUG_sizeof(void);
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000063char* FAST_FUNC utoa_to_buf(unsigned n, char *buf, unsigned buflen)
Rob Landley5b88a382006-07-10 07:41:34 +000064{
Denis Vlasenkoaae03112006-11-05 00:44:39 +000065 unsigned i, out, res;
Denys Vlasenkoab60cd12010-06-10 10:54:44 +020066
Rob Landley22d39582006-07-11 00:44:36 +000067 if (buflen) {
Denis Vlasenkoaae03112006-11-05 00:44:39 +000068 out = 0;
Denys Vlasenkoab60cd12010-06-10 10:54:44 +020069 if (sizeof(n) == 4)
70 // 2^32-1 = 4294967295
71 i = 1000000000;
72#if UINT_MAX > 4294967295 /* prevents warning about "const too large" */
73 else
74 if (sizeof(n) == 8)
75 // 2^64-1 = 18446744073709551615
76 i = 10000000000000000000;
77#endif
78 else
79 BUG_sizeof();
80 for (; i; i /= 10) {
Denis Vlasenkoaae03112006-11-05 00:44:39 +000081 res = n / i;
Denys Vlasenkoab60cd12010-06-10 10:54:44 +020082 n = n % i;
Denis Vlasenkoaae03112006-11-05 00:44:39 +000083 if (res || out || i == 1) {
Denys Vlasenkoab60cd12010-06-10 10:54:44 +020084 if (--buflen == 0)
85 break;
Rob Landley22d39582006-07-11 00:44:36 +000086 out++;
Rob Landley22d39582006-07-11 00:44:36 +000087 *buf++ = '0' + res;
88 }
Rob Landley5b88a382006-07-10 07:41:34 +000089 }
90 }
Denis Vlasenko10457b92007-03-27 22:01:31 +000091 return buf;
Rob Landley5b88a382006-07-10 07:41:34 +000092}
93
Denis Vlasenkob12b1c82008-04-09 00:33:23 +000094/* Convert signed integer to ascii, like utoa_to_buf() */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000095char* FAST_FUNC itoa_to_buf(int n, char *buf, unsigned buflen)
Rob Landley5b88a382006-07-10 07:41:34 +000096{
Denys Vlasenkoab60cd12010-06-10 10:54:44 +020097 if (!buflen)
98 return buf;
99 if (n < 0) {
Rob Landley5b88a382006-07-10 07:41:34 +0000100 n = -n;
101 *buf++ = '-';
Rob Landley22d39582006-07-11 00:44:36 +0000102 buflen--;
Rob Landley5b88a382006-07-10 07:41:34 +0000103 }
Denis Vlasenko10457b92007-03-27 22:01:31 +0000104 return utoa_to_buf((unsigned)n, buf, buflen);
Rob Landley5b88a382006-07-10 07:41:34 +0000105}
106
Rob Landleyda9d1d02006-09-14 19:52:07 +0000107// The following two functions use a static buffer, so calling either one a
108// second time will overwrite previous results.
109//
Denys Vlasenkoab60cd12010-06-10 10:54:44 +0200110// The largest 32 bit integer is -2 billion plus NUL, or 1+10+1=12 bytes.
111// It so happens that sizeof(int) * 3 is enough for 32+ bit ints.
Denis Vlasenkob12b1c82008-04-09 00:33:23 +0000112// (sizeof(int) * 3 + 2 is correct for any width, even 8-bit)
Rob Landleyda9d1d02006-09-14 19:52:07 +0000113
Denis Vlasenkob12b1c82008-04-09 00:33:23 +0000114static char local_buf[sizeof(int) * 3];
Rob Landley23b61be2006-08-04 20:20:03 +0000115
Denys Vlasenkoab60cd12010-06-10 10:54:44 +0200116/* Convert unsigned integer to ascii using a static buffer (returned). */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000117char* FAST_FUNC utoa(unsigned n)
Rob Landley23b61be2006-08-04 20:20:03 +0000118{
Denys Vlasenkoab60cd12010-06-10 10:54:44 +0200119 *(utoa_to_buf(n, local_buf, sizeof(local_buf) - 1)) = '\0';
Rob Landley23b61be2006-08-04 20:20:03 +0000120
121 return local_buf;
122}
123
Denis Vlasenkob12b1c82008-04-09 00:33:23 +0000124/* Convert signed integer to ascii using a static buffer (returned). */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000125char* FAST_FUNC itoa(int n)
Rob Landley5b88a382006-07-10 07:41:34 +0000126{
Denys Vlasenkoab60cd12010-06-10 10:54:44 +0200127 *(itoa_to_buf(n, local_buf, sizeof(local_buf) - 1)) = '\0';
Rob Landley5b88a382006-07-10 07:41:34 +0000128
129 return local_buf;
130}
Rob Landleydf822f22006-07-15 23:00:46 +0000131
Denis Vlasenkob12b1c82008-04-09 00:33:23 +0000132/* Emit a string of hex representation of bytes */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000133char* FAST_FUNC bin2hex(char *p, const char *cp, int count)
Denis Vlasenko3a34d0c2007-01-12 22:10:34 +0000134{
135 while (count) {
136 unsigned char c = *cp++;
137 /* put lowercase hex digits */
Denis Vlasenko98c0bba2007-01-26 23:31:05 +0000138 *p++ = 0x20 | bb_hexdigits_upcase[c >> 4];
139 *p++ = 0x20 | bb_hexdigits_upcase[c & 0xf];
Denis Vlasenko3a34d0c2007-01-12 22:10:34 +0000140 count--;
141 }
142 return p;
143}
144
Denys Vlasenko48363312010-04-04 15:29:32 +0200145/* Convert "[x]x[:][x]x[:][x]x[:][x]x" hex string to binary, no more than COUNT bytes */
146char* FAST_FUNC hex2bin(char *dst, const char *str, int count)
147{
148 errno = EINVAL;
149 while (*str && count) {
150 uint8_t val;
151 uint8_t c = *str++;
152 if (isdigit(c))
153 val = c - '0';
154 else if ((c|0x20) >= 'a' && (c|0x20) <= 'f')
155 val = (c|0x20) - ('a' - 10);
156 else
157 return NULL;
158 val <<= 4;
159 c = *str;
160 if (isdigit(c))
161 val |= c - '0';
162 else if ((c|0x20) >= 'a' && (c|0x20) <= 'f')
163 val |= (c|0x20) - ('a' - 10);
164 else if (c == ':' || c == '\0')
165 val >>= 4;
166 else
167 return NULL;
168
169 *dst++ = val;
170 if (c != '\0')
171 str++;
172 if (*str == ':')
173 str++;
174 count--;
175 }
176 errno = (*str ? ERANGE : 0);
177 return dst;
178}
179
Denis Vlasenkob12b1c82008-04-09 00:33:23 +0000180/* Return how long the file at fd is, if there's any way to determine it. */
Denis Vlasenkob5c60fc2008-01-27 23:41:34 +0000181#ifdef UNUSED
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000182off_t FAST_FUNC fdlength(int fd)
Rob Landley53437472006-07-16 08:14:35 +0000183{
184 off_t bottom = 0, top = 0, pos;
185 long size;
186
Rob Landleyda9d1d02006-09-14 19:52:07 +0000187 // If the ioctl works for this, return it.
Rob Landley53437472006-07-16 08:14:35 +0000188
189 if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512;
190
Denis Vlasenko621204b2006-10-27 09:03:24 +0000191 // FIXME: explain why lseek(SEEK_END) is not used here!
192
Rob Landleyda9d1d02006-09-14 19:52:07 +0000193 // If not, do a binary search for the last location we can read. (Some
194 // block devices don't do BLKGETSIZE right.)
Rob Landley53437472006-07-16 08:14:35 +0000195
196 do {
197 char temp;
198
Denis Vlasenkod25a2642006-09-05 09:36:19 +0000199 pos = bottom + (top - bottom) / 2;
Rob Landley53437472006-07-16 08:14:35 +0000200
Rob Landleyda9d1d02006-09-14 19:52:07 +0000201 // If we can read from the current location, it's bigger.
Rob Landley53437472006-07-16 08:14:35 +0000202
Denis Vlasenkoea620772006-10-14 02:23:43 +0000203 if (lseek(fd, pos, SEEK_SET)>=0 && safe_read(fd, &temp, 1)==1) {
Rob Landley53437472006-07-16 08:14:35 +0000204 if (bottom == top) bottom = top = (top+1) * 2;
205 else bottom = pos;
206
Rob Landleyda9d1d02006-09-14 19:52:07 +0000207 // If we can't, it's smaller.
Rob Landley53437472006-07-16 08:14:35 +0000208
209 } else {
210 if (bottom == top) {
211 if (!top) return 0;
212 bottom = top/2;
213 }
214 else top = pos;
215 }
216 } while (bottom + 1 != top);
217
218 return pos + 1;
219}
Denis Vlasenkob5c60fc2008-01-27 23:41:34 +0000220#endif
Rob Landleyd921b2e2006-08-03 15:41:12 +0000221
Denys Vlasenko19ced5c2010-06-06 21:53:09 +0200222int FAST_FUNC bb_putchar_stderr(char ch)
Denis Vlasenko4e12b1a2008-12-23 23:36:47 +0000223{
Denys Vlasenko19ced5c2010-06-06 21:53:09 +0200224 return write(STDERR_FILENO, &ch, 1);
Denis Vlasenko4e12b1a2008-12-23 23:36:47 +0000225}
226
Denys Vlasenko729ecb82010-06-07 14:14:26 +0200227ssize_t FAST_FUNC full_write1_str(const char *str)
228{
229 return full_write(STDOUT_FILENO, str, strlen(str));
230}
231
232ssize_t FAST_FUNC full_write2_str(const char *str)
233{
234 return full_write(STDERR_FILENO, str, strlen(str));
235}
236
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700237static int wh_helper(int value, int def_val, const char *env_name, int *err)
238{
239 if (value == 0) {
240 char *s = getenv(env_name);
241 if (s) {
242 value = atoi(s);
Denys Vlasenkoa04440c2010-12-05 05:02:49 +0100243 /* If LINES/COLUMNS are set, pretend that there is
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700244 * no error getting w/h, this prevents some ugly
245 * cursor tricks by our callers */
246 *err = 0;
247 }
248 }
249 if (value <= 1 || value >= 30000)
250 value = def_val;
251 return value;
252}
253
Rob Landleyfbdf1212006-09-20 22:06:01 +0000254/* It is perfectly ok to pass in a NULL for either width or for
Denis Vlasenko621204b2006-10-27 09:03:24 +0000255 * height, in which case that value will not be set. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000256int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *height)
Rob Landleyfbdf1212006-09-20 22:06:01 +0000257{
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700258 struct winsize win;
259 int err;
Denis Vlasenko621204b2006-10-27 09:03:24 +0000260
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700261 win.ws_row = 0;
262 win.ws_col = 0;
263 /* I've seen ioctl returning 0, but row/col is (still?) 0.
264 * We treat that as an error too. */
265 err = ioctl(fd, TIOCGWINSZ, &win) != 0 || win.ws_row == 0;
266 if (height)
267 *height = wh_helper(win.ws_row, 24, "LINES", &err);
268 if (width)
269 *width = wh_helper(win.ws_col, 80, "COLUMNS", &err);
270 return err;
Rob Landleyfbdf1212006-09-20 22:06:01 +0000271}
Denis Vlasenko202ac502008-11-05 13:20:58 +0000272
273int FAST_FUNC tcsetattr_stdin_TCSANOW(const struct termios *tp)
274{
275 return tcsetattr(STDIN_FILENO, TCSANOW, tp);
276}
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200277
278pid_t FAST_FUNC safe_waitpid(pid_t pid, int *wstat, int options)
279{
280 pid_t r;
281
282 do
283 r = waitpid(pid, wstat, options);
284 while ((r == -1) && (errno == EINTR));
285 return r;
286}
287
288pid_t FAST_FUNC wait_any_nohang(int *wstat)
289{
290 return safe_waitpid(-1, wstat, WNOHANG);
291}
292
293// Wait for the specified child PID to exit, returning child's error return.
294int FAST_FUNC wait4pid(pid_t pid)
295{
296 int status;
297
298 if (pid <= 0) {
299 /*errno = ECHILD; -- wrong. */
300 /* we expect errno to be already set from failed [v]fork/exec */
301 return -1;
302 }
303 if (safe_waitpid(pid, &status, 0) == -1)
304 return -1;
305 if (WIFEXITED(status))
306 return WEXITSTATUS(status);
307 if (WIFSIGNALED(status))
308 return WTERMSIG(status) + 0x180;
309 return 0;
310}