[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 */
|
---|
[3621] | 28 | int FAST_FUNC ndelay_on(int fd)
|
---|
[821] | 29 | {
|
---|
[3232] | 30 | int flags = fcntl(fd, F_GETFL);
|
---|
| 31 | if (flags & O_NONBLOCK)
|
---|
[3621] | 32 | return flags;
|
---|
[3232] | 33 | fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
---|
[3621] | 34 | return flags;
|
---|
[821] | 35 | }
|
---|
| 36 |
|
---|
[3621] | 37 | int FAST_FUNC ndelay_off(int fd)
|
---|
[821] | 38 | {
|
---|
[3232] | 39 | int flags = fcntl(fd, F_GETFL);
|
---|
| 40 | if (!(flags & O_NONBLOCK))
|
---|
[3621] | 41 | return flags;
|
---|
[3232] | 42 | fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
|
---|
[3621] | 43 | return flags;
|
---|
[821] | 44 | }
|
---|
| 45 |
|
---|
[3232] | 46 | void FAST_FUNC close_on_exec_on(int fd)
|
---|
[821] | 47 | {
|
---|
[3232] | 48 | fcntl(fd, F_SETFD, FD_CLOEXEC);
|
---|
[821] | 49 | }
|
---|
| 50 |
|
---|
[2725] | 51 | char* FAST_FUNC strncpy_IFNAMSIZ(char *dst, const char *src)
|
---|
[821] | 52 | {
|
---|
[2725] | 53 | #ifndef IFNAMSIZ
|
---|
| 54 | enum { IFNAMSIZ = 16 };
|
---|
[821] | 55 | #endif
|
---|
[2725] | 56 | return strncpy(dst, src, IFNAMSIZ);
|
---|
[1765] | 57 | }
|
---|
[821] | 58 |
|
---|
[1765] | 59 |
|
---|
[2725] | 60 | /* Convert unsigned integer to ascii, writing into supplied buffer.
|
---|
| 61 | * A truncated result contains the first few digits of the result ala strncpy.
|
---|
| 62 | * Returns a pointer past last generated digit, does _not_ store NUL.
|
---|
| 63 | */
|
---|
| 64 | void BUG_sizeof(void);
|
---|
| 65 | char* FAST_FUNC utoa_to_buf(unsigned n, char *buf, unsigned buflen)
|
---|
[1765] | 66 | {
|
---|
[2725] | 67 | unsigned i, out, res;
|
---|
[1765] | 68 |
|
---|
| 69 | if (buflen) {
|
---|
| 70 | out = 0;
|
---|
[2725] | 71 | if (sizeof(n) == 4)
|
---|
| 72 | // 2^32-1 = 4294967295
|
---|
| 73 | i = 1000000000;
|
---|
| 74 | #if UINT_MAX > 4294967295 /* prevents warning about "const too large" */
|
---|
| 75 | else
|
---|
| 76 | if (sizeof(n) == 8)
|
---|
| 77 | // 2^64-1 = 18446744073709551615
|
---|
| 78 | i = 10000000000000000000;
|
---|
| 79 | #endif
|
---|
| 80 | else
|
---|
| 81 | BUG_sizeof();
|
---|
| 82 | for (; i; i /= 10) {
|
---|
[1765] | 83 | res = n / i;
|
---|
[2725] | 84 | n = n % i;
|
---|
[1765] | 85 | if (res || out || i == 1) {
|
---|
[2725] | 86 | if (--buflen == 0)
|
---|
| 87 | break;
|
---|
[1765] | 88 | out++;
|
---|
| 89 | *buf++ = '0' + res;
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 | return buf;
|
---|
| 94 | }
|
---|
[821] | 95 |
|
---|
[2725] | 96 | /* Convert signed integer to ascii, like utoa_to_buf() */
|
---|
| 97 | char* FAST_FUNC itoa_to_buf(int n, char *buf, unsigned buflen)
|
---|
[1765] | 98 | {
|
---|
[2725] | 99 | if (!buflen)
|
---|
| 100 | return buf;
|
---|
| 101 | if (n < 0) {
|
---|
[1765] | 102 | n = -n;
|
---|
| 103 | *buf++ = '-';
|
---|
| 104 | buflen--;
|
---|
| 105 | }
|
---|
| 106 | return utoa_to_buf((unsigned)n, buf, buflen);
|
---|
| 107 | }
|
---|
[821] | 108 |
|
---|
[1765] | 109 | // The following two functions use a static buffer, so calling either one a
|
---|
| 110 | // second time will overwrite previous results.
|
---|
| 111 | //
|
---|
[2725] | 112 | // The largest 32 bit integer is -2 billion plus NUL, or 1+10+1=12 bytes.
|
---|
| 113 | // It so happens that sizeof(int) * 3 is enough for 32+ bit ints.
|
---|
| 114 | // (sizeof(int) * 3 + 2 is correct for any width, even 8-bit)
|
---|
[1765] | 115 |
|
---|
[2725] | 116 | static char local_buf[sizeof(int) * 3];
|
---|
[1765] | 117 |
|
---|
[2725] | 118 | /* Convert unsigned integer to ascii using a static buffer (returned). */
|
---|
| 119 | char* FAST_FUNC utoa(unsigned n)
|
---|
[1765] | 120 | {
|
---|
[2725] | 121 | *(utoa_to_buf(n, local_buf, sizeof(local_buf) - 1)) = '\0';
|
---|
[1765] | 122 |
|
---|
| 123 | return local_buf;
|
---|
[821] | 124 | }
|
---|
| 125 |
|
---|
[2725] | 126 | /* Convert signed integer to ascii using a static buffer (returned). */
|
---|
| 127 | char* FAST_FUNC itoa(int n)
|
---|
[821] | 128 | {
|
---|
[2725] | 129 | *(itoa_to_buf(n, local_buf, sizeof(local_buf) - 1)) = '\0';
|
---|
[1765] | 130 |
|
---|
| 131 | return local_buf;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[2725] | 134 | /* Emit a string of hex representation of bytes */
|
---|
| 135 | char* FAST_FUNC bin2hex(char *p, const char *cp, int count)
|
---|
[1765] | 136 | {
|
---|
| 137 | while (count) {
|
---|
| 138 | unsigned char c = *cp++;
|
---|
| 139 | /* put lowercase hex digits */
|
---|
| 140 | *p++ = 0x20 | bb_hexdigits_upcase[c >> 4];
|
---|
| 141 | *p++ = 0x20 | bb_hexdigits_upcase[c & 0xf];
|
---|
| 142 | count--;
|
---|
[821] | 143 | }
|
---|
[1765] | 144 | return p;
|
---|
[821] | 145 | }
|
---|
| 146 |
|
---|
[2725] | 147 | /* Convert "[x]x[:][x]x[:][x]x[:][x]x" hex string to binary, no more than COUNT bytes */
|
---|
| 148 | char* FAST_FUNC hex2bin(char *dst, const char *str, int count)
|
---|
[821] | 149 | {
|
---|
[2725] | 150 | errno = EINVAL;
|
---|
| 151 | while (*str && count) {
|
---|
| 152 | uint8_t val;
|
---|
| 153 | uint8_t c = *str++;
|
---|
| 154 | if (isdigit(c))
|
---|
| 155 | val = c - '0';
|
---|
| 156 | else if ((c|0x20) >= 'a' && (c|0x20) <= 'f')
|
---|
| 157 | val = (c|0x20) - ('a' - 10);
|
---|
| 158 | else
|
---|
| 159 | return NULL;
|
---|
| 160 | val <<= 4;
|
---|
| 161 | c = *str;
|
---|
| 162 | if (isdigit(c))
|
---|
| 163 | val |= c - '0';
|
---|
| 164 | else if ((c|0x20) >= 'a' && (c|0x20) <= 'f')
|
---|
| 165 | val |= (c|0x20) - ('a' - 10);
|
---|
| 166 | else if (c == ':' || c == '\0')
|
---|
| 167 | val >>= 4;
|
---|
| 168 | else
|
---|
| 169 | return NULL;
|
---|
[1765] | 170 |
|
---|
[2725] | 171 | *dst++ = val;
|
---|
| 172 | if (c != '\0')
|
---|
| 173 | str++;
|
---|
| 174 | if (*str == ':')
|
---|
| 175 | str++;
|
---|
| 176 | count--;
|
---|
| 177 | }
|
---|
| 178 | errno = (*str ? ERANGE : 0);
|
---|
| 179 | return dst;
|
---|
[1765] | 180 | }
|
---|
| 181 |
|
---|
[2725] | 182 | /* Return how long the file at fd is, if there's any way to determine it. */
|
---|
| 183 | #ifdef UNUSED
|
---|
| 184 | off_t FAST_FUNC fdlength(int fd)
|
---|
[1765] | 185 | {
|
---|
| 186 | off_t bottom = 0, top = 0, pos;
|
---|
| 187 | long size;
|
---|
| 188 |
|
---|
| 189 | // If the ioctl works for this, return it.
|
---|
| 190 |
|
---|
| 191 | if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512;
|
---|
| 192 |
|
---|
| 193 | // FIXME: explain why lseek(SEEK_END) is not used here!
|
---|
| 194 |
|
---|
| 195 | // If not, do a binary search for the last location we can read. (Some
|
---|
| 196 | // block devices don't do BLKGETSIZE right.)
|
---|
| 197 |
|
---|
| 198 | do {
|
---|
| 199 | char temp;
|
---|
| 200 |
|
---|
| 201 | pos = bottom + (top - bottom) / 2;
|
---|
| 202 |
|
---|
| 203 | // If we can read from the current location, it's bigger.
|
---|
| 204 |
|
---|
| 205 | if (lseek(fd, pos, SEEK_SET)>=0 && safe_read(fd, &temp, 1)==1) {
|
---|
| 206 | if (bottom == top) bottom = top = (top+1) * 2;
|
---|
| 207 | else bottom = pos;
|
---|
| 208 |
|
---|
| 209 | // If we can't, it's smaller.
|
---|
| 210 | } else {
|
---|
| 211 | if (bottom == top) {
|
---|
| 212 | if (!top) return 0;
|
---|
| 213 | bottom = top/2;
|
---|
| 214 | }
|
---|
| 215 | else top = pos;
|
---|
| 216 | }
|
---|
| 217 | } while (bottom + 1 != top);
|
---|
| 218 |
|
---|
| 219 | return pos + 1;
|
---|
| 220 | }
|
---|
[821] | 221 | #endif
|
---|
| 222 |
|
---|
[2725] | 223 | int FAST_FUNC bb_putchar_stderr(char ch)
|
---|
[821] | 224 | {
|
---|
[2725] | 225 | return write(STDERR_FILENO, &ch, 1);
|
---|
[821] | 226 | }
|
---|
| 227 |
|
---|
[2725] | 228 | ssize_t FAST_FUNC full_write1_str(const char *str)
|
---|
[821] | 229 | {
|
---|
[2725] | 230 | return full_write(STDOUT_FILENO, str, strlen(str));
|
---|
[1765] | 231 | }
|
---|
[821] | 232 |
|
---|
[2725] | 233 | ssize_t FAST_FUNC full_write2_str(const char *str)
|
---|
[1765] | 234 | {
|
---|
[2725] | 235 | return full_write(STDERR_FILENO, str, strlen(str));
|
---|
[1765] | 236 | }
|
---|
[821] | 237 |
|
---|
[2725] | 238 | static int wh_helper(int value, int def_val, const char *env_name, int *err)
|
---|
[1765] | 239 | {
|
---|
[2725] | 240 | if (value == 0) {
|
---|
| 241 | char *s = getenv(env_name);
|
---|
| 242 | if (s) {
|
---|
| 243 | value = atoi(s);
|
---|
[3232] | 244 | /* If LINES/COLUMNS are set, pretend that there is
|
---|
[2725] | 245 | * no error getting w/h, this prevents some ugly
|
---|
| 246 | * cursor tricks by our callers */
|
---|
| 247 | *err = 0;
|
---|
| 248 | }
|
---|
[1765] | 249 | }
|
---|
[2725] | 250 | if (value <= 1 || value >= 30000)
|
---|
| 251 | value = def_val;
|
---|
| 252 | return value;
|
---|
[1765] | 253 | }
|
---|
| 254 |
|
---|
[2725] | 255 | /* It is perfectly ok to pass in a NULL for either width or for
|
---|
| 256 | * height, in which case that value will not be set. */
|
---|
| 257 | int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *height)
|
---|
[821] | 258 | {
|
---|
[2725] | 259 | struct winsize win;
|
---|
| 260 | int err;
|
---|
[1765] | 261 |
|
---|
[2725] | 262 | win.ws_row = 0;
|
---|
| 263 | win.ws_col = 0;
|
---|
| 264 | /* I've seen ioctl returning 0, but row/col is (still?) 0.
|
---|
| 265 | * We treat that as an error too. */
|
---|
| 266 | err = ioctl(fd, TIOCGWINSZ, &win) != 0 || win.ws_row == 0;
|
---|
| 267 | if (height)
|
---|
| 268 | *height = wh_helper(win.ws_row, 24, "LINES", &err);
|
---|
| 269 | if (width)
|
---|
| 270 | *width = wh_helper(win.ws_col, 80, "COLUMNS", &err);
|
---|
| 271 | return err;
|
---|
[1765] | 272 | }
|
---|
[3621] | 273 | int FAST_FUNC get_terminal_width(int fd)
|
---|
| 274 | {
|
---|
| 275 | unsigned width;
|
---|
| 276 | get_terminal_width_height(fd, &width, NULL);
|
---|
| 277 | return width;
|
---|
| 278 | }
|
---|
[1765] | 279 |
|
---|
[2725] | 280 | int FAST_FUNC tcsetattr_stdin_TCSANOW(const struct termios *tp)
|
---|
[1765] | 281 | {
|
---|
[2725] | 282 | return tcsetattr(STDIN_FILENO, TCSANOW, tp);
|
---|
[1765] | 283 | }
|
---|
| 284 |
|
---|
[2725] | 285 | pid_t FAST_FUNC safe_waitpid(pid_t pid, int *wstat, int options)
|
---|
[1765] | 286 | {
|
---|
[2725] | 287 | pid_t r;
|
---|
[1765] | 288 |
|
---|
[2725] | 289 | do
|
---|
| 290 | r = waitpid(pid, wstat, options);
|
---|
| 291 | while ((r == -1) && (errno == EINTR));
|
---|
| 292 | return r;
|
---|
[1765] | 293 | }
|
---|
[821] | 294 |
|
---|
[2725] | 295 | pid_t FAST_FUNC wait_any_nohang(int *wstat)
|
---|
[821] | 296 | {
|
---|
[2725] | 297 | return safe_waitpid(-1, wstat, WNOHANG);
|
---|
[821] | 298 | }
|
---|
| 299 |
|
---|
[2725] | 300 | // Wait for the specified child PID to exit, returning child's error return.
|
---|
| 301 | int FAST_FUNC wait4pid(pid_t pid)
|
---|
[821] | 302 | {
|
---|
[2725] | 303 | int status;
|
---|
[1765] | 304 |
|
---|
[2725] | 305 | if (pid <= 0) {
|
---|
| 306 | /*errno = ECHILD; -- wrong. */
|
---|
| 307 | /* we expect errno to be already set from failed [v]fork/exec */
|
---|
| 308 | return -1;
|
---|
[1765] | 309 | }
|
---|
[2725] | 310 | if (safe_waitpid(pid, &status, 0) == -1)
|
---|
| 311 | return -1;
|
---|
| 312 | if (WIFEXITED(status))
|
---|
| 313 | return WEXITSTATUS(status);
|
---|
| 314 | if (WIFSIGNALED(status))
|
---|
| 315 | return WTERMSIG(status) + 0x180;
|
---|
| 316 | return 0;
|
---|
[821] | 317 | }
|
---|
[3621] | 318 |
|
---|
| 319 | // Useful when we do know that pid is valid, and we just want to wait
|
---|
| 320 | // for it to exit. Not existing pid is fatal. waitpid() status is not returned.
|
---|
| 321 | int FAST_FUNC wait_for_exitstatus(pid_t pid)
|
---|
| 322 | {
|
---|
| 323 | int exit_status, n;
|
---|
| 324 |
|
---|
| 325 | n = safe_waitpid(pid, &exit_status, 0);
|
---|
| 326 | if (n < 0)
|
---|
| 327 | bb_perror_msg_and_die("waitpid");
|
---|
| 328 | return exit_status;
|
---|
| 329 | }
|
---|