1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include "libbb.h"
26
27
28int FAST_FUNC ndelay_on(int fd)
29{
30 return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
31}
32
33int FAST_FUNC ndelay_off(int fd)
34{
35 return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK);
36}
37
38int FAST_FUNC close_on_exec_on(int fd)
39{
40 return fcntl(fd, F_SETFD, FD_CLOEXEC);
41}
42
43char* 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
51
52
53
54
55
56void BUG_sizeof(void);
57char* FAST_FUNC utoa_to_buf(unsigned n, char *buf, unsigned buflen)
58{
59 unsigned i, out, res;
60
61 if (buflen) {
62 out = 0;
63 if (sizeof(n) == 4)
64
65 i = 1000000000;
66#if UINT_MAX > 4294967295
67 else
68 if (sizeof(n) == 8)
69
70 i = 10000000000000000000;
71#endif
72 else
73 BUG_sizeof();
74 for (; i; i /= 10) {
75 res = n / i;
76 n = n % i;
77 if (res || out || i == 1) {
78 if (--buflen == 0)
79 break;
80 out++;
81 *buf++ = '0' + res;
82 }
83 }
84 }
85 return buf;
86}
87
88
89char* FAST_FUNC itoa_to_buf(int n, char *buf, unsigned buflen)
90{
91 if (!buflen)
92 return buf;
93 if (n < 0) {
94 n = -n;
95 *buf++ = '-';
96 buflen--;
97 }
98 return utoa_to_buf((unsigned)n, buf, buflen);
99}
100
101
102
103
104
105
106
107
108static char local_buf[sizeof(int) * 3];
109
110
111char* FAST_FUNC utoa(unsigned n)
112{
113 *(utoa_to_buf(n, local_buf, sizeof(local_buf) - 1)) = '\0';
114
115 return local_buf;
116}
117
118
119char* FAST_FUNC itoa(int n)
120{
121 *(itoa_to_buf(n, local_buf, sizeof(local_buf) - 1)) = '\0';
122
123 return local_buf;
124}
125
126
127char* FAST_FUNC bin2hex(char *p, const char *cp, int count)
128{
129 while (count) {
130 unsigned char c = *cp++;
131
132 *p++ = 0x20 | bb_hexdigits_upcase[c >> 4];
133 *p++ = 0x20 | bb_hexdigits_upcase[c & 0xf];
134 count--;
135 }
136 return p;
137}
138
139
140char* FAST_FUNC hex2bin(char *dst, const char *str, int count)
141{
142 errno = EINVAL;
143 while (*str && count) {
144 uint8_t val;
145 uint8_t c = *str++;
146 if (isdigit(c))
147 val = c - '0';
148 else if ((c|0x20) >= 'a' && (c|0x20) <= 'f')
149 val = (c|0x20) - ('a' - 10);
150 else
151 return NULL;
152 val <<= 4;
153 c = *str;
154 if (isdigit(c))
155 val |= c - '0';
156 else if ((c|0x20) >= 'a' && (c|0x20) <= 'f')
157 val |= (c|0x20) - ('a' - 10);
158 else if (c == ':' || c == '\0')
159 val >>= 4;
160 else
161 return NULL;
162
163 *dst++ = val;
164 if (c != '\0')
165 str++;
166 if (*str == ':')
167 str++;
168 count--;
169 }
170 errno = (*str ? ERANGE : 0);
171 return dst;
172}
173
174
175#ifdef UNUSED
176off_t FAST_FUNC fdlength(int fd)
177{
178 off_t bottom = 0, top = 0, pos;
179 long size;
180
181
182
183 if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512;
184
185
186
187
188
189
190 do {
191 char temp;
192
193 pos = bottom + (top - bottom) / 2;
194
195
196
197 if (lseek(fd, pos, SEEK_SET)>=0 && safe_read(fd, &temp, 1)==1) {
198 if (bottom == top) bottom = top = (top+1) * 2;
199 else bottom = pos;
200
201
202
203 } else {
204 if (bottom == top) {
205 if (!top) return 0;
206 bottom = top/2;
207 }
208 else top = pos;
209 }
210 } while (bottom + 1 != top);
211
212 return pos + 1;
213}
214#endif
215
216int FAST_FUNC bb_putchar_stderr(char ch)
217{
218 return write(STDERR_FILENO, &ch, 1);
219}
220
221ssize_t FAST_FUNC full_write1_str(const char *str)
222{
223 return full_write(STDOUT_FILENO, str, strlen(str));
224}
225
226ssize_t FAST_FUNC full_write2_str(const char *str)
227{
228 return full_write(STDERR_FILENO, str, strlen(str));
229}
230
231static int wh_helper(int value, int def_val, const char *env_name, int *err)
232{
233 if (value == 0) {
234 char *s = getenv(env_name);
235 if (s) {
236 value = atoi(s);
237
238
239
240 *err = 0;
241 }
242 }
243 if (value <= 1 || value >= 30000)
244 value = def_val;
245 return value;
246}
247
248
249
250int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *height)
251{
252 struct winsize win;
253 int err;
254
255 win.ws_row = 0;
256 win.ws_col = 0;
257
258
259 err = ioctl(fd, TIOCGWINSZ, &win) != 0 || win.ws_row == 0;
260 if (height)
261 *height = wh_helper(win.ws_row, 24, "LINES", &err);
262 if (width)
263 *width = wh_helper(win.ws_col, 80, "COLUMNS", &err);
264 return err;
265}
266
267int FAST_FUNC tcsetattr_stdin_TCSANOW(const struct termios *tp)
268{
269 return tcsetattr(STDIN_FILENO, TCSANOW, tp);
270}
271
272pid_t FAST_FUNC safe_waitpid(pid_t pid, int *wstat, int options)
273{
274 pid_t r;
275
276 do
277 r = waitpid(pid, wstat, options);
278 while ((r == -1) && (errno == EINTR));
279 return r;
280}
281
282pid_t FAST_FUNC wait_any_nohang(int *wstat)
283{
284 return safe_waitpid(-1, wstat, WNOHANG);
285}
286
287
288int FAST_FUNC wait4pid(pid_t pid)
289{
290 int status;
291
292 if (pid <= 0) {
293
294
295 return -1;
296 }
297 if (safe_waitpid(pid, &status, 0) == -1)
298 return -1;
299 if (WIFEXITED(status))
300 return WEXITSTATUS(status);
301 if (WIFSIGNALED(status))
302 return WTERMSIG(status) + 0x180;
303 return 0;
304}
305