[821] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 3 | * Utility routines.
|
---|
| 4 | *
|
---|
| 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
---|
[1765] | 6 | * Copyright (C) 2006 Rob Landley
|
---|
[2725] | 7 | * Copyright (C) 2006 Denys Vlasenko
|
---|
[821] | 8 | *
|
---|
[2725] | 9 | * Licensed under GPLv2, see file LICENSE in this source tree.
|
---|
[821] | 10 | */
|
---|
| 11 |
|
---|
[2725] | 12 | /* 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.
|
---|
[1765] | 23 | */
|
---|
| 24 |
|
---|
[2725] | 25 | #include "libbb.h"
|
---|
[821] | 26 |
|
---|
[2725] | 27 | /* Turn on nonblocking I/O on a fd */
|
---|
[3232] | 28 | void FAST_FUNC ndelay_on(int fd)
|
---|
[821] | 29 | {
|
---|
[3232] | 30 | int flags = fcntl(fd, F_GETFL);
|
---|
| 31 | if (flags & O_NONBLOCK)
|
---|
| 32 | return;
|
---|
| 33 | fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
---|
[821] | 34 | }
|
---|
| 35 |
|
---|
[3232] | 36 | void FAST_FUNC ndelay_off(int fd)
|
---|
[821] | 37 | {
|
---|
[3232] | 38 | int flags = fcntl(fd, F_GETFL);
|
---|
| 39 | if (!(flags & O_NONBLOCK))
|
---|
| 40 | return;
|
---|
| 41 | fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
|
---|
[821] | 42 | }
|
---|
| 43 |
|
---|
[3232] | 44 | void FAST_FUNC close_on_exec_on(int fd)
|
---|
[821] | 45 | {
|
---|
[3232] | 46 | fcntl(fd, F_SETFD, FD_CLOEXEC);
|
---|
[821] | 47 | }
|
---|
| 48 |
|
---|
[2725] | 49 | char* FAST_FUNC strncpy_IFNAMSIZ(char *dst, const char *src)
|
---|
[821] | 50 | {
|
---|
[2725] | 51 | #ifndef IFNAMSIZ
|
---|
| 52 | enum { IFNAMSIZ = 16 };
|
---|
[821] | 53 | #endif
|
---|
[2725] | 54 | return strncpy(dst, src, IFNAMSIZ);
|
---|
[1765] | 55 | }
|
---|
[821] | 56 |
|
---|
[1765] | 57 |
|
---|
[2725] | 58 | /* 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 | */
|
---|
| 62 | void BUG_sizeof(void);
|
---|
| 63 | char* FAST_FUNC utoa_to_buf(unsigned n, char *buf, unsigned buflen)
|
---|
[1765] | 64 | {
|
---|
[2725] | 65 | unsigned i, out, res;
|
---|
[1765] | 66 |
|
---|
| 67 | if (buflen) {
|
---|
| 68 | out = 0;
|
---|
[2725] | 69 | 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) {
|
---|
[1765] | 81 | res = n / i;
|
---|
[2725] | 82 | n = n % i;
|
---|
[1765] | 83 | if (res || out || i == 1) {
|
---|
[2725] | 84 | if (--buflen == 0)
|
---|
| 85 | break;
|
---|
[1765] | 86 | out++;
|
---|
| 87 | *buf++ = '0' + res;
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | return buf;
|
---|
| 92 | }
|
---|
[821] | 93 |
|
---|
[2725] | 94 | /* Convert signed integer to ascii, like utoa_to_buf() */
|
---|
| 95 | char* FAST_FUNC itoa_to_buf(int n, char *buf, unsigned buflen)
|
---|
[1765] | 96 | {
|
---|
[2725] | 97 | if (!buflen)
|
---|
| 98 | return buf;
|
---|
| 99 | if (n < 0) {
|
---|
[1765] | 100 | n = -n;
|
---|
| 101 | *buf++ = '-';
|
---|
| 102 | buflen--;
|
---|
| 103 | }
|
---|
| 104 | return utoa_to_buf((unsigned)n, buf, buflen);
|
---|
| 105 | }
|
---|
[821] | 106 |
|
---|
[1765] | 107 | // The following two functions use a static buffer, so calling either one a
|
---|
| 108 | // second time will overwrite previous results.
|
---|
| 109 | //
|
---|
[2725] | 110 | // 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.
|
---|
| 112 | // (sizeof(int) * 3 + 2 is correct for any width, even 8-bit)
|
---|
[1765] | 113 |
|
---|
[2725] | 114 | static char local_buf[sizeof(int) * 3];
|
---|
[1765] | 115 |
|
---|
[2725] | 116 | /* Convert unsigned integer to ascii using a static buffer (returned). */
|
---|
| 117 | char* FAST_FUNC utoa(unsigned n)
|
---|
[1765] | 118 | {
|
---|
[2725] | 119 | *(utoa_to_buf(n, local_buf, sizeof(local_buf) - 1)) = '\0';
|
---|
[1765] | 120 |
|
---|
| 121 | return local_buf;
|
---|
[821] | 122 | }
|
---|
| 123 |
|
---|
[2725] | 124 | /* Convert signed integer to ascii using a static buffer (returned). */
|
---|
| 125 | char* FAST_FUNC itoa(int n)
|
---|
[821] | 126 | {
|
---|
[2725] | 127 | *(itoa_to_buf(n, local_buf, sizeof(local_buf) - 1)) = '\0';
|
---|
[1765] | 128 |
|
---|
| 129 | return local_buf;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[2725] | 132 | /* Emit a string of hex representation of bytes */
|
---|
| 133 | char* FAST_FUNC bin2hex(char *p, const char *cp, int count)
|
---|
[1765] | 134 | {
|
---|
| 135 | while (count) {
|
---|
| 136 | unsigned char c = *cp++;
|
---|
| 137 | /* put lowercase hex digits */
|
---|
| 138 | *p++ = 0x20 | bb_hexdigits_upcase[c >> 4];
|
---|
| 139 | *p++ = 0x20 | bb_hexdigits_upcase[c & 0xf];
|
---|
| 140 | count--;
|
---|
[821] | 141 | }
|
---|
[1765] | 142 | return p;
|
---|
[821] | 143 | }
|
---|
| 144 |
|
---|
[2725] | 145 | /* Convert "[x]x[:][x]x[:][x]x[:][x]x" hex string to binary, no more than COUNT bytes */
|
---|
| 146 | char* FAST_FUNC hex2bin(char *dst, const char *str, int count)
|
---|
[821] | 147 | {
|
---|
[2725] | 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;
|
---|
[1765] | 168 |
|
---|
[2725] | 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;
|
---|
[1765] | 178 | }
|
---|
| 179 |
|
---|
[2725] | 180 | /* Return how long the file at fd is, if there's any way to determine it. */
|
---|
| 181 | #ifdef UNUSED
|
---|
| 182 | off_t FAST_FUNC fdlength(int fd)
|
---|
[1765] | 183 | {
|
---|
| 184 | off_t bottom = 0, top = 0, pos;
|
---|
| 185 | long size;
|
---|
| 186 |
|
---|
| 187 | // If the ioctl works for this, return it.
|
---|
| 188 |
|
---|
| 189 | if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512;
|
---|
| 190 |
|
---|
| 191 | // FIXME: explain why lseek(SEEK_END) is not used here!
|
---|
| 192 |
|
---|
| 193 | // If not, do a binary search for the last location we can read. (Some
|
---|
| 194 | // block devices don't do BLKGETSIZE right.)
|
---|
| 195 |
|
---|
| 196 | do {
|
---|
| 197 | char temp;
|
---|
| 198 |
|
---|
| 199 | pos = bottom + (top - bottom) / 2;
|
---|
| 200 |
|
---|
| 201 | // If we can read from the current location, it's bigger.
|
---|
| 202 |
|
---|
| 203 | if (lseek(fd, pos, SEEK_SET)>=0 && safe_read(fd, &temp, 1)==1) {
|
---|
| 204 | if (bottom == top) bottom = top = (top+1) * 2;
|
---|
| 205 | else bottom = pos;
|
---|
| 206 |
|
---|
| 207 | // If we can't, it's smaller.
|
---|
| 208 |
|
---|
| 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 | }
|
---|
[821] | 220 | #endif
|
---|
| 221 |
|
---|
[2725] | 222 | int FAST_FUNC bb_putchar_stderr(char ch)
|
---|
[821] | 223 | {
|
---|
[2725] | 224 | return write(STDERR_FILENO, &ch, 1);
|
---|
[821] | 225 | }
|
---|
| 226 |
|
---|
[2725] | 227 | ssize_t FAST_FUNC full_write1_str(const char *str)
|
---|
[821] | 228 | {
|
---|
[2725] | 229 | return full_write(STDOUT_FILENO, str, strlen(str));
|
---|
[1765] | 230 | }
|
---|
[821] | 231 |
|
---|
[2725] | 232 | ssize_t FAST_FUNC full_write2_str(const char *str)
|
---|
[1765] | 233 | {
|
---|
[2725] | 234 | return full_write(STDERR_FILENO, str, strlen(str));
|
---|
[1765] | 235 | }
|
---|
[821] | 236 |
|
---|
[2725] | 237 | static int wh_helper(int value, int def_val, const char *env_name, int *err)
|
---|
[1765] | 238 | {
|
---|
[2725] | 239 | if (value == 0) {
|
---|
| 240 | char *s = getenv(env_name);
|
---|
| 241 | if (s) {
|
---|
| 242 | value = atoi(s);
|
---|
[3232] | 243 | /* If LINES/COLUMNS are set, pretend that there is
|
---|
[2725] | 244 | * no error getting w/h, this prevents some ugly
|
---|
| 245 | * cursor tricks by our callers */
|
---|
| 246 | *err = 0;
|
---|
| 247 | }
|
---|
[1765] | 248 | }
|
---|
[2725] | 249 | if (value <= 1 || value >= 30000)
|
---|
| 250 | value = def_val;
|
---|
| 251 | return value;
|
---|
[1765] | 252 | }
|
---|
| 253 |
|
---|
[2725] | 254 | /* It is perfectly ok to pass in a NULL for either width or for
|
---|
| 255 | * height, in which case that value will not be set. */
|
---|
| 256 | int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *height)
|
---|
[821] | 257 | {
|
---|
[2725] | 258 | struct winsize win;
|
---|
| 259 | int err;
|
---|
[1765] | 260 |
|
---|
[2725] | 261 | 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;
|
---|
[1765] | 271 | }
|
---|
| 272 |
|
---|
[2725] | 273 | int FAST_FUNC tcsetattr_stdin_TCSANOW(const struct termios *tp)
|
---|
[1765] | 274 | {
|
---|
[2725] | 275 | return tcsetattr(STDIN_FILENO, TCSANOW, tp);
|
---|
[1765] | 276 | }
|
---|
| 277 |
|
---|
[2725] | 278 | pid_t FAST_FUNC safe_waitpid(pid_t pid, int *wstat, int options)
|
---|
[1765] | 279 | {
|
---|
[2725] | 280 | pid_t r;
|
---|
[1765] | 281 |
|
---|
[2725] | 282 | do
|
---|
| 283 | r = waitpid(pid, wstat, options);
|
---|
| 284 | while ((r == -1) && (errno == EINTR));
|
---|
| 285 | return r;
|
---|
[1765] | 286 | }
|
---|
[821] | 287 |
|
---|
[2725] | 288 | pid_t FAST_FUNC wait_any_nohang(int *wstat)
|
---|
[821] | 289 | {
|
---|
[2725] | 290 | return safe_waitpid(-1, wstat, WNOHANG);
|
---|
[821] | 291 | }
|
---|
| 292 |
|
---|
[2725] | 293 | // Wait for the specified child PID to exit, returning child's error return.
|
---|
| 294 | int FAST_FUNC wait4pid(pid_t pid)
|
---|
[821] | 295 | {
|
---|
[2725] | 296 | int status;
|
---|
[1765] | 297 |
|
---|
[2725] | 298 | if (pid <= 0) {
|
---|
| 299 | /*errno = ECHILD; -- wrong. */
|
---|
| 300 | /* we expect errno to be already set from failed [v]fork/exec */
|
---|
| 301 | return -1;
|
---|
[1765] | 302 | }
|
---|
[2725] | 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;
|
---|
[821] | 310 | }
|
---|