blob: d93dd2af9c5bd68d7b74dbd8fb556fba966ff09c [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 *
Rob Landleyfbdf1212006-09-20 22:06:01 +00009 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
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 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000028int FAST_FUNC ndelay_on(int fd)
Denis Vlasenko75f8d082006-11-22 15:54:52 +000029{
Denys Vlasenkoab19ede2009-11-11 21:05:42 +010030 return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
Denis Vlasenko75f8d082006-11-22 15:54:52 +000031}
32
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000033int FAST_FUNC ndelay_off(int fd)
Denis Vlasenkoa5b3e762006-12-24 07:15:50 +000034{
Denys Vlasenkoab19ede2009-11-11 21:05:42 +010035 return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK);
Denis Vlasenkoa5b3e762006-12-24 07:15:50 +000036}
37
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000038int FAST_FUNC close_on_exec_on(int fd)
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +000039{
Denis Vlasenkob12b1c82008-04-09 00:33:23 +000040 return fcntl(fd, F_SETFD, FD_CLOEXEC);
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +000041}
42
Denis Vlasenko360d9662008-12-02 18:18:50 +000043char* FAST_FUNC strncpy_IFNAMSIZ(char *dst, const char *src)
44{
45#ifndef IFNAMSIZ
46 enum { IFNAMSIZ = 16 };
47#endif
48 return strncpy(dst, src, IFNAMSIZ);
49}
50
Denis Vlasenko56ea65c2008-01-06 03:26:53 +000051
Denis Vlasenko450196c2007-03-28 21:57:12 +000052// Convert unsigned integer to ascii, writing into supplied buffer.
53// A truncated result contains the first few digits of the result ala strncpy.
54// Returns a pointer past last generated digit, does _not_ store NUL.
Denis Vlasenkoaae03112006-11-05 00:44:39 +000055void BUG_sizeof_unsigned_not_4(void);
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000056char* FAST_FUNC utoa_to_buf(unsigned n, char *buf, unsigned buflen)
Rob Landley5b88a382006-07-10 07:41:34 +000057{
Denis Vlasenkoaae03112006-11-05 00:44:39 +000058 unsigned i, out, res;
59 if (sizeof(unsigned) != 4)
60 BUG_sizeof_unsigned_not_4();
Rob Landley22d39582006-07-11 00:44:36 +000061 if (buflen) {
Denis Vlasenkoaae03112006-11-05 00:44:39 +000062 out = 0;
63 for (i = 1000000000; i; i /= 10) {
64 res = n / i;
65 if (res || out || i == 1) {
66 if (!--buflen) break;
Rob Landley22d39582006-07-11 00:44:36 +000067 out++;
68 n -= res*i;
69 *buf++ = '0' + res;
70 }
Rob Landley5b88a382006-07-10 07:41:34 +000071 }
72 }
Denis Vlasenko10457b92007-03-27 22:01:31 +000073 return buf;
Rob Landley5b88a382006-07-10 07:41:34 +000074}
75
Denis Vlasenkob12b1c82008-04-09 00:33:23 +000076/* Convert signed integer to ascii, like utoa_to_buf() */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000077char* FAST_FUNC itoa_to_buf(int n, char *buf, unsigned buflen)
Rob Landley5b88a382006-07-10 07:41:34 +000078{
Denis Vlasenko6c106572008-04-19 19:05:12 +000079 if (buflen && n < 0) {
Rob Landley5b88a382006-07-10 07:41:34 +000080 n = -n;
81 *buf++ = '-';
Rob Landley22d39582006-07-11 00:44:36 +000082 buflen--;
Rob Landley5b88a382006-07-10 07:41:34 +000083 }
Denis Vlasenko10457b92007-03-27 22:01:31 +000084 return utoa_to_buf((unsigned)n, buf, buflen);
Rob Landley5b88a382006-07-10 07:41:34 +000085}
86
Rob Landleyda9d1d02006-09-14 19:52:07 +000087// The following two functions use a static buffer, so calling either one a
88// second time will overwrite previous results.
89//
90// The largest 32 bit integer is -2 billion plus null terminator, or 12 bytes.
Denis Vlasenkob12b1c82008-04-09 00:33:23 +000091// It so happens that sizeof(int) * 3 is enough for 32+ bits.
92// (sizeof(int) * 3 + 2 is correct for any width, even 8-bit)
Rob Landleyda9d1d02006-09-14 19:52:07 +000093
Denis Vlasenkob12b1c82008-04-09 00:33:23 +000094static char local_buf[sizeof(int) * 3];
Rob Landley23b61be2006-08-04 20:20:03 +000095
Rob Landleyda9d1d02006-09-14 19:52:07 +000096// Convert unsigned integer to ascii using a static buffer (returned).
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000097char* FAST_FUNC utoa(unsigned n)
Rob Landley23b61be2006-08-04 20:20:03 +000098{
Denis Vlasenko10457b92007-03-27 22:01:31 +000099 *(utoa_to_buf(n, local_buf, sizeof(local_buf))) = '\0';
Rob Landley23b61be2006-08-04 20:20:03 +0000100
101 return local_buf;
102}
103
Denis Vlasenkob12b1c82008-04-09 00:33:23 +0000104/* Convert signed integer to ascii using a static buffer (returned). */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000105char* FAST_FUNC itoa(int n)
Rob Landley5b88a382006-07-10 07:41:34 +0000106{
Denis Vlasenko10457b92007-03-27 22:01:31 +0000107 *(itoa_to_buf(n, local_buf, sizeof(local_buf))) = '\0';
Rob Landley5b88a382006-07-10 07:41:34 +0000108
109 return local_buf;
110}
Rob Landleydf822f22006-07-15 23:00:46 +0000111
Denis Vlasenkob12b1c82008-04-09 00:33:23 +0000112/* Emit a string of hex representation of bytes */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000113char* FAST_FUNC bin2hex(char *p, const char *cp, int count)
Denis Vlasenko3a34d0c2007-01-12 22:10:34 +0000114{
115 while (count) {
116 unsigned char c = *cp++;
117 /* put lowercase hex digits */
Denis Vlasenko98c0bba2007-01-26 23:31:05 +0000118 *p++ = 0x20 | bb_hexdigits_upcase[c >> 4];
119 *p++ = 0x20 | bb_hexdigits_upcase[c & 0xf];
Denis Vlasenko3a34d0c2007-01-12 22:10:34 +0000120 count--;
121 }
122 return p;
123}
124
Denys Vlasenko48363312010-04-04 15:29:32 +0200125/* Convert "[x]x[:][x]x[:][x]x[:][x]x" hex string to binary, no more than COUNT bytes */
126char* FAST_FUNC hex2bin(char *dst, const char *str, int count)
127{
128 errno = EINVAL;
129 while (*str && count) {
130 uint8_t val;
131 uint8_t c = *str++;
132 if (isdigit(c))
133 val = c - '0';
134 else if ((c|0x20) >= 'a' && (c|0x20) <= 'f')
135 val = (c|0x20) - ('a' - 10);
136 else
137 return NULL;
138 val <<= 4;
139 c = *str;
140 if (isdigit(c))
141 val |= c - '0';
142 else if ((c|0x20) >= 'a' && (c|0x20) <= 'f')
143 val |= (c|0x20) - ('a' - 10);
144 else if (c == ':' || c == '\0')
145 val >>= 4;
146 else
147 return NULL;
148
149 *dst++ = val;
150 if (c != '\0')
151 str++;
152 if (*str == ':')
153 str++;
154 count--;
155 }
156 errno = (*str ? ERANGE : 0);
157 return dst;
158}
159
Denis Vlasenkob12b1c82008-04-09 00:33:23 +0000160/* Return how long the file at fd is, if there's any way to determine it. */
Denis Vlasenkob5c60fc2008-01-27 23:41:34 +0000161#ifdef UNUSED
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000162off_t FAST_FUNC fdlength(int fd)
Rob Landley53437472006-07-16 08:14:35 +0000163{
164 off_t bottom = 0, top = 0, pos;
165 long size;
166
Rob Landleyda9d1d02006-09-14 19:52:07 +0000167 // If the ioctl works for this, return it.
Rob Landley53437472006-07-16 08:14:35 +0000168
169 if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512;
170
Denis Vlasenko621204b2006-10-27 09:03:24 +0000171 // FIXME: explain why lseek(SEEK_END) is not used here!
172
Rob Landleyda9d1d02006-09-14 19:52:07 +0000173 // If not, do a binary search for the last location we can read. (Some
174 // block devices don't do BLKGETSIZE right.)
Rob Landley53437472006-07-16 08:14:35 +0000175
176 do {
177 char temp;
178
Denis Vlasenkod25a2642006-09-05 09:36:19 +0000179 pos = bottom + (top - bottom) / 2;
Rob Landley53437472006-07-16 08:14:35 +0000180
Rob Landleyda9d1d02006-09-14 19:52:07 +0000181 // If we can read from the current location, it's bigger.
Rob Landley53437472006-07-16 08:14:35 +0000182
Denis Vlasenkoea620772006-10-14 02:23:43 +0000183 if (lseek(fd, pos, SEEK_SET)>=0 && safe_read(fd, &temp, 1)==1) {
Rob Landley53437472006-07-16 08:14:35 +0000184 if (bottom == top) bottom = top = (top+1) * 2;
185 else bottom = pos;
186
Rob Landleyda9d1d02006-09-14 19:52:07 +0000187 // If we can't, it's smaller.
Rob Landley53437472006-07-16 08:14:35 +0000188
189 } else {
190 if (bottom == top) {
191 if (!top) return 0;
192 bottom = top/2;
193 }
194 else top = pos;
195 }
196 } while (bottom + 1 != top);
197
198 return pos + 1;
199}
Denis Vlasenkob5c60fc2008-01-27 23:41:34 +0000200#endif
Rob Landleyd921b2e2006-08-03 15:41:12 +0000201
Denis Vlasenko4e12b1a2008-12-23 23:36:47 +0000202char* FAST_FUNC xmalloc_ttyname(int fd)
203{
204 char *buf = xzalloc(128);
205 int r = ttyname_r(fd, buf, 127);
206 if (r) {
207 free(buf);
208 buf = NULL;
209 }
210 return buf;
211}
212
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700213static int wh_helper(int value, int def_val, const char *env_name, int *err)
214{
215 if (value == 0) {
216 char *s = getenv(env_name);
217 if (s) {
218 value = atoi(s);
219 /* If LINES/COLUMNS are set, pretent that there is
220 * no error getting w/h, this prevents some ugly
221 * cursor tricks by our callers */
222 *err = 0;
223 }
224 }
225 if (value <= 1 || value >= 30000)
226 value = def_val;
227 return value;
228}
229
Rob Landleyfbdf1212006-09-20 22:06:01 +0000230/* It is perfectly ok to pass in a NULL for either width or for
Denis Vlasenko621204b2006-10-27 09:03:24 +0000231 * height, in which case that value will not be set. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000232int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *height)
Rob Landleyfbdf1212006-09-20 22:06:01 +0000233{
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700234 struct winsize win;
235 int err;
Denis Vlasenko621204b2006-10-27 09:03:24 +0000236
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700237 win.ws_row = 0;
238 win.ws_col = 0;
239 /* I've seen ioctl returning 0, but row/col is (still?) 0.
240 * We treat that as an error too. */
241 err = ioctl(fd, TIOCGWINSZ, &win) != 0 || win.ws_row == 0;
242 if (height)
243 *height = wh_helper(win.ws_row, 24, "LINES", &err);
244 if (width)
245 *width = wh_helper(win.ws_col, 80, "COLUMNS", &err);
246 return err;
Rob Landleyfbdf1212006-09-20 22:06:01 +0000247}
Denis Vlasenko202ac502008-11-05 13:20:58 +0000248
249int FAST_FUNC tcsetattr_stdin_TCSANOW(const struct termios *tp)
250{
251 return tcsetattr(STDIN_FILENO, TCSANOW, tp);
252}
Vladimir Dronnikovdb67a202009-10-15 09:24:25 +0200253
254void FAST_FUNC generate_uuid(uint8_t *buf)
255{
256 /* http://www.ietf.org/rfc/rfc4122.txt
257 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
258 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
259 * | time_low |
260 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
261 * | time_mid | time_hi_and_version |
262 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
263 * |clk_seq_and_variant | node (0-1) |
264 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
265 * | node (2-5) |
266 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
267 * IOW, uuid has this layout:
268 * uint32_t time_low (big endian)
269 * uint16_t time_mid (big endian)
270 * uint16_t time_hi_and_version (big endian)
271 * version is a 4-bit field:
272 * 1 Time-based
273 * 2 DCE Security, with embedded POSIX UIDs
274 * 3 Name-based (MD5)
275 * 4 Randomly generated
276 * 5 Name-based (SHA-1)
277 * uint16_t clk_seq_and_variant (big endian)
278 * variant is a 3-bit field:
279 * 0xx Reserved, NCS backward compatibility
280 * 10x The variant specified in rfc4122
281 * 110 Reserved, Microsoft backward compatibility
282 * 111 Reserved for future definition
283 * uint8_t node[6]
284 *
285 * For version 4, these bits are set/cleared:
286 * time_hi_and_version & 0x0fff | 0x4000
287 * clk_seq_and_variant & 0x3fff | 0x8000
288 */
289 pid_t pid;
290 int i;
291
292 i = open("/dev/urandom", O_RDONLY);
293 if (i >= 0) {
294 read(i, buf, 16);
295 close(i);
296 }
297 /* Paranoia. /dev/urandom may be missing.
298 * rand() is guaranteed to generate at least [0, 2^15) range,
299 * but lowest bits in some libc are not so "random". */
300 srand(monotonic_us());
301 pid = getpid();
302 while (1) {
303 for (i = 0; i < 16; i++)
304 buf[i] ^= rand() >> 5;
305 if (pid == 0)
306 break;
307 srand(pid);
308 pid = 0;
309 }
310
311 /* version = 4 */
312 buf[4 + 2 ] = (buf[4 + 2 ] & 0x0f) | 0x40;
313 /* variant = 10x */
314 buf[4 + 2 + 2] = (buf[4 + 2 + 2] & 0x3f) | 0x80;
315}