aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2010-06-06 21:53:09 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2010-06-06 21:53:09 +0200
commit19ced5c4253bc154aa499a72b6343e01245c92c0 (patch)
treec1c148612896e748749ce882ed25ad5ffd74418c /libbb
parent5f3303712ef483d270097cae4ba0a559b1056121 (diff)
downloadbusybox-w32-19ced5c4253bc154aa499a72b6343e01245c92c0.tar.gz
busybox-w32-19ced5c4253bc154aa499a72b6343e01245c92c0.tar.bz2
busybox-w32-19ced5c4253bc154aa499a72b6343e01245c92c0.zip
pipe_progress: make it independent of printf machinery
function old new delta bb_putchar_stderr - 24 +24 ParseField 494 471 -23 progress_meter 212 188 -24 xargs_main 888 842 -46 pipe_progress_main 151 105 -46 ------------------------------------------------------------------------------ (add/remove: 2/0 grow/shrink: 0/4 up/down: 24/-139) Total: -115 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb')
-rw-r--r--libbb/Kbuild.src1
-rw-r--r--libbb/read.c365
-rw-r--r--libbb/read_printf.c374
-rw-r--r--libbb/xfuncs.c73
-rw-r--r--libbb/xfuncs_printf.c74
5 files changed, 451 insertions, 436 deletions
diff --git a/libbb/Kbuild.src b/libbb/Kbuild.src
index 1b11d5d39..68d04b3bd 100644
--- a/libbb/Kbuild.src
+++ b/libbb/Kbuild.src
@@ -81,6 +81,7 @@ lib-y += procps.o
81lib-y += progress.o 81lib-y += progress.o
82lib-y += ptr_to_globals.o 82lib-y += ptr_to_globals.o
83lib-y += read.o 83lib-y += read.o
84lib-y += read_printf.o
84lib-y += read_key.o 85lib-y += read_key.o
85lib-y += recursive_action.o 86lib-y += recursive_action.o
86lib-y += remove_file.o 87lib-y += remove_file.o
diff --git a/libbb/read.c b/libbb/read.c
index b1eb3f24b..1ed7c5f92 100644
--- a/libbb/read.c
+++ b/libbb/read.c
@@ -8,16 +8,6 @@
8 */ 8 */
9#include "libbb.h" 9#include "libbb.h"
10 10
11#define ZIPPED (ENABLE_FEATURE_SEAMLESS_LZMA \
12 || ENABLE_FEATURE_SEAMLESS_BZ2 \
13 || ENABLE_FEATURE_SEAMLESS_GZ \
14 /* || ENABLE_FEATURE_SEAMLESS_Z */ \
15)
16
17#if ZIPPED
18# include "unarchive.h"
19#endif
20
21ssize_t FAST_FUNC safe_read(int fd, void *buf, size_t count) 11ssize_t FAST_FUNC safe_read(int fd, void *buf, size_t count)
22{ 12{
23 ssize_t n; 13 ssize_t n;
@@ -29,58 +19,6 @@ ssize_t FAST_FUNC safe_read(int fd, void *buf, size_t count)
29 return n; 19 return n;
30} 20}
31 21
32/* Suppose that you are a shell. You start child processes.
33 * They work and eventually exit. You want to get user input.
34 * You read stdin. But what happens if last child switched
35 * its stdin into O_NONBLOCK mode?
36 *
37 * *** SURPRISE! It will affect the parent too! ***
38 * *** BIG SURPRISE! It stays even after child exits! ***
39 *
40 * This is a design bug in UNIX API.
41 * fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
42 * will set nonblocking mode not only on _your_ stdin, but
43 * also on stdin of your parent, etc.
44 *
45 * In general,
46 * fd2 = dup(fd1);
47 * fcntl(fd2, F_SETFL, fcntl(fd2, F_GETFL) | O_NONBLOCK);
48 * sets both fd1 and fd2 to O_NONBLOCK. This includes cases
49 * where duping is done implicitly by fork() etc.
50 *
51 * We need
52 * fcntl(fd2, F_SETFD, fcntl(fd2, F_GETFD) | O_NONBLOCK);
53 * (note SETFD, not SETFL!) but such thing doesn't exist.
54 *
55 * Alternatively, we need nonblocking_read(fd, ...) which doesn't
56 * require O_NONBLOCK dance at all. Actually, it exists:
57 * n = recv(fd, buf, len, MSG_DONTWAIT);
58 * "MSG_DONTWAIT:
59 * Enables non-blocking operation; if the operation
60 * would block, EAGAIN is returned."
61 * but recv() works only for sockets!
62 *
63 * So far I don't see any good solution, I can only propose
64 * that affected readers should be careful and use this routine,
65 * which detects EAGAIN and uses poll() to wait on the fd.
66 * Thankfully, poll() doesn't care about O_NONBLOCK flag.
67 */
68ssize_t FAST_FUNC nonblock_safe_read(int fd, void *buf, size_t count)
69{
70 struct pollfd pfd[1];
71 ssize_t n;
72
73 while (1) {
74 n = safe_read(fd, buf, count);
75 if (n >= 0 || errno != EAGAIN)
76 return n;
77 /* fd is in O_NONBLOCK mode. Wait using poll and repeat */
78 pfd[0].fd = fd;
79 pfd[0].events = POLLIN;
80 safe_poll(pfd, 1, -1);
81 }
82}
83
84/* 22/*
85 * Read all of the supplied buffer from a file. 23 * Read all of the supplied buffer from a file.
86 * This does multiple reads as necessary. 24 * This does multiple reads as necessary.
@@ -115,60 +53,6 @@ ssize_t FAST_FUNC full_read(int fd, void *buf, size_t len)
115 return total; 53 return total;
116} 54}
117 55
118/* Die with an error message if we can't read the entire buffer. */
119void FAST_FUNC xread(int fd, void *buf, size_t count)
120{
121 if (count) {
122 ssize_t size = full_read(fd, buf, count);
123 if ((size_t)size != count)
124 bb_error_msg_and_die("short read");
125 }
126}
127
128/* Die with an error message if we can't read one character. */
129unsigned char FAST_FUNC xread_char(int fd)
130{
131 char tmp;
132 xread(fd, &tmp, 1);
133 return tmp;
134}
135
136// Reads one line a-la fgets (but doesn't save terminating '\n').
137// Reads byte-by-byte. Useful when it is important to not read ahead.
138// Bytes are appended to pfx (which must be malloced, or NULL).
139char* FAST_FUNC xmalloc_reads(int fd, char *buf, size_t *maxsz_p)
140{
141 char *p;
142 size_t sz = buf ? strlen(buf) : 0;
143 size_t maxsz = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
144
145 goto jump_in;
146 while (sz < maxsz) {
147 if ((size_t)(p - buf) == sz) {
148 jump_in:
149 buf = xrealloc(buf, sz + 128);
150 p = buf + sz;
151 sz += 128;
152 }
153 /* nonblock_safe_read() because we are used by e.g. shells */
154 if (nonblock_safe_read(fd, p, 1) != 1) { /* EOF/error */
155 if (p == buf) { /* we read nothing */
156 free(buf);
157 return NULL;
158 }
159 break;
160 }
161 if (*p == '\n')
162 break;
163 p++;
164 }
165 *p = '\0';
166 if (maxsz_p)
167 *maxsz_p = p - buf;
168 p++;
169 return xrealloc(buf, p - buf);
170}
171
172ssize_t FAST_FUNC read_close(int fd, void *buf, size_t size) 56ssize_t FAST_FUNC read_close(int fd, void *buf, size_t size)
173{ 57{
174 /*int e;*/ 58 /*int e;*/
@@ -186,252 +70,3 @@ ssize_t FAST_FUNC open_read_close(const char *filename, void *buf, size_t size)
186 return fd; 70 return fd;
187 return read_close(fd, buf, size); 71 return read_close(fd, buf, size);
188} 72}
189
190
191// Read (potentially big) files in one go. File size is estimated
192// by stat. Extra '\0' byte is appended.
193void* FAST_FUNC xmalloc_read(int fd, size_t *maxsz_p)
194{
195 char *buf;
196 size_t size, rd_size, total;
197 size_t to_read;
198 struct stat st;
199
200 to_read = maxsz_p ? *maxsz_p : (INT_MAX - 4095); /* max to read */
201
202 /* Estimate file size */
203 st.st_size = 0; /* in case fstat fails, assume 0 */
204 fstat(fd, &st);
205 /* /proc/N/stat files report st_size 0 */
206 /* In order to make such files readable, we add small const */
207 size = (st.st_size | 0x3ff) + 1;
208
209 total = 0;
210 buf = NULL;
211 while (1) {
212 if (to_read < size)
213 size = to_read;
214 buf = xrealloc(buf, total + size + 1);
215 rd_size = full_read(fd, buf + total, size);
216 if ((ssize_t)rd_size == (ssize_t)(-1)) { /* error */
217 free(buf);
218 return NULL;
219 }
220 total += rd_size;
221 if (rd_size < size) /* EOF */
222 break;
223 if (to_read <= rd_size)
224 break;
225 to_read -= rd_size;
226 /* grow by 1/8, but in [1k..64k] bounds */
227 size = ((total / 8) | 0x3ff) + 1;
228 if (size > 64*1024)
229 size = 64*1024;
230 }
231 buf = xrealloc(buf, total + 1);
232 buf[total] = '\0';
233
234 if (maxsz_p)
235 *maxsz_p = total;
236 return buf;
237}
238
239#ifdef USING_LSEEK_TO_GET_SIZE
240/* Alternatively, file size can be obtained by lseek to the end.
241 * The code is slightly bigger. Retained in case fstat approach
242 * will not work for some weird cases (/proc, block devices, etc).
243 * (NB: lseek also can fail to work for some weird files) */
244
245// Read (potentially big) files in one go. File size is estimated by
246// lseek to end.
247void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
248{
249 char *buf;
250 size_t size;
251 int fd;
252 off_t len;
253
254 fd = open(filename, O_RDONLY);
255 if (fd < 0)
256 return NULL;
257
258 /* /proc/N/stat files report len 0 here */
259 /* In order to make such files readable, we add small const */
260 size = 0x3ff; /* read only 1k on unseekable files */
261 len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */
262 if (len != (off_t)-1) {
263 xlseek(fd, 0, SEEK_SET);
264 size = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
265 if (len < size)
266 size = len;
267 }
268
269 buf = xmalloc(size + 1);
270 size = read_close(fd, buf, size);
271 if ((ssize_t)size < 0) {
272 free(buf);
273 return NULL;
274 }
275 buf = xrealloc(buf, size + 1);
276 buf[size] = '\0';
277
278 if (maxsz_p)
279 *maxsz_p = size;
280 return buf;
281}
282#endif
283
284// Read (potentially big) files in one go. File size is estimated
285// by stat.
286void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
287{
288 char *buf;
289 int fd;
290
291 fd = open(filename, O_RDONLY);
292 if (fd < 0)
293 return NULL;
294
295 buf = xmalloc_read(fd, maxsz_p);
296 close(fd);
297 return buf;
298}
299
300void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p)
301{
302 void *buf = xmalloc_open_read_close(filename, maxsz_p);
303 if (!buf)
304 bb_perror_msg_and_die("can't read '%s'", filename);
305 return buf;
306}
307
308/* Used by e.g. rpm which gives us a fd without filename,
309 * thus we can't guess the format from filename's extension.
310 */
311#if ZIPPED
312void FAST_FUNC setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/)
313{
314 const int fail_if_not_detected = 1;
315 union {
316 uint8_t b[4];
317 uint16_t b16[2];
318 uint32_t b32[1];
319 } magic;
320 int offset = -2;
321# if BB_MMU
322 IF_DESKTOP(long long) int FAST_FUNC (*xformer)(int src_fd, int dst_fd);
323 enum { xformer_prog = 0 };
324# else
325 enum { xformer = 0 };
326 const char *xformer_prog;
327# endif
328
329 /* .gz and .bz2 both have 2-byte signature, and their
330 * unpack_XXX_stream wants this header skipped. */
331 xread(fd, magic.b16, sizeof(magic.b16));
332 if (ENABLE_FEATURE_SEAMLESS_GZ
333 && magic.b16[0] == GZIP_MAGIC
334 ) {
335# if BB_MMU
336 xformer = unpack_gz_stream;
337# else
338 xformer_prog = "gunzip";
339# endif
340 goto found_magic;
341 }
342 if (ENABLE_FEATURE_SEAMLESS_BZ2
343 && magic.b16[0] == BZIP2_MAGIC
344 ) {
345# if BB_MMU
346 xformer = unpack_bz2_stream;
347# else
348 xformer_prog = "bunzip2";
349# endif
350 goto found_magic;
351 }
352 if (ENABLE_FEATURE_SEAMLESS_XZ
353 && magic.b16[0] == XZ_MAGIC1
354 ) {
355 /* .xz signature: 0xfd, '7', 'z', 'X', 'Z', 0x00 */
356 /* More info at: http://tukaani.org/xz/xz-file-format.txt */
357 offset = -6;
358 xread(fd, magic.b32, sizeof(magic.b32));
359 if (magic.b32[0] == XZ_MAGIC2) {
360# if BB_MMU
361 xformer = unpack_xz_stream;
362 /* unpack_xz_stream wants fd at position 0 */
363 xlseek(fd, offset, SEEK_CUR);
364# else
365 xformer_prog = "unxz";
366# endif
367 goto found_magic;
368 }
369 }
370
371 /* No known magic seen */
372 if (fail_if_not_detected)
373 bb_error_msg_and_die("no gzip"
374 IF_FEATURE_SEAMLESS_BZ2("/bzip2")
375 IF_FEATURE_SEAMLESS_XZ("/xz")
376 " magic");
377 xlseek(fd, offset, SEEK_CUR);
378 return;
379
380 found_magic:
381# if !BB_MMU
382 /* NOMMU version of open_transformer execs
383 * an external unzipper that wants
384 * file position at the start of the file */
385 xlseek(fd, offset, SEEK_CUR);
386# endif
387 open_transformer(fd, xformer, xformer_prog);
388}
389#endif /* ZIPPED */
390
391int FAST_FUNC open_zipped(const char *fname)
392{
393#if !ZIPPED
394 return open(fname, O_RDONLY);
395#else
396 char *sfx;
397 int fd;
398
399 fd = open(fname, O_RDONLY);
400 if (fd < 0)
401 return fd;
402
403 sfx = strrchr(fname, '.');
404 if (sfx) {
405 sfx++;
406 if (ENABLE_FEATURE_SEAMLESS_LZMA && strcmp(sfx, "lzma") == 0)
407 /* .lzma has no header/signature, just trust it */
408 open_transformer(fd, unpack_lzma_stream, "unlzma");
409 else
410 if ((ENABLE_FEATURE_SEAMLESS_GZ && strcmp(sfx, "gz") == 0)
411 || (ENABLE_FEATURE_SEAMLESS_BZ2 && strcmp(sfx, "bz2") == 0)
412 || (ENABLE_FEATURE_SEAMLESS_XZ && strcmp(sfx, "xz") == 0)
413 ) {
414 setup_unzip_on_fd(fd /*, fail_if_not_detected: 1*/);
415 }
416 }
417
418 return fd;
419#endif
420}
421
422void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p)
423{
424 int fd;
425 char *image;
426
427 fd = open_zipped(fname);
428 if (fd < 0)
429 return NULL;
430
431 image = xmalloc_read(fd, maxsz_p);
432 if (!image)
433 bb_perror_msg("read error from '%s'", fname);
434 close(fd);
435
436 return image;
437}
diff --git a/libbb/read_printf.c b/libbb/read_printf.c
new file mode 100644
index 000000000..53f528f5a
--- /dev/null
+++ b/libbb/read_printf.c
@@ -0,0 +1,374 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */
9#include "libbb.h"
10
11#define ZIPPED (ENABLE_FEATURE_SEAMLESS_LZMA \
12 || ENABLE_FEATURE_SEAMLESS_BZ2 \
13 || ENABLE_FEATURE_SEAMLESS_GZ \
14 /* || ENABLE_FEATURE_SEAMLESS_Z */ \
15)
16
17#if ZIPPED
18# include "unarchive.h"
19#endif
20
21
22/* Suppose that you are a shell. You start child processes.
23 * They work and eventually exit. You want to get user input.
24 * You read stdin. But what happens if last child switched
25 * its stdin into O_NONBLOCK mode?
26 *
27 * *** SURPRISE! It will affect the parent too! ***
28 * *** BIG SURPRISE! It stays even after child exits! ***
29 *
30 * This is a design bug in UNIX API.
31 * fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
32 * will set nonblocking mode not only on _your_ stdin, but
33 * also on stdin of your parent, etc.
34 *
35 * In general,
36 * fd2 = dup(fd1);
37 * fcntl(fd2, F_SETFL, fcntl(fd2, F_GETFL) | O_NONBLOCK);
38 * sets both fd1 and fd2 to O_NONBLOCK. This includes cases
39 * where duping is done implicitly by fork() etc.
40 *
41 * We need
42 * fcntl(fd2, F_SETFD, fcntl(fd2, F_GETFD) | O_NONBLOCK);
43 * (note SETFD, not SETFL!) but such thing doesn't exist.
44 *
45 * Alternatively, we need nonblocking_read(fd, ...) which doesn't
46 * require O_NONBLOCK dance at all. Actually, it exists:
47 * n = recv(fd, buf, len, MSG_DONTWAIT);
48 * "MSG_DONTWAIT:
49 * Enables non-blocking operation; if the operation
50 * would block, EAGAIN is returned."
51 * but recv() works only for sockets!
52 *
53 * So far I don't see any good solution, I can only propose
54 * that affected readers should be careful and use this routine,
55 * which detects EAGAIN and uses poll() to wait on the fd.
56 * Thankfully, poll() doesn't care about O_NONBLOCK flag.
57 */
58ssize_t FAST_FUNC nonblock_safe_read(int fd, void *buf, size_t count)
59{
60 struct pollfd pfd[1];
61 ssize_t n;
62
63 while (1) {
64 n = safe_read(fd, buf, count);
65 if (n >= 0 || errno != EAGAIN)
66 return n;
67 /* fd is in O_NONBLOCK mode. Wait using poll and repeat */
68 pfd[0].fd = fd;
69 pfd[0].events = POLLIN;
70 safe_poll(pfd, 1, -1); /* note: this pulls in printf */
71 }
72}
73
74// Reads one line a-la fgets (but doesn't save terminating '\n').
75// Reads byte-by-byte. Useful when it is important to not read ahead.
76// Bytes are appended to pfx (which must be malloced, or NULL).
77char* FAST_FUNC xmalloc_reads(int fd, char *buf, size_t *maxsz_p)
78{
79 char *p;
80 size_t sz = buf ? strlen(buf) : 0;
81 size_t maxsz = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
82
83 goto jump_in;
84 while (sz < maxsz) {
85 if ((size_t)(p - buf) == sz) {
86 jump_in:
87 buf = xrealloc(buf, sz + 128);
88 p = buf + sz;
89 sz += 128;
90 }
91 /* nonblock_safe_read() because we are used by e.g. shells */
92 if (nonblock_safe_read(fd, p, 1) != 1) { /* EOF/error */
93 if (p == buf) { /* we read nothing */
94 free(buf);
95 return NULL;
96 }
97 break;
98 }
99 if (*p == '\n')
100 break;
101 p++;
102 }
103 *p = '\0';
104 if (maxsz_p)
105 *maxsz_p = p - buf;
106 p++;
107 return xrealloc(buf, p - buf);
108}
109
110// Read (potentially big) files in one go. File size is estimated
111// by stat. Extra '\0' byte is appended.
112void* FAST_FUNC xmalloc_read(int fd, size_t *maxsz_p)
113{
114 char *buf;
115 size_t size, rd_size, total;
116 size_t to_read;
117 struct stat st;
118
119 to_read = maxsz_p ? *maxsz_p : (INT_MAX - 4095); /* max to read */
120
121 /* Estimate file size */
122 st.st_size = 0; /* in case fstat fails, assume 0 */
123 fstat(fd, &st);
124 /* /proc/N/stat files report st_size 0 */
125 /* In order to make such files readable, we add small const */
126 size = (st.st_size | 0x3ff) + 1;
127
128 total = 0;
129 buf = NULL;
130 while (1) {
131 if (to_read < size)
132 size = to_read;
133 buf = xrealloc(buf, total + size + 1);
134 rd_size = full_read(fd, buf + total, size);
135 if ((ssize_t)rd_size == (ssize_t)(-1)) { /* error */
136 free(buf);
137 return NULL;
138 }
139 total += rd_size;
140 if (rd_size < size) /* EOF */
141 break;
142 if (to_read <= rd_size)
143 break;
144 to_read -= rd_size;
145 /* grow by 1/8, but in [1k..64k] bounds */
146 size = ((total / 8) | 0x3ff) + 1;
147 if (size > 64*1024)
148 size = 64*1024;
149 }
150 buf = xrealloc(buf, total + 1);
151 buf[total] = '\0';
152
153 if (maxsz_p)
154 *maxsz_p = total;
155 return buf;
156}
157
158#ifdef USING_LSEEK_TO_GET_SIZE
159/* Alternatively, file size can be obtained by lseek to the end.
160 * The code is slightly bigger. Retained in case fstat approach
161 * will not work for some weird cases (/proc, block devices, etc).
162 * (NB: lseek also can fail to work for some weird files) */
163
164// Read (potentially big) files in one go. File size is estimated by
165// lseek to end.
166void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
167{
168 char *buf;
169 size_t size;
170 int fd;
171 off_t len;
172
173 fd = open(filename, O_RDONLY);
174 if (fd < 0)
175 return NULL;
176
177 /* /proc/N/stat files report len 0 here */
178 /* In order to make such files readable, we add small const */
179 size = 0x3ff; /* read only 1k on unseekable files */
180 len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */
181 if (len != (off_t)-1) {
182 xlseek(fd, 0, SEEK_SET);
183 size = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
184 if (len < size)
185 size = len;
186 }
187
188 buf = xmalloc(size + 1);
189 size = read_close(fd, buf, size);
190 if ((ssize_t)size < 0) {
191 free(buf);
192 return NULL;
193 }
194 buf = xrealloc(buf, size + 1);
195 buf[size] = '\0';
196
197 if (maxsz_p)
198 *maxsz_p = size;
199 return buf;
200}
201#endif
202
203// Read (potentially big) files in one go. File size is estimated
204// by stat.
205void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
206{
207 char *buf;
208 int fd;
209
210 fd = open(filename, O_RDONLY);
211 if (fd < 0)
212 return NULL;
213
214 buf = xmalloc_read(fd, maxsz_p);
215 close(fd);
216 return buf;
217}
218
219/* Die with an error message if we can't read the entire buffer. */
220void FAST_FUNC xread(int fd, void *buf, size_t count)
221{
222 if (count) {
223 ssize_t size = full_read(fd, buf, count);
224 if ((size_t)size != count)
225 bb_error_msg_and_die("short read");
226 }
227}
228
229/* Die with an error message if we can't read one character. */
230unsigned char FAST_FUNC xread_char(int fd)
231{
232 char tmp;
233 xread(fd, &tmp, 1);
234 return tmp;
235}
236
237void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p)
238{
239 void *buf = xmalloc_open_read_close(filename, maxsz_p);
240 if (!buf)
241 bb_perror_msg_and_die("can't read '%s'", filename);
242 return buf;
243}
244
245/* Used by e.g. rpm which gives us a fd without filename,
246 * thus we can't guess the format from filename's extension.
247 */
248#if ZIPPED
249void FAST_FUNC setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/)
250{
251 const int fail_if_not_detected = 1;
252 union {
253 uint8_t b[4];
254 uint16_t b16[2];
255 uint32_t b32[1];
256 } magic;
257 int offset = -2;
258# if BB_MMU
259 IF_DESKTOP(long long) int FAST_FUNC (*xformer)(int src_fd, int dst_fd);
260 enum { xformer_prog = 0 };
261# else
262 enum { xformer = 0 };
263 const char *xformer_prog;
264# endif
265
266 /* .gz and .bz2 both have 2-byte signature, and their
267 * unpack_XXX_stream wants this header skipped. */
268 xread(fd, magic.b16, sizeof(magic.b16));
269 if (ENABLE_FEATURE_SEAMLESS_GZ
270 && magic.b16[0] == GZIP_MAGIC
271 ) {
272# if BB_MMU
273 xformer = unpack_gz_stream;
274# else
275 xformer_prog = "gunzip";
276# endif
277 goto found_magic;
278 }
279 if (ENABLE_FEATURE_SEAMLESS_BZ2
280 && magic.b16[0] == BZIP2_MAGIC
281 ) {
282# if BB_MMU
283 xformer = unpack_bz2_stream;
284# else
285 xformer_prog = "bunzip2";
286# endif
287 goto found_magic;
288 }
289 if (ENABLE_FEATURE_SEAMLESS_XZ
290 && magic.b16[0] == XZ_MAGIC1
291 ) {
292 /* .xz signature: 0xfd, '7', 'z', 'X', 'Z', 0x00 */
293 /* More info at: http://tukaani.org/xz/xz-file-format.txt */
294 offset = -6;
295 xread(fd, magic.b32, sizeof(magic.b32));
296 if (magic.b32[0] == XZ_MAGIC2) {
297# if BB_MMU
298 xformer = unpack_xz_stream;
299 /* unpack_xz_stream wants fd at position 0 */
300 xlseek(fd, offset, SEEK_CUR);
301# else
302 xformer_prog = "unxz";
303# endif
304 goto found_magic;
305 }
306 }
307
308 /* No known magic seen */
309 if (fail_if_not_detected)
310 bb_error_msg_and_die("no gzip"
311 IF_FEATURE_SEAMLESS_BZ2("/bzip2")
312 IF_FEATURE_SEAMLESS_XZ("/xz")
313 " magic");
314 xlseek(fd, offset, SEEK_CUR);
315 return;
316
317 found_magic:
318# if !BB_MMU
319 /* NOMMU version of open_transformer execs
320 * an external unzipper that wants
321 * file position at the start of the file */
322 xlseek(fd, offset, SEEK_CUR);
323# endif
324 open_transformer(fd, xformer, xformer_prog);
325}
326#endif /* ZIPPED */
327
328int FAST_FUNC open_zipped(const char *fname)
329{
330#if !ZIPPED
331 return open(fname, O_RDONLY);
332#else
333 char *sfx;
334 int fd;
335
336 fd = open(fname, O_RDONLY);
337 if (fd < 0)
338 return fd;
339
340 sfx = strrchr(fname, '.');
341 if (sfx) {
342 sfx++;
343 if (ENABLE_FEATURE_SEAMLESS_LZMA && strcmp(sfx, "lzma") == 0)
344 /* .lzma has no header/signature, just trust it */
345 open_transformer(fd, unpack_lzma_stream, "unlzma");
346 else
347 if ((ENABLE_FEATURE_SEAMLESS_GZ && strcmp(sfx, "gz") == 0)
348 || (ENABLE_FEATURE_SEAMLESS_BZ2 && strcmp(sfx, "bz2") == 0)
349 || (ENABLE_FEATURE_SEAMLESS_XZ && strcmp(sfx, "xz") == 0)
350 ) {
351 setup_unzip_on_fd(fd /*, fail_if_not_detected: 1*/);
352 }
353 }
354
355 return fd;
356#endif
357}
358
359void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p)
360{
361 int fd;
362 char *image;
363
364 fd = open_zipped(fname);
365 if (fd < 0)
366 return NULL;
367
368 image = xmalloc_read(fd, maxsz_p);
369 if (!image)
370 bb_perror_msg("read error from '%s'", fname);
371 close(fd);
372
373 return image;
374}
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index d93dd2af9..6200fc600 100644
--- a/libbb/xfuncs.c
+++ b/libbb/xfuncs.c
@@ -199,15 +199,9 @@ off_t FAST_FUNC fdlength(int fd)
199} 199}
200#endif 200#endif
201 201
202char* FAST_FUNC xmalloc_ttyname(int fd) 202int FAST_FUNC bb_putchar_stderr(char ch)
203{ 203{
204 char *buf = xzalloc(128); 204 return write(STDERR_FILENO, &ch, 1);
205 int r = ttyname_r(fd, buf, 127);
206 if (r) {
207 free(buf);
208 buf = NULL;
209 }
210 return buf;
211} 205}
212 206
213static int wh_helper(int value, int def_val, const char *env_name, int *err) 207static int wh_helper(int value, int def_val, const char *env_name, int *err)
@@ -250,66 +244,3 @@ int FAST_FUNC tcsetattr_stdin_TCSANOW(const struct termios *tp)
250{ 244{
251 return tcsetattr(STDIN_FILENO, TCSANOW, tp); 245 return tcsetattr(STDIN_FILENO, TCSANOW, tp);
252} 246}
253
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}
diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c
index 7207ec58a..03aeaaa38 100644
--- a/libbb/xfuncs_printf.c
+++ b/libbb/xfuncs_printf.c
@@ -510,3 +510,77 @@ int FAST_FUNC bb_xioctl(int fd, unsigned request, void *argp)
510 return ret; 510 return ret;
511} 511}
512#endif 512#endif
513
514char* FAST_FUNC xmalloc_ttyname(int fd)
515{
516 char *buf = xzalloc(128);
517 int r = ttyname_r(fd, buf, 127);
518 if (r) {
519 free(buf);
520 buf = NULL;
521 }
522 return buf;
523}
524
525void FAST_FUNC generate_uuid(uint8_t *buf)
526{
527 /* http://www.ietf.org/rfc/rfc4122.txt
528 * 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
529 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
530 * | time_low |
531 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
532 * | time_mid | time_hi_and_version |
533 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
534 * |clk_seq_and_variant | node (0-1) |
535 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
536 * | node (2-5) |
537 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
538 * IOW, uuid has this layout:
539 * uint32_t time_low (big endian)
540 * uint16_t time_mid (big endian)
541 * uint16_t time_hi_and_version (big endian)
542 * version is a 4-bit field:
543 * 1 Time-based
544 * 2 DCE Security, with embedded POSIX UIDs
545 * 3 Name-based (MD5)
546 * 4 Randomly generated
547 * 5 Name-based (SHA-1)
548 * uint16_t clk_seq_and_variant (big endian)
549 * variant is a 3-bit field:
550 * 0xx Reserved, NCS backward compatibility
551 * 10x The variant specified in rfc4122
552 * 110 Reserved, Microsoft backward compatibility
553 * 111 Reserved for future definition
554 * uint8_t node[6]
555 *
556 * For version 4, these bits are set/cleared:
557 * time_hi_and_version & 0x0fff | 0x4000
558 * clk_seq_and_variant & 0x3fff | 0x8000
559 */
560 pid_t pid;
561 int i;
562
563 i = open("/dev/urandom", O_RDONLY);
564 if (i >= 0) {
565 read(i, buf, 16);
566 close(i);
567 }
568 /* Paranoia. /dev/urandom may be missing.
569 * rand() is guaranteed to generate at least [0, 2^15) range,
570 * but lowest bits in some libc are not so "random". */
571 srand(monotonic_us()); /* pulls in printf */
572 pid = getpid();
573 while (1) {
574 for (i = 0; i < 16; i++)
575 buf[i] ^= rand() >> 5;
576 if (pid == 0)
577 break;
578 srand(pid);
579 pid = 0;
580 }
581
582 /* version = 4 */
583 buf[4 + 2 ] = (buf[4 + 2 ] & 0x0f) | 0x40;
584 /* variant = 10x */
585 buf[4 + 2 + 2] = (buf[4 + 2 + 2] & 0x3f) | 0x80;
586}