Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006 Rob Landley |
Denis Vlasenko | d18f52b | 2008-03-02 12:53:15 +0000 | [diff] [blame] | 7 | * Copyright (C) 2006 Denys Vlasenko |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 8 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 9 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
Denis Vlasenko | b12b1c8 | 2008-04-09 00:33:23 +0000 | [diff] [blame] | 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. |
| 23 | */ |
| 24 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 25 | #include "libbb.h" |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 26 | |
Denis Vlasenko | b12b1c8 | 2008-04-09 00:33:23 +0000 | [diff] [blame] | 27 | /* Turn on nonblocking I/O on a fd */ |
Denys Vlasenko | d6e7672 | 2014-09-22 21:14:02 +0200 | [diff] [blame] | 28 | int FAST_FUNC ndelay_on(int fd) |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 29 | { |
Denys Vlasenko | e9a40e3 | 2011-01-24 00:29:55 +0100 | [diff] [blame] | 30 | int flags = fcntl(fd, F_GETFL); |
| 31 | if (flags & O_NONBLOCK) |
Denys Vlasenko | d6e7672 | 2014-09-22 21:14:02 +0200 | [diff] [blame] | 32 | return flags; |
Denys Vlasenko | e9a40e3 | 2011-01-24 00:29:55 +0100 | [diff] [blame] | 33 | fcntl(fd, F_SETFL, flags | O_NONBLOCK); |
Denys Vlasenko | d6e7672 | 2014-09-22 21:14:02 +0200 | [diff] [blame] | 34 | return flags; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Denys Vlasenko | d6e7672 | 2014-09-22 21:14:02 +0200 | [diff] [blame] | 37 | int FAST_FUNC ndelay_off(int fd) |
Denis Vlasenko | a5b3e76 | 2006-12-24 07:15:50 +0000 | [diff] [blame] | 38 | { |
Denys Vlasenko | e9a40e3 | 2011-01-24 00:29:55 +0100 | [diff] [blame] | 39 | int flags = fcntl(fd, F_GETFL); |
| 40 | if (!(flags & O_NONBLOCK)) |
Denys Vlasenko | d6e7672 | 2014-09-22 21:14:02 +0200 | [diff] [blame] | 41 | return flags; |
Denys Vlasenko | e9a40e3 | 2011-01-24 00:29:55 +0100 | [diff] [blame] | 42 | fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); |
Denys Vlasenko | d6e7672 | 2014-09-22 21:14:02 +0200 | [diff] [blame] | 43 | return flags; |
Denis Vlasenko | a5b3e76 | 2006-12-24 07:15:50 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Denys Vlasenko | e9a40e3 | 2011-01-24 00:29:55 +0100 | [diff] [blame] | 46 | void FAST_FUNC close_on_exec_on(int fd) |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 47 | { |
Denys Vlasenko | e9a40e3 | 2011-01-24 00:29:55 +0100 | [diff] [blame] | 48 | fcntl(fd, F_SETFD, FD_CLOEXEC); |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 51 | char* FAST_FUNC strncpy_IFNAMSIZ(char *dst, const char *src) |
| 52 | { |
| 53 | #ifndef IFNAMSIZ |
| 54 | enum { IFNAMSIZ = 16 }; |
| 55 | #endif |
| 56 | return strncpy(dst, src, IFNAMSIZ); |
| 57 | } |
| 58 | |
Denis Vlasenko | 56ea65c | 2008-01-06 03:26:53 +0000 | [diff] [blame] | 59 | |
Denys Vlasenko | ab60cd1 | 2010-06-10 10:54:44 +0200 | [diff] [blame] | 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); |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 65 | char* FAST_FUNC utoa_to_buf(unsigned n, char *buf, unsigned buflen) |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 66 | { |
Denis Vlasenko | aae0311 | 2006-11-05 00:44:39 +0000 | [diff] [blame] | 67 | unsigned i, out, res; |
Denys Vlasenko | ab60cd1 | 2010-06-10 10:54:44 +0200 | [diff] [blame] | 68 | |
Rob Landley | 22d3958 | 2006-07-11 00:44:36 +0000 | [diff] [blame] | 69 | if (buflen) { |
Denis Vlasenko | aae0311 | 2006-11-05 00:44:39 +0000 | [diff] [blame] | 70 | out = 0; |
Denys Vlasenko | ab60cd1 | 2010-06-10 10:54:44 +0200 | [diff] [blame] | 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) { |
Denis Vlasenko | aae0311 | 2006-11-05 00:44:39 +0000 | [diff] [blame] | 83 | res = n / i; |
Denys Vlasenko | ab60cd1 | 2010-06-10 10:54:44 +0200 | [diff] [blame] | 84 | n = n % i; |
Denis Vlasenko | aae0311 | 2006-11-05 00:44:39 +0000 | [diff] [blame] | 85 | if (res || out || i == 1) { |
Denys Vlasenko | ab60cd1 | 2010-06-10 10:54:44 +0200 | [diff] [blame] | 86 | if (--buflen == 0) |
| 87 | break; |
Rob Landley | 22d3958 | 2006-07-11 00:44:36 +0000 | [diff] [blame] | 88 | out++; |
Rob Landley | 22d3958 | 2006-07-11 00:44:36 +0000 | [diff] [blame] | 89 | *buf++ = '0' + res; |
| 90 | } |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 93 | return buf; |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Denis Vlasenko | b12b1c8 | 2008-04-09 00:33:23 +0000 | [diff] [blame] | 96 | /* Convert signed integer to ascii, like utoa_to_buf() */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 97 | char* FAST_FUNC itoa_to_buf(int n, char *buf, unsigned buflen) |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 98 | { |
Denys Vlasenko | ab60cd1 | 2010-06-10 10:54:44 +0200 | [diff] [blame] | 99 | if (!buflen) |
| 100 | return buf; |
| 101 | if (n < 0) { |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 102 | n = -n; |
| 103 | *buf++ = '-'; |
Rob Landley | 22d3958 | 2006-07-11 00:44:36 +0000 | [diff] [blame] | 104 | buflen--; |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 105 | } |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 106 | return utoa_to_buf((unsigned)n, buf, buflen); |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Rob Landley | da9d1d0 | 2006-09-14 19:52:07 +0000 | [diff] [blame] | 109 | // The following two functions use a static buffer, so calling either one a |
| 110 | // second time will overwrite previous results. |
| 111 | // |
Denys Vlasenko | ab60cd1 | 2010-06-10 10:54:44 +0200 | [diff] [blame] | 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. |
Denis Vlasenko | b12b1c8 | 2008-04-09 00:33:23 +0000 | [diff] [blame] | 114 | // (sizeof(int) * 3 + 2 is correct for any width, even 8-bit) |
Rob Landley | da9d1d0 | 2006-09-14 19:52:07 +0000 | [diff] [blame] | 115 | |
Denis Vlasenko | b12b1c8 | 2008-04-09 00:33:23 +0000 | [diff] [blame] | 116 | static char local_buf[sizeof(int) * 3]; |
Rob Landley | 23b61be | 2006-08-04 20:20:03 +0000 | [diff] [blame] | 117 | |
Denys Vlasenko | ab60cd1 | 2010-06-10 10:54:44 +0200 | [diff] [blame] | 118 | /* Convert unsigned integer to ascii using a static buffer (returned). */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 119 | char* FAST_FUNC utoa(unsigned n) |
Rob Landley | 23b61be | 2006-08-04 20:20:03 +0000 | [diff] [blame] | 120 | { |
Denys Vlasenko | ab60cd1 | 2010-06-10 10:54:44 +0200 | [diff] [blame] | 121 | *(utoa_to_buf(n, local_buf, sizeof(local_buf) - 1)) = '\0'; |
Rob Landley | 23b61be | 2006-08-04 20:20:03 +0000 | [diff] [blame] | 122 | |
| 123 | return local_buf; |
| 124 | } |
| 125 | |
Denis Vlasenko | b12b1c8 | 2008-04-09 00:33:23 +0000 | [diff] [blame] | 126 | /* Convert signed integer to ascii using a static buffer (returned). */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 127 | char* FAST_FUNC itoa(int n) |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 128 | { |
Denys Vlasenko | ab60cd1 | 2010-06-10 10:54:44 +0200 | [diff] [blame] | 129 | *(itoa_to_buf(n, local_buf, sizeof(local_buf) - 1)) = '\0'; |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 130 | |
| 131 | return local_buf; |
| 132 | } |
Rob Landley | df822f2 | 2006-07-15 23:00:46 +0000 | [diff] [blame] | 133 | |
Denis Vlasenko | b12b1c8 | 2008-04-09 00:33:23 +0000 | [diff] [blame] | 134 | /* Emit a string of hex representation of bytes */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 135 | char* FAST_FUNC bin2hex(char *p, const char *cp, int count) |
Denis Vlasenko | 3a34d0c | 2007-01-12 22:10:34 +0000 | [diff] [blame] | 136 | { |
| 137 | while (count) { |
| 138 | unsigned char c = *cp++; |
| 139 | /* put lowercase hex digits */ |
Denis Vlasenko | 98c0bba | 2007-01-26 23:31:05 +0000 | [diff] [blame] | 140 | *p++ = 0x20 | bb_hexdigits_upcase[c >> 4]; |
| 141 | *p++ = 0x20 | bb_hexdigits_upcase[c & 0xf]; |
Denis Vlasenko | 3a34d0c | 2007-01-12 22:10:34 +0000 | [diff] [blame] | 142 | count--; |
| 143 | } |
| 144 | return p; |
| 145 | } |
| 146 | |
Denys Vlasenko | 4836331 | 2010-04-04 15:29:32 +0200 | [diff] [blame] | 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) |
| 149 | { |
| 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; |
| 170 | |
| 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; |
| 180 | } |
| 181 | |
Denis Vlasenko | b12b1c8 | 2008-04-09 00:33:23 +0000 | [diff] [blame] | 182 | /* Return how long the file at fd is, if there's any way to determine it. */ |
Denis Vlasenko | b5c60fc | 2008-01-27 23:41:34 +0000 | [diff] [blame] | 183 | #ifdef UNUSED |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 184 | off_t FAST_FUNC fdlength(int fd) |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 185 | { |
| 186 | off_t bottom = 0, top = 0, pos; |
| 187 | long size; |
| 188 | |
Rob Landley | da9d1d0 | 2006-09-14 19:52:07 +0000 | [diff] [blame] | 189 | // If the ioctl works for this, return it. |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 190 | |
| 191 | if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512; |
| 192 | |
Denis Vlasenko | 621204b | 2006-10-27 09:03:24 +0000 | [diff] [blame] | 193 | // FIXME: explain why lseek(SEEK_END) is not used here! |
| 194 | |
Rob Landley | da9d1d0 | 2006-09-14 19:52:07 +0000 | [diff] [blame] | 195 | // If not, do a binary search for the last location we can read. (Some |
| 196 | // block devices don't do BLKGETSIZE right.) |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 197 | |
| 198 | do { |
| 199 | char temp; |
| 200 | |
Denis Vlasenko | d25a264 | 2006-09-05 09:36:19 +0000 | [diff] [blame] | 201 | pos = bottom + (top - bottom) / 2; |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 202 | |
Rob Landley | da9d1d0 | 2006-09-14 19:52:07 +0000 | [diff] [blame] | 203 | // If we can read from the current location, it's bigger. |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 204 | |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 205 | if (lseek(fd, pos, SEEK_SET)>=0 && safe_read(fd, &temp, 1)==1) { |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 206 | if (bottom == top) bottom = top = (top+1) * 2; |
| 207 | else bottom = pos; |
| 208 | |
Rob Landley | da9d1d0 | 2006-09-14 19:52:07 +0000 | [diff] [blame] | 209 | // If we can't, it's smaller. |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 210 | |
| 211 | } else { |
| 212 | if (bottom == top) { |
| 213 | if (!top) return 0; |
| 214 | bottom = top/2; |
| 215 | } |
| 216 | else top = pos; |
| 217 | } |
| 218 | } while (bottom + 1 != top); |
| 219 | |
| 220 | return pos + 1; |
| 221 | } |
Denis Vlasenko | b5c60fc | 2008-01-27 23:41:34 +0000 | [diff] [blame] | 222 | #endif |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 223 | |
Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [diff] [blame] | 224 | int FAST_FUNC bb_putchar_stderr(char ch) |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 225 | { |
Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [diff] [blame] | 226 | return write(STDERR_FILENO, &ch, 1); |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Denys Vlasenko | 729ecb8 | 2010-06-07 14:14:26 +0200 | [diff] [blame] | 229 | ssize_t FAST_FUNC full_write1_str(const char *str) |
| 230 | { |
| 231 | return full_write(STDOUT_FILENO, str, strlen(str)); |
| 232 | } |
| 233 | |
| 234 | ssize_t FAST_FUNC full_write2_str(const char *str) |
| 235 | { |
| 236 | return full_write(STDERR_FILENO, str, strlen(str)); |
| 237 | } |
| 238 | |
Denys Vlasenko | c175c46 | 2010-04-18 22:09:30 -0700 | [diff] [blame] | 239 | static int wh_helper(int value, int def_val, const char *env_name, int *err) |
| 240 | { |
| 241 | if (value == 0) { |
| 242 | char *s = getenv(env_name); |
| 243 | if (s) { |
| 244 | value = atoi(s); |
Denys Vlasenko | a04440c | 2010-12-05 05:02:49 +0100 | [diff] [blame] | 245 | /* If LINES/COLUMNS are set, pretend that there is |
Denys Vlasenko | c175c46 | 2010-04-18 22:09:30 -0700 | [diff] [blame] | 246 | * no error getting w/h, this prevents some ugly |
| 247 | * cursor tricks by our callers */ |
| 248 | *err = 0; |
| 249 | } |
| 250 | } |
| 251 | if (value <= 1 || value >= 30000) |
| 252 | value = def_val; |
| 253 | return value; |
| 254 | } |
| 255 | |
Rob Landley | fbdf121 | 2006-09-20 22:06:01 +0000 | [diff] [blame] | 256 | /* It is perfectly ok to pass in a NULL for either width or for |
Denis Vlasenko | 621204b | 2006-10-27 09:03:24 +0000 | [diff] [blame] | 257 | * height, in which case that value will not be set. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 258 | int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *height) |
Rob Landley | fbdf121 | 2006-09-20 22:06:01 +0000 | [diff] [blame] | 259 | { |
Denys Vlasenko | c175c46 | 2010-04-18 22:09:30 -0700 | [diff] [blame] | 260 | struct winsize win; |
| 261 | int err; |
Denis Vlasenko | 621204b | 2006-10-27 09:03:24 +0000 | [diff] [blame] | 262 | |
Denys Vlasenko | c175c46 | 2010-04-18 22:09:30 -0700 | [diff] [blame] | 263 | win.ws_row = 0; |
| 264 | win.ws_col = 0; |
| 265 | /* I've seen ioctl returning 0, but row/col is (still?) 0. |
| 266 | * We treat that as an error too. */ |
| 267 | err = ioctl(fd, TIOCGWINSZ, &win) != 0 || win.ws_row == 0; |
| 268 | if (height) |
| 269 | *height = wh_helper(win.ws_row, 24, "LINES", &err); |
| 270 | if (width) |
| 271 | *width = wh_helper(win.ws_col, 80, "COLUMNS", &err); |
| 272 | return err; |
Rob Landley | fbdf121 | 2006-09-20 22:06:01 +0000 | [diff] [blame] | 273 | } |
Denis Vlasenko | 202ac50 | 2008-11-05 13:20:58 +0000 | [diff] [blame] | 274 | |
| 275 | int FAST_FUNC tcsetattr_stdin_TCSANOW(const struct termios *tp) |
| 276 | { |
| 277 | return tcsetattr(STDIN_FILENO, TCSANOW, tp); |
| 278 | } |
Pascal Bellard | 21e8e8d | 2010-07-04 00:57:03 +0200 | [diff] [blame] | 279 | |
| 280 | pid_t FAST_FUNC safe_waitpid(pid_t pid, int *wstat, int options) |
| 281 | { |
| 282 | pid_t r; |
| 283 | |
| 284 | do |
| 285 | r = waitpid(pid, wstat, options); |
| 286 | while ((r == -1) && (errno == EINTR)); |
| 287 | return r; |
| 288 | } |
| 289 | |
| 290 | pid_t FAST_FUNC wait_any_nohang(int *wstat) |
| 291 | { |
| 292 | return safe_waitpid(-1, wstat, WNOHANG); |
| 293 | } |
| 294 | |
| 295 | // Wait for the specified child PID to exit, returning child's error return. |
| 296 | int FAST_FUNC wait4pid(pid_t pid) |
| 297 | { |
| 298 | int status; |
| 299 | |
| 300 | if (pid <= 0) { |
| 301 | /*errno = ECHILD; -- wrong. */ |
| 302 | /* we expect errno to be already set from failed [v]fork/exec */ |
| 303 | return -1; |
| 304 | } |
| 305 | if (safe_waitpid(pid, &status, 0) == -1) |
| 306 | return -1; |
| 307 | if (WIFEXITED(status)) |
| 308 | return WEXITSTATUS(status); |
| 309 | if (WIFSIGNALED(status)) |
| 310 | return WTERMSIG(status) + 0x180; |
| 311 | return 0; |
| 312 | } |