blob: fe3c647d05acc4b8071e6b30921ba47358ee8331 [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 Vlasenko75f8d082006-11-22 15:54:52 +000028int ndelay_on(int fd)
29{
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 Vlasenkoa5b3e762006-12-24 07:15:50 +000033int ndelay_off(int fd)
34{
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 Vlasenkob12b1c82008-04-09 00:33:23 +000038int 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 Vlasenkob12b1c82008-04-09 00:33:23 +000043/* Convert unsigned long long value into compact 4-char
Denis Vlasenko56ea65c2008-01-06 03:26:53 +000044 * representation. Examples: "1234", "1.2k", " 27M", "123T"
45 * String is not terminated (buf[4] is untouched) */
46void smart_ulltoa4(unsigned long long ul, char buf[5], const char *scale)
Denis Vlasenkoaae03112006-11-05 00:44:39 +000047{
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000048 const char *fmt;
Denis Vlasenkoaae03112006-11-05 00:44:39 +000049 char c;
Denis Vlasenkob308d812007-08-28 19:35:34 +000050 unsigned v, u, idx = 0;
51
52 if (ul > 9999) { // do not scale if 9999 or less
53 ul *= 10;
54 do {
Denis Vlasenkoaae03112006-11-05 00:44:39 +000055 ul /= 1024;
56 idx++;
Denis Vlasenkob308d812007-08-28 19:35:34 +000057 } while (ul >= 10000);
Denis Vlasenkoaae03112006-11-05 00:44:39 +000058 }
59 v = ul; // ullong divisions are expensive, avoid them
60
61 fmt = " 123456789";
Denis Vlasenkob308d812007-08-28 19:35:34 +000062 u = v / 10;
63 v = v % 10;
64 if (!idx) {
65 // 9999 or less: use "1234" format
66 // u is value/10, v is last digit
67 c = buf[0] = " 123456789"[u/100];
Denis Vlasenko1b6fa4c2007-03-24 12:08:36 +000068 if (c != ' ') fmt = "0123456789";
Denis Vlasenkob308d812007-08-28 19:35:34 +000069 c = buf[1] = fmt[u/10%10];
Denis Vlasenko1b6fa4c2007-03-24 12:08:36 +000070 if (c != ' ') fmt = "0123456789";
Denis Vlasenkob308d812007-08-28 19:35:34 +000071 buf[2] = fmt[u%10];
72 buf[3] = "0123456789"[v];
Denis Vlasenkoaae03112006-11-05 00:44:39 +000073 } else {
Denis Vlasenkob308d812007-08-28 19:35:34 +000074 // u is value, v is 1/10ths (allows for 9.2M format)
75 if (u >= 10) {
76 // value is >= 10: use "123M', " 12M" formats
77 c = buf[0] = " 123456789"[u/100];
Denis Vlasenko1b6fa4c2007-03-24 12:08:36 +000078 if (c != ' ') fmt = "0123456789";
Denis Vlasenkob308d812007-08-28 19:35:34 +000079 v = u % 10;
80 u = u / 10;
81 buf[1] = fmt[u%10];
82 } else {
83 // value is < 10: use "9.2M" format
84 buf[0] = "0123456789"[u];
Denis Vlasenkoaae03112006-11-05 00:44:39 +000085 buf[1] = '.';
Denis Vlasenkoaae03112006-11-05 00:44:39 +000086 }
Denis Vlasenkob308d812007-08-28 19:35:34 +000087 buf[2] = "0123456789"[v];
Denis Vlasenko56ea65c2008-01-06 03:26:53 +000088 buf[3] = scale[idx]; /* typically scale = " kmgt..." */
Denis Vlasenkoaae03112006-11-05 00:44:39 +000089 }
Denis Vlasenkoaae03112006-11-05 00:44:39 +000090}
91
Denis Vlasenkob12b1c82008-04-09 00:33:23 +000092/* Convert unsigned long long value into compact 5-char representation.
Denis Vlasenko56ea65c2008-01-06 03:26:53 +000093 * String is not terminated (buf[5] is untouched) */
94void smart_ulltoa5(unsigned long long ul, char buf[6], const char *scale)
95{
96 const char *fmt;
97 char c;
98 unsigned v, u, idx = 0;
99
100 if (ul > 99999) { // do not scale if 99999 or less
101 ul *= 10;
102 do {
103 ul /= 1024;
104 idx++;
105 } while (ul >= 100000);
106 }
107 v = ul; // ullong divisions are expensive, avoid them
108
109 fmt = " 123456789";
110 u = v / 10;
111 v = v % 10;
112 if (!idx) {
113 // 99999 or less: use "12345" format
114 // u is value/10, v is last digit
115 c = buf[0] = " 123456789"[u/1000];
116 if (c != ' ') fmt = "0123456789";
117 c = buf[1] = fmt[u/100%10];
118 if (c != ' ') fmt = "0123456789";
119 c = buf[2] = fmt[u/10%10];
120 if (c != ' ') fmt = "0123456789";
121 buf[3] = fmt[u%10];
122 buf[4] = "0123456789"[v];
123 } else {
124 // value has been scaled into 0..9999.9 range
125 // u is value, v is 1/10ths (allows for 92.1M format)
126 if (u >= 100) {
127 // value is >= 100: use "1234M', " 123M" formats
128 c = buf[0] = " 123456789"[u/1000];
129 if (c != ' ') fmt = "0123456789";
130 c = buf[1] = fmt[u/100%10];
131 if (c != ' ') fmt = "0123456789";
132 v = u % 10;
133 u = u / 10;
134 buf[2] = fmt[u%10];
135 } else {
136 // value is < 100: use "92.1M" format
137 c = buf[0] = " 123456789"[u/10];
138 if (c != ' ') fmt = "0123456789";
139 buf[1] = fmt[u%10];
140 buf[2] = '.';
141 }
142 buf[3] = "0123456789"[v];
143 buf[4] = scale[idx]; /* typically scale = " kmgt..." */
144 }
145}
146
147
Denis Vlasenko450196c2007-03-28 21:57:12 +0000148// Convert unsigned integer to ascii, writing into supplied buffer.
149// A truncated result contains the first few digits of the result ala strncpy.
150// Returns a pointer past last generated digit, does _not_ store NUL.
Denis Vlasenkoaae03112006-11-05 00:44:39 +0000151void BUG_sizeof_unsigned_not_4(void);
Denis Vlasenko10457b92007-03-27 22:01:31 +0000152char *utoa_to_buf(unsigned n, char *buf, unsigned buflen)
Rob Landley5b88a382006-07-10 07:41:34 +0000153{
Denis Vlasenkoaae03112006-11-05 00:44:39 +0000154 unsigned i, out, res;
155 if (sizeof(unsigned) != 4)
156 BUG_sizeof_unsigned_not_4();
Rob Landley22d39582006-07-11 00:44:36 +0000157 if (buflen) {
Denis Vlasenkoaae03112006-11-05 00:44:39 +0000158 out = 0;
159 for (i = 1000000000; i; i /= 10) {
160 res = n / i;
161 if (res || out || i == 1) {
162 if (!--buflen) break;
Rob Landley22d39582006-07-11 00:44:36 +0000163 out++;
164 n -= res*i;
165 *buf++ = '0' + res;
166 }
Rob Landley5b88a382006-07-10 07:41:34 +0000167 }
168 }
Denis Vlasenko10457b92007-03-27 22:01:31 +0000169 return buf;
Rob Landley5b88a382006-07-10 07:41:34 +0000170}
171
Denis Vlasenkob12b1c82008-04-09 00:33:23 +0000172/* Convert signed integer to ascii, like utoa_to_buf() */
Denis Vlasenko10457b92007-03-27 22:01:31 +0000173char *itoa_to_buf(int n, char *buf, unsigned buflen)
Rob Landley5b88a382006-07-10 07:41:34 +0000174{
Denis Vlasenko6c106572008-04-19 19:05:12 +0000175 if (buflen && n < 0) {
Rob Landley5b88a382006-07-10 07:41:34 +0000176 n = -n;
177 *buf++ = '-';
Rob Landley22d39582006-07-11 00:44:36 +0000178 buflen--;
Rob Landley5b88a382006-07-10 07:41:34 +0000179 }
Denis Vlasenko10457b92007-03-27 22:01:31 +0000180 return utoa_to_buf((unsigned)n, buf, buflen);
Rob Landley5b88a382006-07-10 07:41:34 +0000181}
182
Rob Landleyda9d1d02006-09-14 19:52:07 +0000183// The following two functions use a static buffer, so calling either one a
184// second time will overwrite previous results.
185//
186// The largest 32 bit integer is -2 billion plus null terminator, or 12 bytes.
Denis Vlasenkob12b1c82008-04-09 00:33:23 +0000187// It so happens that sizeof(int) * 3 is enough for 32+ bits.
188// (sizeof(int) * 3 + 2 is correct for any width, even 8-bit)
Rob Landleyda9d1d02006-09-14 19:52:07 +0000189
Denis Vlasenkob12b1c82008-04-09 00:33:23 +0000190static char local_buf[sizeof(int) * 3];
Rob Landley23b61be2006-08-04 20:20:03 +0000191
Rob Landleyda9d1d02006-09-14 19:52:07 +0000192// Convert unsigned integer to ascii using a static buffer (returned).
Rob Landley23b61be2006-08-04 20:20:03 +0000193char *utoa(unsigned n)
194{
Denis Vlasenko10457b92007-03-27 22:01:31 +0000195 *(utoa_to_buf(n, local_buf, sizeof(local_buf))) = '\0';
Rob Landley23b61be2006-08-04 20:20:03 +0000196
197 return local_buf;
198}
199
Denis Vlasenkob12b1c82008-04-09 00:33:23 +0000200/* Convert signed integer to ascii using a static buffer (returned). */
Rob Landley5b88a382006-07-10 07:41:34 +0000201char *itoa(int n)
202{
Denis Vlasenko10457b92007-03-27 22:01:31 +0000203 *(itoa_to_buf(n, local_buf, sizeof(local_buf))) = '\0';
Rob Landley5b88a382006-07-10 07:41:34 +0000204
205 return local_buf;
206}
Rob Landleydf822f22006-07-15 23:00:46 +0000207
Denis Vlasenkob12b1c82008-04-09 00:33:23 +0000208/* Emit a string of hex representation of bytes */
Denis Vlasenko3a34d0c2007-01-12 22:10:34 +0000209char *bin2hex(char *p, const char *cp, int count)
210{
211 while (count) {
212 unsigned char c = *cp++;
213 /* put lowercase hex digits */
Denis Vlasenko98c0bba2007-01-26 23:31:05 +0000214 *p++ = 0x20 | bb_hexdigits_upcase[c >> 4];
215 *p++ = 0x20 | bb_hexdigits_upcase[c & 0xf];
Denis Vlasenko3a34d0c2007-01-12 22:10:34 +0000216 count--;
217 }
218 return p;
219}
220
Denis Vlasenkob12b1c82008-04-09 00:33:23 +0000221/* Return how long the file at fd is, if there's any way to determine it. */
Denis Vlasenkob5c60fc2008-01-27 23:41:34 +0000222#ifdef UNUSED
Rob Landley53437472006-07-16 08:14:35 +0000223off_t fdlength(int fd)
224{
225 off_t bottom = 0, top = 0, pos;
226 long size;
227
Rob Landleyda9d1d02006-09-14 19:52:07 +0000228 // If the ioctl works for this, return it.
Rob Landley53437472006-07-16 08:14:35 +0000229
230 if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512;
231
Denis Vlasenko621204b2006-10-27 09:03:24 +0000232 // FIXME: explain why lseek(SEEK_END) is not used here!
233
Rob Landleyda9d1d02006-09-14 19:52:07 +0000234 // If not, do a binary search for the last location we can read. (Some
235 // block devices don't do BLKGETSIZE right.)
Rob Landley53437472006-07-16 08:14:35 +0000236
237 do {
238 char temp;
239
Denis Vlasenkod25a2642006-09-05 09:36:19 +0000240 pos = bottom + (top - bottom) / 2;
Rob Landley53437472006-07-16 08:14:35 +0000241
Rob Landleyda9d1d02006-09-14 19:52:07 +0000242 // If we can read from the current location, it's bigger.
Rob Landley53437472006-07-16 08:14:35 +0000243
Denis Vlasenkoea620772006-10-14 02:23:43 +0000244 if (lseek(fd, pos, SEEK_SET)>=0 && safe_read(fd, &temp, 1)==1) {
Rob Landley53437472006-07-16 08:14:35 +0000245 if (bottom == top) bottom = top = (top+1) * 2;
246 else bottom = pos;
247
Rob Landleyda9d1d02006-09-14 19:52:07 +0000248 // If we can't, it's smaller.
Rob Landley53437472006-07-16 08:14:35 +0000249
250 } else {
251 if (bottom == top) {
252 if (!top) return 0;
253 bottom = top/2;
254 }
255 else top = pos;
256 }
257 } while (bottom + 1 != top);
258
259 return pos + 1;
260}
Denis Vlasenkob5c60fc2008-01-27 23:41:34 +0000261#endif
Rob Landleyd921b2e2006-08-03 15:41:12 +0000262
Rob Landleyfbdf1212006-09-20 22:06:01 +0000263/* It is perfectly ok to pass in a NULL for either width or for
Denis Vlasenko621204b2006-10-27 09:03:24 +0000264 * height, in which case that value will not be set. */
Denis Vlasenko55995022008-05-18 22:28:26 +0000265int get_terminal_width_height(int fd, unsigned *width, unsigned *height)
Rob Landleyfbdf1212006-09-20 22:06:01 +0000266{
267 struct winsize win = { 0, 0, 0, 0 };
268 int ret = ioctl(fd, TIOCGWINSZ, &win);
Denis Vlasenko621204b2006-10-27 09:03:24 +0000269
270 if (height) {
271 if (!win.ws_row) {
272 char *s = getenv("LINES");
273 if (s) win.ws_row = atoi(s);
274 }
275 if (win.ws_row <= 1 || win.ws_row >= 30000)
276 win.ws_row = 24;
277 *height = (int) win.ws_row;
Rob Landleyfbdf1212006-09-20 22:06:01 +0000278 }
Denis Vlasenko621204b2006-10-27 09:03:24 +0000279
280 if (width) {
281 if (!win.ws_col) {
282 char *s = getenv("COLUMNS");
283 if (s) win.ws_col = atoi(s);
284 }
285 if (win.ws_col <= 1 || win.ws_col >= 30000)
286 win.ws_col = 80;
287 *width = (int) win.ws_col;
Rob Landleyfbdf1212006-09-20 22:06:01 +0000288 }
Rob Landleyfbdf1212006-09-20 22:06:01 +0000289
290 return ret;
291}