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 | */ |
Denis Vlasenko | b12b1c8 | 2008-04-09 00:33:23 +0000 | [diff] [blame] | 11 | /* We need to have separate xfuncs.c and xfuncs_printf.c because |
| 12 | * with current linkers, even with section garbage collection, |
| 13 | * if *.o module references any of XXXprintf functions, you pull in |
| 14 | * entire printf machinery. Even if you do not use the function |
| 15 | * which uses XXXprintf. |
| 16 | * |
| 17 | * xfuncs.c contains functions (not necessarily xfuncs) |
| 18 | * which do not pull in printf, directly or indirectly. |
| 19 | * xfunc_printf.c contains those which do. |
| 20 | * |
| 21 | * TODO: move xmalloc() and xatonum() here. |
| 22 | */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 23 | #include "libbb.h" |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 24 | |
Denis Vlasenko | b12b1c8 | 2008-04-09 00:33:23 +0000 | [diff] [blame] | 25 | /* Turn on nonblocking I/O on a fd */ |
Denys Vlasenko | d6e7672 | 2014-09-22 21:14:02 +0200 | [diff] [blame] | 26 | int FAST_FUNC ndelay_on(int fd) |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 27 | { |
Denys Vlasenko | e9a40e3 | 2011-01-24 00:29:55 +0100 | [diff] [blame] | 28 | int flags = fcntl(fd, F_GETFL); |
| 29 | if (flags & O_NONBLOCK) |
Denys Vlasenko | d6e7672 | 2014-09-22 21:14:02 +0200 | [diff] [blame] | 30 | return flags; |
Denys Vlasenko | e9a40e3 | 2011-01-24 00:29:55 +0100 | [diff] [blame] | 31 | fcntl(fd, F_SETFL, flags | O_NONBLOCK); |
Denys Vlasenko | d6e7672 | 2014-09-22 21:14:02 +0200 | [diff] [blame] | 32 | return flags; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Denys Vlasenko | d6e7672 | 2014-09-22 21:14:02 +0200 | [diff] [blame] | 35 | int FAST_FUNC ndelay_off(int fd) |
Denis Vlasenko | a5b3e76 | 2006-12-24 07:15:50 +0000 | [diff] [blame] | 36 | { |
Denys Vlasenko | e9a40e3 | 2011-01-24 00:29:55 +0100 | [diff] [blame] | 37 | int flags = fcntl(fd, F_GETFL); |
| 38 | if (!(flags & O_NONBLOCK)) |
Denys Vlasenko | d6e7672 | 2014-09-22 21:14:02 +0200 | [diff] [blame] | 39 | return flags; |
Denys Vlasenko | e9a40e3 | 2011-01-24 00:29:55 +0100 | [diff] [blame] | 40 | fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); |
Denys Vlasenko | d6e7672 | 2014-09-22 21:14:02 +0200 | [diff] [blame] | 41 | return flags; |
Denis Vlasenko | a5b3e76 | 2006-12-24 07:15:50 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Denys Vlasenko | e9a40e3 | 2011-01-24 00:29:55 +0100 | [diff] [blame] | 44 | void FAST_FUNC close_on_exec_on(int fd) |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 45 | { |
Denys Vlasenko | e9a40e3 | 2011-01-24 00:29:55 +0100 | [diff] [blame] | 46 | fcntl(fd, F_SETFD, FD_CLOEXEC); |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 49 | char* 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 Vlasenko | 56ea65c | 2008-01-06 03:26:53 +0000 | [diff] [blame] | 57 | |
Denys Vlasenko | ab60cd1 | 2010-06-10 10:54:44 +0200 | [diff] [blame] | 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 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 62 | char* FAST_FUNC utoa_to_buf(unsigned n, char *buf, unsigned buflen) |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 63 | { |
Denis Vlasenko | aae0311 | 2006-11-05 00:44:39 +0000 | [diff] [blame] | 64 | unsigned i, out, res; |
Denys Vlasenko | ab60cd1 | 2010-06-10 10:54:44 +0200 | [diff] [blame] | 65 | |
Rob Landley | 22d3958 | 2006-07-11 00:44:36 +0000 | [diff] [blame] | 66 | if (buflen) { |
Denis Vlasenko | aae0311 | 2006-11-05 00:44:39 +0000 | [diff] [blame] | 67 | out = 0; |
Denys Vlasenko | e4defe8 | 2018-03-28 17:12:56 +0200 | [diff] [blame] | 68 | |
| 69 | BUILD_BUG_ON(sizeof(n) != 4 && sizeof(n) != 8); |
Denys Vlasenko | ab60cd1 | 2010-06-10 10:54:44 +0200 | [diff] [blame] | 70 | if (sizeof(n) == 4) |
| 71 | // 2^32-1 = 4294967295 |
| 72 | i = 1000000000; |
Denys Vlasenko | e4defe8 | 2018-03-28 17:12:56 +0200 | [diff] [blame] | 73 | #if UINT_MAX > 0xffffffff /* prevents warning about "const too large" */ |
Denys Vlasenko | ab60cd1 | 2010-06-10 10:54:44 +0200 | [diff] [blame] | 74 | else |
| 75 | if (sizeof(n) == 8) |
| 76 | // 2^64-1 = 18446744073709551615 |
| 77 | i = 10000000000000000000; |
| 78 | #endif |
Denys Vlasenko | ab60cd1 | 2010-06-10 10:54:44 +0200 | [diff] [blame] | 79 | for (; i; i /= 10) { |
Denis Vlasenko | aae0311 | 2006-11-05 00:44:39 +0000 | [diff] [blame] | 80 | res = n / i; |
Denys Vlasenko | ab60cd1 | 2010-06-10 10:54:44 +0200 | [diff] [blame] | 81 | n = n % i; |
Denis Vlasenko | aae0311 | 2006-11-05 00:44:39 +0000 | [diff] [blame] | 82 | if (res || out || i == 1) { |
Denys Vlasenko | ab60cd1 | 2010-06-10 10:54:44 +0200 | [diff] [blame] | 83 | if (--buflen == 0) |
| 84 | break; |
Rob Landley | 22d3958 | 2006-07-11 00:44:36 +0000 | [diff] [blame] | 85 | out++; |
Rob Landley | 22d3958 | 2006-07-11 00:44:36 +0000 | [diff] [blame] | 86 | *buf++ = '0' + res; |
| 87 | } |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 90 | return buf; |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Denis Vlasenko | b12b1c8 | 2008-04-09 00:33:23 +0000 | [diff] [blame] | 93 | /* Convert signed integer to ascii, like utoa_to_buf() */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 94 | char* FAST_FUNC itoa_to_buf(int n, char *buf, unsigned buflen) |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 95 | { |
Denys Vlasenko | ab60cd1 | 2010-06-10 10:54:44 +0200 | [diff] [blame] | 96 | if (!buflen) |
| 97 | return buf; |
| 98 | if (n < 0) { |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 99 | n = -n; |
| 100 | *buf++ = '-'; |
Rob Landley | 22d3958 | 2006-07-11 00:44:36 +0000 | [diff] [blame] | 101 | buflen--; |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 102 | } |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 103 | return utoa_to_buf((unsigned)n, buf, buflen); |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Rob Landley | da9d1d0 | 2006-09-14 19:52:07 +0000 | [diff] [blame] | 106 | // The following two functions use a static buffer, so calling either one a |
| 107 | // second time will overwrite previous results. |
| 108 | // |
Denys Vlasenko | ab60cd1 | 2010-06-10 10:54:44 +0200 | [diff] [blame] | 109 | // The largest 32 bit integer is -2 billion plus NUL, or 1+10+1=12 bytes. |
| 110 | // 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] | 111 | // (sizeof(int) * 3 + 2 is correct for any width, even 8-bit) |
Rob Landley | da9d1d0 | 2006-09-14 19:52:07 +0000 | [diff] [blame] | 112 | |
Denis Vlasenko | b12b1c8 | 2008-04-09 00:33:23 +0000 | [diff] [blame] | 113 | static char local_buf[sizeof(int) * 3]; |
Rob Landley | 23b61be | 2006-08-04 20:20:03 +0000 | [diff] [blame] | 114 | |
Denys Vlasenko | ab60cd1 | 2010-06-10 10:54:44 +0200 | [diff] [blame] | 115 | /* Convert unsigned integer to ascii using a static buffer (returned). */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 116 | char* FAST_FUNC utoa(unsigned n) |
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 | *(utoa_to_buf(n, local_buf, sizeof(local_buf) - 1)) = '\0'; |
Rob Landley | 23b61be | 2006-08-04 20:20:03 +0000 | [diff] [blame] | 119 | |
| 120 | return local_buf; |
| 121 | } |
| 122 | |
Denis Vlasenko | b12b1c8 | 2008-04-09 00:33:23 +0000 | [diff] [blame] | 123 | /* Convert signed integer to ascii using a static buffer (returned). */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 124 | char* FAST_FUNC itoa(int n) |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 125 | { |
Denys Vlasenko | ab60cd1 | 2010-06-10 10:54:44 +0200 | [diff] [blame] | 126 | *(itoa_to_buf(n, local_buf, sizeof(local_buf) - 1)) = '\0'; |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 127 | |
| 128 | return local_buf; |
| 129 | } |
Rob Landley | df822f2 | 2006-07-15 23:00:46 +0000 | [diff] [blame] | 130 | |
Denis Vlasenko | b12b1c8 | 2008-04-09 00:33:23 +0000 | [diff] [blame] | 131 | /* Emit a string of hex representation of bytes */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 132 | char* FAST_FUNC bin2hex(char *p, const char *cp, int count) |
Denis Vlasenko | 3a34d0c | 2007-01-12 22:10:34 +0000 | [diff] [blame] | 133 | { |
| 134 | while (count) { |
| 135 | unsigned char c = *cp++; |
| 136 | /* put lowercase hex digits */ |
Denis Vlasenko | 98c0bba | 2007-01-26 23:31:05 +0000 | [diff] [blame] | 137 | *p++ = 0x20 | bb_hexdigits_upcase[c >> 4]; |
| 138 | *p++ = 0x20 | bb_hexdigits_upcase[c & 0xf]; |
Denis Vlasenko | 3a34d0c | 2007-01-12 22:10:34 +0000 | [diff] [blame] | 139 | count--; |
| 140 | } |
| 141 | return p; |
| 142 | } |
| 143 | |
Denys Vlasenko | 4836331 | 2010-04-04 15:29:32 +0200 | [diff] [blame] | 144 | /* Convert "[x]x[:][x]x[:][x]x[:][x]x" hex string to binary, no more than COUNT bytes */ |
| 145 | char* FAST_FUNC hex2bin(char *dst, const char *str, int count) |
| 146 | { |
| 147 | errno = EINVAL; |
| 148 | while (*str && count) { |
| 149 | uint8_t val; |
| 150 | uint8_t c = *str++; |
| 151 | if (isdigit(c)) |
| 152 | val = c - '0'; |
| 153 | else if ((c|0x20) >= 'a' && (c|0x20) <= 'f') |
| 154 | val = (c|0x20) - ('a' - 10); |
| 155 | else |
| 156 | return NULL; |
| 157 | val <<= 4; |
| 158 | c = *str; |
| 159 | if (isdigit(c)) |
| 160 | val |= c - '0'; |
| 161 | else if ((c|0x20) >= 'a' && (c|0x20) <= 'f') |
| 162 | val |= (c|0x20) - ('a' - 10); |
| 163 | else if (c == ':' || c == '\0') |
| 164 | val >>= 4; |
| 165 | else |
| 166 | return NULL; |
| 167 | |
| 168 | *dst++ = val; |
| 169 | if (c != '\0') |
| 170 | str++; |
| 171 | if (*str == ':') |
| 172 | str++; |
| 173 | count--; |
| 174 | } |
| 175 | errno = (*str ? ERANGE : 0); |
| 176 | return dst; |
| 177 | } |
| 178 | |
Denis Vlasenko | b12b1c8 | 2008-04-09 00:33:23 +0000 | [diff] [blame] | 179 | /* 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] | 180 | #ifdef UNUSED |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 181 | off_t FAST_FUNC fdlength(int fd) |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 182 | { |
| 183 | off_t bottom = 0, top = 0, pos; |
| 184 | long size; |
| 185 | |
Rob Landley | da9d1d0 | 2006-09-14 19:52:07 +0000 | [diff] [blame] | 186 | // If the ioctl works for this, return it. |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 187 | |
| 188 | if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512; |
| 189 | |
Denis Vlasenko | 621204b | 2006-10-27 09:03:24 +0000 | [diff] [blame] | 190 | // FIXME: explain why lseek(SEEK_END) is not used here! |
| 191 | |
Rob Landley | da9d1d0 | 2006-09-14 19:52:07 +0000 | [diff] [blame] | 192 | // If not, do a binary search for the last location we can read. (Some |
| 193 | // block devices don't do BLKGETSIZE right.) |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 194 | |
| 195 | do { |
| 196 | char temp; |
| 197 | |
Denis Vlasenko | d25a264 | 2006-09-05 09:36:19 +0000 | [diff] [blame] | 198 | pos = bottom + (top - bottom) / 2; |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 199 | |
Rob Landley | da9d1d0 | 2006-09-14 19:52:07 +0000 | [diff] [blame] | 200 | // If we can read from the current location, it's bigger. |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 201 | |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 202 | 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] | 203 | if (bottom == top) bottom = top = (top+1) * 2; |
| 204 | else bottom = pos; |
| 205 | |
Rob Landley | da9d1d0 | 2006-09-14 19:52:07 +0000 | [diff] [blame] | 206 | // If we can't, it's smaller. |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 207 | } else { |
| 208 | if (bottom == top) { |
| 209 | if (!top) return 0; |
| 210 | bottom = top/2; |
| 211 | } |
| 212 | else top = pos; |
| 213 | } |
| 214 | } while (bottom + 1 != top); |
| 215 | |
| 216 | return pos + 1; |
| 217 | } |
Denis Vlasenko | b5c60fc | 2008-01-27 23:41:34 +0000 | [diff] [blame] | 218 | #endif |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 219 | |
Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [diff] [blame] | 220 | int FAST_FUNC bb_putchar_stderr(char ch) |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 221 | { |
Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [diff] [blame] | 222 | return write(STDERR_FILENO, &ch, 1); |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Denys Vlasenko | 729ecb8 | 2010-06-07 14:14:26 +0200 | [diff] [blame] | 225 | ssize_t FAST_FUNC full_write1_str(const char *str) |
| 226 | { |
| 227 | return full_write(STDOUT_FILENO, str, strlen(str)); |
| 228 | } |
| 229 | |
| 230 | ssize_t FAST_FUNC full_write2_str(const char *str) |
| 231 | { |
| 232 | return full_write(STDERR_FILENO, str, strlen(str)); |
| 233 | } |
| 234 | |
Denys Vlasenko | c175c46 | 2010-04-18 22:09:30 -0700 | [diff] [blame] | 235 | static int wh_helper(int value, int def_val, const char *env_name, int *err) |
| 236 | { |
Denys Vlasenko | 7c3c92c | 2016-10-31 01:52:18 +0100 | [diff] [blame] | 237 | /* Envvars override even if "value" from ioctl is valid (>0). |
| 238 | * Rationale: it's impossible to guess what user wants. |
| 239 | * For example: "man CMD | ...": should "man" format output |
| 240 | * to stdout's width? stdin's width? /dev/tty's width? 80 chars? |
| 241 | * We _cant_ know it. If "..." saves text for e.g. email, |
| 242 | * then it's probably 80 chars. |
| 243 | * If "..." is, say, "grep -v DISCARD | $PAGER", then user |
| 244 | * would prefer his tty's width to be used! |
| 245 | * |
| 246 | * Since we don't know, at least allow user to do this: |
| 247 | * "COLUMNS=80 man CMD | ..." |
| 248 | */ |
| 249 | char *s = getenv(env_name); |
| 250 | if (s) { |
| 251 | value = atoi(s); |
| 252 | /* If LINES/COLUMNS are set, pretend that there is |
| 253 | * no error getting w/h, this prevents some ugly |
| 254 | * cursor tricks by our callers */ |
| 255 | *err = 0; |
Denys Vlasenko | c175c46 | 2010-04-18 22:09:30 -0700 | [diff] [blame] | 256 | } |
Denys Vlasenko | 7c3c92c | 2016-10-31 01:52:18 +0100 | [diff] [blame] | 257 | |
Denys Vlasenko | c175c46 | 2010-04-18 22:09:30 -0700 | [diff] [blame] | 258 | if (value <= 1 || value >= 30000) |
| 259 | value = def_val; |
| 260 | return value; |
| 261 | } |
| 262 | |
Rob Landley | fbdf121 | 2006-09-20 22:06:01 +0000 | [diff] [blame] | 263 | /* 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] | 264 | * height, in which case that value will not be set. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 265 | 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] | 266 | { |
Denys Vlasenko | c175c46 | 2010-04-18 22:09:30 -0700 | [diff] [blame] | 267 | struct winsize win; |
| 268 | int err; |
Denys Vlasenko | 7c3c92c | 2016-10-31 01:52:18 +0100 | [diff] [blame] | 269 | int close_me = -1; |
| 270 | |
| 271 | if (fd == -1) { |
| 272 | if (isatty(STDOUT_FILENO)) |
| 273 | fd = STDOUT_FILENO; |
| 274 | else |
| 275 | if (isatty(STDERR_FILENO)) |
| 276 | fd = STDERR_FILENO; |
| 277 | else |
| 278 | if (isatty(STDIN_FILENO)) |
| 279 | fd = STDIN_FILENO; |
| 280 | else |
| 281 | close_me = fd = open("/dev/tty", O_RDONLY); |
| 282 | } |
Denis Vlasenko | 621204b | 2006-10-27 09:03:24 +0000 | [diff] [blame] | 283 | |
Denys Vlasenko | c175c46 | 2010-04-18 22:09:30 -0700 | [diff] [blame] | 284 | win.ws_row = 0; |
| 285 | win.ws_col = 0; |
| 286 | /* I've seen ioctl returning 0, but row/col is (still?) 0. |
| 287 | * We treat that as an error too. */ |
| 288 | err = ioctl(fd, TIOCGWINSZ, &win) != 0 || win.ws_row == 0; |
| 289 | if (height) |
| 290 | *height = wh_helper(win.ws_row, 24, "LINES", &err); |
| 291 | if (width) |
| 292 | *width = wh_helper(win.ws_col, 80, "COLUMNS", &err); |
Denys Vlasenko | 7c3c92c | 2016-10-31 01:52:18 +0100 | [diff] [blame] | 293 | |
| 294 | if (close_me >= 0) |
| 295 | close(close_me); |
| 296 | |
Denys Vlasenko | c175c46 | 2010-04-18 22:09:30 -0700 | [diff] [blame] | 297 | return err; |
Rob Landley | fbdf121 | 2006-09-20 22:06:01 +0000 | [diff] [blame] | 298 | } |
Denys Vlasenko | 641caae | 2015-10-23 01:44:22 +0200 | [diff] [blame] | 299 | int FAST_FUNC get_terminal_width(int fd) |
| 300 | { |
| 301 | unsigned width; |
| 302 | get_terminal_width_height(fd, &width, NULL); |
| 303 | return width; |
| 304 | } |
Denis Vlasenko | 202ac50 | 2008-11-05 13:20:58 +0000 | [diff] [blame] | 305 | |
Denys Vlasenko | df0383c | 2021-06-05 08:33:03 +0200 | [diff] [blame] | 306 | int FAST_FUNC is_TERM_dumb(void) |
Sören Tempel | 3d9c649 | 2021-05-23 14:14:10 +0200 | [diff] [blame] | 307 | { |
| 308 | char *term = getenv("TERM"); |
| 309 | return term && strcmp(term, "dumb") == 0; |
| 310 | } |
| 311 | |
Denis Vlasenko | 202ac50 | 2008-11-05 13:20:58 +0000 | [diff] [blame] | 312 | int FAST_FUNC tcsetattr_stdin_TCSANOW(const struct termios *tp) |
| 313 | { |
| 314 | return tcsetattr(STDIN_FILENO, TCSANOW, tp); |
| 315 | } |
Pascal Bellard | 21e8e8d | 2010-07-04 00:57:03 +0200 | [diff] [blame] | 316 | |
Denys Vlasenko | aaaaaa5 | 2017-09-15 17:14:01 +0200 | [diff] [blame] | 317 | int FAST_FUNC get_termios_and_make_raw(int fd, struct termios *newterm, struct termios *oldterm, int flags) |
Denys Vlasenko | 01ccdd1 | 2017-01-11 16:17:59 +0100 | [diff] [blame] | 318 | { |
Denys Vlasenko | aaaaaa5 | 2017-09-15 17:14:01 +0200 | [diff] [blame] | 319 | //TODO: slattach, shell read might be adapted to use this too: grep for "tcsetattr", "[VTIME] = 0" |
| 320 | int r; |
Denys Vlasenko | 01ccdd1 | 2017-01-11 16:17:59 +0100 | [diff] [blame] | 321 | |
Denys Vlasenko | aaaaaa5 | 2017-09-15 17:14:01 +0200 | [diff] [blame] | 322 | memset(oldterm, 0, sizeof(*oldterm)); /* paranoia */ |
| 323 | r = tcgetattr(fd, oldterm); |
| 324 | *newterm = *oldterm; |
Denys Vlasenko | 01ccdd1 | 2017-01-11 16:17:59 +0100 | [diff] [blame] | 325 | |
| 326 | /* Turn off buffered input (ICANON) |
| 327 | * Turn off echoing (ECHO) |
| 328 | * and separate echoing of newline (ECHONL, normally off anyway) |
| 329 | */ |
Denys Vlasenko | aaaaaa5 | 2017-09-15 17:14:01 +0200 | [diff] [blame] | 330 | newterm->c_lflag &= ~(ICANON | ECHO | ECHONL); |
Denys Vlasenko | 01ccdd1 | 2017-01-11 16:17:59 +0100 | [diff] [blame] | 331 | if (flags & TERMIOS_CLEAR_ISIG) { |
| 332 | /* dont recognize INT/QUIT/SUSP chars */ |
Denys Vlasenko | aaaaaa5 | 2017-09-15 17:14:01 +0200 | [diff] [blame] | 333 | newterm->c_lflag &= ~ISIG; |
Denys Vlasenko | 01ccdd1 | 2017-01-11 16:17:59 +0100 | [diff] [blame] | 334 | } |
| 335 | /* reads will block only if < 1 char is available */ |
Denys Vlasenko | aaaaaa5 | 2017-09-15 17:14:01 +0200 | [diff] [blame] | 336 | newterm->c_cc[VMIN] = 1; |
Denys Vlasenko | 01ccdd1 | 2017-01-11 16:17:59 +0100 | [diff] [blame] | 337 | /* no timeout (reads block forever) */ |
Denys Vlasenko | aaaaaa5 | 2017-09-15 17:14:01 +0200 | [diff] [blame] | 338 | newterm->c_cc[VTIME] = 0; |
Denys Vlasenko | aaaaaa5 | 2017-09-15 17:14:01 +0200 | [diff] [blame] | 339 | /* IXON, IXOFF, and IXANY: |
| 340 | * IXOFF=1: sw flow control is enabled on input queue: |
| 341 | * tty transmits a STOP char when input queue is close to full |
| 342 | * and transmits a START char when input queue is nearly empty. |
| 343 | * IXON=1: sw flow control is enabled on output queue: |
| 344 | * tty will stop sending if STOP char is received, |
| 345 | * and resume sending if START is received, or if any char |
| 346 | * is received and IXANY=1. |
| 347 | */ |
Denys Vlasenko | 058a153 | 2018-04-16 10:24:48 +0200 | [diff] [blame] | 348 | if (flags & TERMIOS_RAW_CRNL_INPUT) { |
Denys Vlasenko | aaaaaa5 | 2017-09-15 17:14:01 +0200 | [diff] [blame] | 349 | /* IXON=0: XON/XOFF chars are treated as normal chars (why we do this?) */ |
Denys Vlasenko | 01ccdd1 | 2017-01-11 16:17:59 +0100 | [diff] [blame] | 350 | /* dont convert CR to NL on input */ |
Denys Vlasenko | aaaaaa5 | 2017-09-15 17:14:01 +0200 | [diff] [blame] | 351 | newterm->c_iflag &= ~(IXON | ICRNL); |
Denys Vlasenko | 058a153 | 2018-04-16 10:24:48 +0200 | [diff] [blame] | 352 | } |
| 353 | if (flags & TERMIOS_RAW_CRNL_OUTPUT) { |
Denys Vlasenko | aaaaaa5 | 2017-09-15 17:14:01 +0200 | [diff] [blame] | 354 | /* dont convert NL to CR+NL on output */ |
| 355 | newterm->c_oflag &= ~(ONLCR); |
Denys Vlasenko | 7735e52 | 2017-09-15 17:19:55 +0200 | [diff] [blame] | 356 | /* Maybe clear more c_oflag bits? Usually, only OPOST and ONLCR are set. |
| 357 | * OPOST Enable output processing (reqd for OLCUC and *NL* bits to work) |
Denys Vlasenko | aaaaaa5 | 2017-09-15 17:14:01 +0200 | [diff] [blame] | 358 | * OLCUC Map lowercase characters to uppercase on output. |
| 359 | * OCRNL Map CR to NL on output. |
| 360 | * ONOCR Don't output CR at column 0. |
| 361 | * ONLRET Don't output CR. |
| 362 | */ |
Denys Vlasenko | 01ccdd1 | 2017-01-11 16:17:59 +0100 | [diff] [blame] | 363 | } |
| 364 | if (flags & TERMIOS_RAW_INPUT) { |
James Clarke | 24e17b4 | 2017-10-30 15:18:32 +0100 | [diff] [blame] | 365 | #ifndef IMAXBEL |
| 366 | # define IMAXBEL 0 |
| 367 | #endif |
| 368 | #ifndef IUCLC |
| 369 | # define IUCLC 0 |
| 370 | #endif |
| 371 | #ifndef IXANY |
| 372 | # define IXANY 0 |
| 373 | #endif |
Denys Vlasenko | 058a153 | 2018-04-16 10:24:48 +0200 | [diff] [blame] | 374 | /* IXOFF=0: disable sending XON/XOFF if input buf is full |
| 375 | * IXON=0: input XON/XOFF chars are not special |
| 376 | * BRKINT=0: dont send SIGINT on break |
| 377 | * IMAXBEL=0: dont echo BEL on input line too long |
| 378 | * INLCR,ICRNL,IUCLC: dont convert anything on input |
| 379 | */ |
Denys Vlasenko | aaaaaa5 | 2017-09-15 17:14:01 +0200 | [diff] [blame] | 380 | newterm->c_iflag &= ~(IXOFF|IXON|IXANY|BRKINT|INLCR|ICRNL|IUCLC|IMAXBEL); |
Denys Vlasenko | 01ccdd1 | 2017-01-11 16:17:59 +0100 | [diff] [blame] | 381 | } |
Denys Vlasenko | aaaaaa5 | 2017-09-15 17:14:01 +0200 | [diff] [blame] | 382 | return r; |
| 383 | } |
Denys Vlasenko | 01ccdd1 | 2017-01-11 16:17:59 +0100 | [diff] [blame] | 384 | |
Denys Vlasenko | aaaaaa5 | 2017-09-15 17:14:01 +0200 | [diff] [blame] | 385 | int FAST_FUNC set_termios_to_raw(int fd, struct termios *oldterm, int flags) |
| 386 | { |
| 387 | struct termios newterm; |
| 388 | |
| 389 | get_termios_and_make_raw(fd, &newterm, oldterm, flags); |
Denys Vlasenko | 01ccdd1 | 2017-01-11 16:17:59 +0100 | [diff] [blame] | 390 | return tcsetattr(fd, TCSANOW, &newterm); |
| 391 | } |
| 392 | |
Pascal Bellard | 21e8e8d | 2010-07-04 00:57:03 +0200 | [diff] [blame] | 393 | pid_t FAST_FUNC safe_waitpid(pid_t pid, int *wstat, int options) |
| 394 | { |
| 395 | pid_t r; |
| 396 | |
| 397 | do |
| 398 | r = waitpid(pid, wstat, options); |
| 399 | while ((r == -1) && (errno == EINTR)); |
| 400 | return r; |
| 401 | } |
| 402 | |
| 403 | pid_t FAST_FUNC wait_any_nohang(int *wstat) |
| 404 | { |
| 405 | return safe_waitpid(-1, wstat, WNOHANG); |
| 406 | } |
| 407 | |
| 408 | // Wait for the specified child PID to exit, returning child's error return. |
| 409 | int FAST_FUNC wait4pid(pid_t pid) |
| 410 | { |
| 411 | int status; |
| 412 | |
| 413 | if (pid <= 0) { |
| 414 | /*errno = ECHILD; -- wrong. */ |
| 415 | /* we expect errno to be already set from failed [v]fork/exec */ |
| 416 | return -1; |
| 417 | } |
| 418 | if (safe_waitpid(pid, &status, 0) == -1) |
| 419 | return -1; |
| 420 | if (WIFEXITED(status)) |
| 421 | return WEXITSTATUS(status); |
| 422 | if (WIFSIGNALED(status)) |
| 423 | return WTERMSIG(status) + 0x180; |
| 424 | return 0; |
| 425 | } |
Denys Vlasenko | db5546c | 2022-01-05 22:16:06 +0100 | [diff] [blame] | 426 | |
| 427 | void FAST_FUNC exit_SUCCESS(void) |
| 428 | { |
| 429 | exit(EXIT_SUCCESS); |
| 430 | } |
| 431 | |
| 432 | void FAST_FUNC _exit_SUCCESS(void) |
| 433 | { |
| 434 | _exit(EXIT_SUCCESS); |
| 435 | } |