blob: a86efc2981d49cd80c5fa5016bbd63b4b033f38c [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{
Denis Vlasenkod37f2222007-08-19 13:42:08 +000030 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{
Denis Vlasenkod37f2222007-08-19 13:42:08 +000035 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 Vlasenkob12b1c82008-04-09 00:33:23 +000051/* Convert unsigned long long value into compact 4-char
Denis Vlasenko56ea65c2008-01-06 03:26:53 +000052 * representation. Examples: "1234", "1.2k", " 27M", "123T"
53 * String is not terminated (buf[4] is untouched) */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000054void FAST_FUNC smart_ulltoa4(unsigned long long ul, char buf[5], const char *scale)
Denis Vlasenkoaae03112006-11-05 00:44:39 +000055{
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000056 const char *fmt;
Denis Vlasenkoaae03112006-11-05 00:44:39 +000057 char c;
Denis Vlasenkob308d812007-08-28 19:35:34 +000058 unsigned v, u, idx = 0;
59
60 if (ul > 9999) { // do not scale if 9999 or less
61 ul *= 10;
62 do {
Denis Vlasenkoaae03112006-11-05 00:44:39 +000063 ul /= 1024;
64 idx++;
Denis Vlasenkob308d812007-08-28 19:35:34 +000065 } while (ul >= 10000);
Denis Vlasenkoaae03112006-11-05 00:44:39 +000066 }
67 v = ul; // ullong divisions are expensive, avoid them
68
69 fmt = " 123456789";
Denis Vlasenkob308d812007-08-28 19:35:34 +000070 u = v / 10;
71 v = v % 10;
72 if (!idx) {
73 // 9999 or less: use "1234" format
74 // u is value/10, v is last digit
75 c = buf[0] = " 123456789"[u/100];
Denis Vlasenko1b6fa4c2007-03-24 12:08:36 +000076 if (c != ' ') fmt = "0123456789";
Denis Vlasenkob308d812007-08-28 19:35:34 +000077 c = buf[1] = fmt[u/10%10];
Denis Vlasenko1b6fa4c2007-03-24 12:08:36 +000078 if (c != ' ') fmt = "0123456789";
Denis Vlasenkob308d812007-08-28 19:35:34 +000079 buf[2] = fmt[u%10];
80 buf[3] = "0123456789"[v];
Denis Vlasenkoaae03112006-11-05 00:44:39 +000081 } else {
Denis Vlasenkob308d812007-08-28 19:35:34 +000082 // u is value, v is 1/10ths (allows for 9.2M format)
83 if (u >= 10) {
84 // value is >= 10: use "123M', " 12M" formats
85 c = buf[0] = " 123456789"[u/100];
Denis Vlasenko1b6fa4c2007-03-24 12:08:36 +000086 if (c != ' ') fmt = "0123456789";
Denis Vlasenkob308d812007-08-28 19:35:34 +000087 v = u % 10;
88 u = u / 10;
89 buf[1] = fmt[u%10];
90 } else {
91 // value is < 10: use "9.2M" format
92 buf[0] = "0123456789"[u];
Denis Vlasenkoaae03112006-11-05 00:44:39 +000093 buf[1] = '.';
Denis Vlasenkoaae03112006-11-05 00:44:39 +000094 }
Denis Vlasenkob308d812007-08-28 19:35:34 +000095 buf[2] = "0123456789"[v];
Denis Vlasenko56ea65c2008-01-06 03:26:53 +000096 buf[3] = scale[idx]; /* typically scale = " kmgt..." */
Denis Vlasenkoaae03112006-11-05 00:44:39 +000097 }
Denis Vlasenkoaae03112006-11-05 00:44:39 +000098}
99
Denis Vlasenkob12b1c82008-04-09 00:33:23 +0000100/* Convert unsigned long long value into compact 5-char representation.
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000101 * String is not terminated (buf[5] is untouched) */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000102void FAST_FUNC smart_ulltoa5(unsigned long long ul, char buf[6], const char *scale)
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000103{
104 const char *fmt;
105 char c;
106 unsigned v, u, idx = 0;
107
108 if (ul > 99999) { // do not scale if 99999 or less
109 ul *= 10;
110 do {
111 ul /= 1024;
112 idx++;
113 } while (ul >= 100000);
114 }
115 v = ul; // ullong divisions are expensive, avoid them
116
117 fmt = " 123456789";
118 u = v / 10;
119 v = v % 10;
120 if (!idx) {
121 // 99999 or less: use "12345" format
122 // u is value/10, v is last digit
123 c = buf[0] = " 123456789"[u/1000];
124 if (c != ' ') fmt = "0123456789";
125 c = buf[1] = fmt[u/100%10];
126 if (c != ' ') fmt = "0123456789";
127 c = buf[2] = fmt[u/10%10];
128 if (c != ' ') fmt = "0123456789";
129 buf[3] = fmt[u%10];
130 buf[4] = "0123456789"[v];
131 } else {
132 // value has been scaled into 0..9999.9 range
133 // u is value, v is 1/10ths (allows for 92.1M format)
134 if (u >= 100) {
135 // value is >= 100: use "1234M', " 123M" formats
136 c = buf[0] = " 123456789"[u/1000];
137 if (c != ' ') fmt = "0123456789";
138 c = buf[1] = fmt[u/100%10];
139 if (c != ' ') fmt = "0123456789";
140 v = u % 10;
141 u = u / 10;
142 buf[2] = fmt[u%10];
143 } else {
144 // value is < 100: use "92.1M" format
145 c = buf[0] = " 123456789"[u/10];
146 if (c != ' ') fmt = "0123456789";
147 buf[1] = fmt[u%10];
148 buf[2] = '.';
149 }
150 buf[3] = "0123456789"[v];
151 buf[4] = scale[idx]; /* typically scale = " kmgt..." */
152 }
153}
154
155
Denis Vlasenko450196c2007-03-28 21:57:12 +0000156// Convert unsigned integer to ascii, writing into supplied buffer.
157// A truncated result contains the first few digits of the result ala strncpy.
158// Returns a pointer past last generated digit, does _not_ store NUL.
Denis Vlasenkoaae03112006-11-05 00:44:39 +0000159void BUG_sizeof_unsigned_not_4(void);
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000160char* FAST_FUNC utoa_to_buf(unsigned n, char *buf, unsigned buflen)
Rob Landley5b88a382006-07-10 07:41:34 +0000161{
Denis Vlasenkoaae03112006-11-05 00:44:39 +0000162 unsigned i, out, res;
163 if (sizeof(unsigned) != 4)
164 BUG_sizeof_unsigned_not_4();
Rob Landley22d39582006-07-11 00:44:36 +0000165 if (buflen) {
Denis Vlasenkoaae03112006-11-05 00:44:39 +0000166 out = 0;
167 for (i = 1000000000; i; i /= 10) {
168 res = n / i;
169 if (res || out || i == 1) {
170 if (!--buflen) break;
Rob Landley22d39582006-07-11 00:44:36 +0000171 out++;
172 n -= res*i;
173 *buf++ = '0' + res;
174 }
Rob Landley5b88a382006-07-10 07:41:34 +0000175 }
176 }
Denis Vlasenko10457b92007-03-27 22:01:31 +0000177 return buf;
Rob Landley5b88a382006-07-10 07:41:34 +0000178}
179
Denis Vlasenkob12b1c82008-04-09 00:33:23 +0000180/* Convert signed integer to ascii, like utoa_to_buf() */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000181char* FAST_FUNC itoa_to_buf(int n, char *buf, unsigned buflen)
Rob Landley5b88a382006-07-10 07:41:34 +0000182{
Denis Vlasenko6c106572008-04-19 19:05:12 +0000183 if (buflen && n < 0) {
Rob Landley5b88a382006-07-10 07:41:34 +0000184 n = -n;
185 *buf++ = '-';
Rob Landley22d39582006-07-11 00:44:36 +0000186 buflen--;
Rob Landley5b88a382006-07-10 07:41:34 +0000187 }
Denis Vlasenko10457b92007-03-27 22:01:31 +0000188 return utoa_to_buf((unsigned)n, buf, buflen);
Rob Landley5b88a382006-07-10 07:41:34 +0000189}
190
Rob Landleyda9d1d02006-09-14 19:52:07 +0000191// The following two functions use a static buffer, so calling either one a
192// second time will overwrite previous results.
193//
194// The largest 32 bit integer is -2 billion plus null terminator, or 12 bytes.
Denis Vlasenkob12b1c82008-04-09 00:33:23 +0000195// It so happens that sizeof(int) * 3 is enough for 32+ bits.
196// (sizeof(int) * 3 + 2 is correct for any width, even 8-bit)
Rob Landleyda9d1d02006-09-14 19:52:07 +0000197
Denis Vlasenkob12b1c82008-04-09 00:33:23 +0000198static char local_buf[sizeof(int) * 3];
Rob Landley23b61be2006-08-04 20:20:03 +0000199
Rob Landleyda9d1d02006-09-14 19:52:07 +0000200// Convert unsigned integer to ascii using a static buffer (returned).
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000201char* FAST_FUNC utoa(unsigned n)
Rob Landley23b61be2006-08-04 20:20:03 +0000202{
Denis Vlasenko10457b92007-03-27 22:01:31 +0000203 *(utoa_to_buf(n, local_buf, sizeof(local_buf))) = '\0';
Rob Landley23b61be2006-08-04 20:20:03 +0000204
205 return local_buf;
206}
207
Denis Vlasenkob12b1c82008-04-09 00:33:23 +0000208/* Convert signed integer to ascii using a static buffer (returned). */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000209char* FAST_FUNC itoa(int n)
Rob Landley5b88a382006-07-10 07:41:34 +0000210{
Denis Vlasenko10457b92007-03-27 22:01:31 +0000211 *(itoa_to_buf(n, local_buf, sizeof(local_buf))) = '\0';
Rob Landley5b88a382006-07-10 07:41:34 +0000212
213 return local_buf;
214}
Rob Landleydf822f22006-07-15 23:00:46 +0000215
Denis Vlasenkob12b1c82008-04-09 00:33:23 +0000216/* Emit a string of hex representation of bytes */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000217char* FAST_FUNC bin2hex(char *p, const char *cp, int count)
Denis Vlasenko3a34d0c2007-01-12 22:10:34 +0000218{
219 while (count) {
220 unsigned char c = *cp++;
221 /* put lowercase hex digits */
Denis Vlasenko98c0bba2007-01-26 23:31:05 +0000222 *p++ = 0x20 | bb_hexdigits_upcase[c >> 4];
223 *p++ = 0x20 | bb_hexdigits_upcase[c & 0xf];
Denis Vlasenko3a34d0c2007-01-12 22:10:34 +0000224 count--;
225 }
226 return p;
227}
228
Denis Vlasenkob12b1c82008-04-09 00:33:23 +0000229/* Return how long the file at fd is, if there's any way to determine it. */
Denis Vlasenkob5c60fc2008-01-27 23:41:34 +0000230#ifdef UNUSED
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000231off_t FAST_FUNC fdlength(int fd)
Rob Landley53437472006-07-16 08:14:35 +0000232{
233 off_t bottom = 0, top = 0, pos;
234 long size;
235
Rob Landleyda9d1d02006-09-14 19:52:07 +0000236 // If the ioctl works for this, return it.
Rob Landley53437472006-07-16 08:14:35 +0000237
238 if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512;
239
Denis Vlasenko621204b2006-10-27 09:03:24 +0000240 // FIXME: explain why lseek(SEEK_END) is not used here!
241
Rob Landleyda9d1d02006-09-14 19:52:07 +0000242 // If not, do a binary search for the last location we can read. (Some
243 // block devices don't do BLKGETSIZE right.)
Rob Landley53437472006-07-16 08:14:35 +0000244
245 do {
246 char temp;
247
Denis Vlasenkod25a2642006-09-05 09:36:19 +0000248 pos = bottom + (top - bottom) / 2;
Rob Landley53437472006-07-16 08:14:35 +0000249
Rob Landleyda9d1d02006-09-14 19:52:07 +0000250 // If we can read from the current location, it's bigger.
Rob Landley53437472006-07-16 08:14:35 +0000251
Denis Vlasenkoea620772006-10-14 02:23:43 +0000252 if (lseek(fd, pos, SEEK_SET)>=0 && safe_read(fd, &temp, 1)==1) {
Rob Landley53437472006-07-16 08:14:35 +0000253 if (bottom == top) bottom = top = (top+1) * 2;
254 else bottom = pos;
255
Rob Landleyda9d1d02006-09-14 19:52:07 +0000256 // If we can't, it's smaller.
Rob Landley53437472006-07-16 08:14:35 +0000257
258 } else {
259 if (bottom == top) {
260 if (!top) return 0;
261 bottom = top/2;
262 }
263 else top = pos;
264 }
265 } while (bottom + 1 != top);
266
267 return pos + 1;
268}
Denis Vlasenkob5c60fc2008-01-27 23:41:34 +0000269#endif
Rob Landleyd921b2e2006-08-03 15:41:12 +0000270
Denis Vlasenko4e12b1a2008-12-23 23:36:47 +0000271char* FAST_FUNC xmalloc_ttyname(int fd)
272{
273 char *buf = xzalloc(128);
274 int r = ttyname_r(fd, buf, 127);
275 if (r) {
276 free(buf);
277 buf = NULL;
278 }
279 return buf;
280}
281
Rob Landleyfbdf1212006-09-20 22:06:01 +0000282/* It is perfectly ok to pass in a NULL for either width or for
Denis Vlasenko621204b2006-10-27 09:03:24 +0000283 * height, in which case that value will not be set. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000284int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *height)
Rob Landleyfbdf1212006-09-20 22:06:01 +0000285{
286 struct winsize win = { 0, 0, 0, 0 };
287 int ret = ioctl(fd, TIOCGWINSZ, &win);
Denis Vlasenko621204b2006-10-27 09:03:24 +0000288
289 if (height) {
290 if (!win.ws_row) {
291 char *s = getenv("LINES");
292 if (s) win.ws_row = atoi(s);
293 }
294 if (win.ws_row <= 1 || win.ws_row >= 30000)
295 win.ws_row = 24;
296 *height = (int) win.ws_row;
Rob Landleyfbdf1212006-09-20 22:06:01 +0000297 }
Denis Vlasenko621204b2006-10-27 09:03:24 +0000298
299 if (width) {
300 if (!win.ws_col) {
301 char *s = getenv("COLUMNS");
302 if (s) win.ws_col = atoi(s);
303 }
304 if (win.ws_col <= 1 || win.ws_col >= 30000)
305 win.ws_col = 80;
306 *width = (int) win.ws_col;
Rob Landleyfbdf1212006-09-20 22:06:01 +0000307 }
Rob Landleyfbdf1212006-09-20 22:06:01 +0000308
309 return ret;
310}
Denis Vlasenko202ac502008-11-05 13:20:58 +0000311
312int FAST_FUNC tcsetattr_stdin_TCSANOW(const struct termios *tp)
313{
314 return tcsetattr(STDIN_FILENO, TCSANOW, tp);
315}