1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 * Copyright (C) 2006 Rob Landley
7 * Copyright (C) 2006 Denys Vlasenko
9 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
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.
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.
22 * TODO: move xmalloc() and xatonum() here.
27 /* Turn on nonblocking I/O on a fd */
28 int FAST_FUNC ndelay_on(int fd)
30 return fcntl(fd, F_SETFL, fcntl(fd,F_GETFL) | O_NONBLOCK);
33 int FAST_FUNC ndelay_off(int fd)
35 return fcntl(fd, F_SETFL, fcntl(fd,F_GETFL) & ~O_NONBLOCK);
38 int FAST_FUNC close_on_exec_on(int fd)
40 return fcntl(fd, F_SETFD, FD_CLOEXEC);
43 char* FAST_FUNC strncpy_IFNAMSIZ(char *dst, const char *src)
46 enum { IFNAMSIZ = 16 };
48 return strncpy(dst, src, IFNAMSIZ);
52 // 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.
55 void BUG_sizeof_unsigned_not_4(void);
56 char* FAST_FUNC utoa_to_buf(unsigned n, char *buf, unsigned buflen)
59 if (sizeof(unsigned) != 4)
60 BUG_sizeof_unsigned_not_4();
63 for (i = 1000000000; i; i /= 10) {
65 if (res || out || i == 1) {
76 /* Convert signed integer to ascii, like utoa_to_buf() */
77 char* FAST_FUNC itoa_to_buf(int n, char *buf, unsigned buflen)
79 if (buflen && n < 0) {
84 return utoa_to_buf((unsigned)n, buf, buflen);
87 // The following two functions use a static buffer, so calling either one a
88 // second time will overwrite previous results.
90 // The largest 32 bit integer is -2 billion plus null terminator, or 12 bytes.
91 // 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)
94 static char local_buf[sizeof(int) * 3];
96 // Convert unsigned integer to ascii using a static buffer (returned).
97 char* FAST_FUNC utoa(unsigned n)
99 *(utoa_to_buf(n, local_buf, sizeof(local_buf))) = '\0';
104 /* Convert signed integer to ascii using a static buffer (returned). */
105 char* FAST_FUNC itoa(int n)
107 *(itoa_to_buf(n, local_buf, sizeof(local_buf))) = '\0';
112 /* Emit a string of hex representation of bytes */
113 char* FAST_FUNC bin2hex(char *p, const char *cp, int count)
116 unsigned char c = *cp++;
117 /* put lowercase hex digits */
118 *p++ = 0x20 | bb_hexdigits_upcase[c >> 4];
119 *p++ = 0x20 | bb_hexdigits_upcase[c & 0xf];
125 /* Return how long the file at fd is, if there's any way to determine it. */
127 off_t FAST_FUNC fdlength(int fd)
129 off_t bottom = 0, top = 0, pos;
132 // If the ioctl works for this, return it.
134 if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512;
136 // FIXME: explain why lseek(SEEK_END) is not used here!
138 // If not, do a binary search for the last location we can read. (Some
139 // block devices don't do BLKGETSIZE right.)
144 pos = bottom + (top - bottom) / 2;
146 // If we can read from the current location, it's bigger.
148 if (lseek(fd, pos, SEEK_SET)>=0 && safe_read(fd, &temp, 1)==1) {
149 if (bottom == top) bottom = top = (top+1) * 2;
152 // If we can't, it's smaller.
161 } while (bottom + 1 != top);
167 char* FAST_FUNC xmalloc_ttyname(int fd)
169 char *buf = xzalloc(128);
170 int r = ttyname_r(fd, buf, 127);
178 /* It is perfectly ok to pass in a NULL for either width or for
179 * height, in which case that value will not be set. */
180 int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *height)
182 struct winsize win = { 0, 0, 0, 0 };
183 int ret = ioctl(fd, TIOCGWINSZ, &win);
187 char *s = getenv("LINES");
188 if (s) win.ws_row = atoi(s);
190 if (win.ws_row <= 1 || win.ws_row >= 30000)
192 *height = (int) win.ws_row;
197 char *s = getenv("COLUMNS");
198 if (s) win.ws_col = atoi(s);
200 if (win.ws_col <= 1 || win.ws_col >= 30000)
202 *width = (int) win.ws_col;
208 int FAST_FUNC tcsetattr_stdin_TCSANOW(const struct termios *tp)
210 return tcsetattr(STDIN_FILENO, TCSANOW, tp);