aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2006-07-16 08:14:35 +0000
committerRob Landley <rob@landley.net>2006-07-16 08:14:35 +0000
commit534374755d618c9c36c9940c82756241c4b25a67 (patch)
treefac906b4fa40a68c53cecf20215a7a25b3b1cab6
parentafb94ecf2bb6c53ce2a381d6ce45a426243c76d9 (diff)
downloadbusybox-w32-534374755d618c9c36c9940c82756241c4b25a67.tar.gz
busybox-w32-534374755d618c9c36c9940c82756241c4b25a67.tar.bz2
busybox-w32-534374755d618c9c36c9940c82756241c4b25a67.zip
Cleaup read() and write() variants, plus a couple of new functions like
xlseek and fdlength() for the new mkswap.
-rw-r--r--applets/applets.c57
-rw-r--r--archival/ar.c2
-rw-r--r--archival/gunzip.c4
-rw-r--r--archival/libunarchive/Makefile.in1
-rw-r--r--archival/libunarchive/archive_xread_all.c21
-rw-r--r--archival/libunarchive/archive_xread_all_eof.c2
-rw-r--r--archival/libunarchive/check_header_gzip.c14
-rw-r--r--archival/libunarchive/data_extract_to_buffer.c2
-rw-r--r--archival/libunarchive/decompress_uncompress.c2
-rw-r--r--archival/libunarchive/decompress_unzip.c7
-rw-r--r--archival/libunarchive/get_header_ar.c4
-rw-r--r--archival/libunarchive/get_header_cpio.c5
-rw-r--r--archival/libunarchive/get_header_tar.c12
-rw-r--r--archival/libunarchive/get_header_tar_gz.c2
-rw-r--r--archival/libunarchive/unpack_ar_archive.c2
-rw-r--r--archival/rpm.c2
-rw-r--r--archival/rpm2cpio.c6
-rw-r--r--archival/tar.c12
-rw-r--r--archival/uncompress.c2
-rw-r--r--archival/unzip.c61
-rw-r--r--console-tools/loadkmap.c14
-rw-r--r--coreutils/dd.c12
-rw-r--r--coreutils/tail.c3
-rw-r--r--include/libbb.h12
-rw-r--r--include/unarchive.h1
-rw-r--r--init/init.c6
-rw-r--r--libbb/change_identity.c6
-rw-r--r--libbb/copyfd.c16
-rw-r--r--libbb/create_icmp6_socket.c2
-rw-r--r--libbb/create_icmp_socket.c2
-rw-r--r--libbb/full_read.c2
-rw-r--r--libbb/full_write.c2
-rw-r--r--libbb/loop.c1
-rw-r--r--libbb/xfuncs.c85
-rw-r--r--miscutils/rx.c2
-rw-r--r--networking/httpd.c16
-rw-r--r--networking/nc.c12
-rw-r--r--networking/tftp.c4
-rw-r--r--shell/ash.c10
39 files changed, 204 insertions, 224 deletions
diff --git a/applets/applets.c b/applets/applets.c
index 27becfd68..bd8cfec5d 100644
--- a/applets/applets.c
+++ b/applets/applets.c
@@ -343,22 +343,15 @@ static void check_suid (struct BB_applet *applet)
343 bb_error_msg_and_die ("You have no permission to run this applet!"); 343 bb_error_msg_and_die ("You have no permission to run this applet!");
344 344
345 if ((sct->m_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { /* *both* have to be set for sgid */ 345 if ((sct->m_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { /* *both* have to be set for sgid */
346 if (setegid (sct->m_gid)) 346 xsetgid(sct->m_gid);
347 bb_error_msg_and_die 347 } else xsetgid(rgid); /* no sgid -> drop */
348 ("BusyBox binary has insufficient rights to set proper GID for applet!"); 348
349 } else 349 if (sct->m_mode & S_ISUID) xsetuid(sct->m_uid);
350 setgid (rgid); /* no sgid -> drop */ 350 else xsetuid(ruid); /* no suid -> drop */
351
352 if (sct->m_mode & S_ISUID) {
353 if (seteuid (sct->m_uid))
354 bb_error_msg_and_die
355 ("BusyBox binary has insufficient rights to set proper UID for applet!");
356 } else
357 setuid (ruid); /* no suid -> drop */
358 } else { 351 } else {
359 /* default: drop all privileges */ 352 /* default: drop all privileges */
360 setgid (rgid); 353 xsetgid(rgid);
361 setuid (ruid); 354 xsetuid(ruid);
362 } 355 }
363 return; 356 return;
364 } else { 357 } else {
@@ -374,11 +367,10 @@ static void check_suid (struct BB_applet *applet)
374#endif 367#endif
375 368
376 if (applet->need_suid == _BB_SUID_ALWAYS) { 369 if (applet->need_suid == _BB_SUID_ALWAYS) {
377 if (geteuid () != 0) 370 if (geteuid()) bb_error_msg_and_die("Applet requires root privileges!");
378 bb_error_msg_and_die ("This applet requires root privileges!");
379 } else if (applet->need_suid == _BB_SUID_NEVER) { 371 } else if (applet->need_suid == _BB_SUID_NEVER) {
380 setgid (rgid); /* drop all privileges */ 372 xsetgid(rgid); /* drop all privileges */
381 setuid (ruid); 373 xsetuid(ruid);
382 } 374 }
383} 375}
384#else 376#else
@@ -419,14 +411,14 @@ static const char *unpack_usage_messages(void)
419 case -1: /* error */ 411 case -1: /* error */
420 exit(1); 412 exit(1);
421 case 0: /* child */ 413 case 0: /* child */
422 bb_full_write(input[1], packed_usage, sizeof(packed_usage)); 414 full_write(input[1], packed_usage, sizeof(packed_usage));
423 exit(0); 415 exit(0);
424 } 416 }
425 /* parent */ 417 /* parent */
426 close(input[1]); 418 close(input[1]);
427 419
428 buf = xmalloc(SIZEOF_usage_messages); 420 buf = xmalloc(SIZEOF_usage_messages);
429 bb_full_read(output[0], buf, SIZEOF_usage_messages); 421 full_read(output[0], buf, SIZEOF_usage_messages);
430 return buf; 422 return buf;
431} 423}
432 424
@@ -454,33 +446,32 @@ void bb_show_usage (void)
454 exit (bb_default_error_retval); 446 exit (bb_default_error_retval);
455} 447}
456 448
457static int applet_name_compare (const void *x, const void *y) 449static int applet_name_compare(const void *name, const void *vapplet)
458{ 450{
459 const char *name = x; 451 const struct BB_applet *applet = vapplet;
460 const struct BB_applet *applet = y;
461 452
462 return strcmp (name, applet->name); 453 return strcmp(name, applet->name);
463} 454}
464 455
465extern const size_t NUM_APPLETS; 456extern const size_t NUM_APPLETS;
466 457
467struct BB_applet *find_applet_by_name (const char *name) 458struct BB_applet *find_applet_by_name(const char *name)
468{ 459{
469 return bsearch (name, applets, NUM_APPLETS, sizeof (struct BB_applet), 460 return bsearch(name, applets, NUM_APPLETS, sizeof(struct BB_applet),
470 applet_name_compare); 461 applet_name_compare);
471} 462}
472 463
473void run_applet_by_name (const char *name, int argc, char **argv) 464void run_applet_by_name(const char *name, int argc, char **argv)
474{ 465{
475 if(ENABLE_FEATURE_SUID_CONFIG) parse_config_file (); 466 if (ENABLE_FEATURE_SUID_CONFIG) parse_config_file();
476 467
477 if(!strncmp(name, "busybox", 7)) busybox_main(argc, argv); 468 if (!strncmp(name, "busybox", 7)) busybox_main(argc, argv);
478 /* Do a binary search to find the applet entry given the name. */ 469 /* Do a binary search to find the applet entry given the name. */
479 applet_using = find_applet_by_name(name); 470 applet_using = find_applet_by_name(name);
480 if(applet_using) { 471 if (applet_using) {
481 bb_applet_name = applet_using->name; 472 bb_applet_name = applet_using->name;
482 if(argc==2 && !strcmp(argv[1], "--help")) bb_show_usage (); 473 if(argc==2 && !strcmp(argv[1], "--help")) bb_show_usage();
483 if(ENABLE_FEATURE_SUID) check_suid (applet_using); 474 if(ENABLE_FEATURE_SUID) check_suid(applet_using);
484 exit ((*(applet_using->main)) (argc, argv)); 475 exit((*(applet_using->main))(argc, argv));
485 } 476 }
486} 477}
diff --git a/archival/ar.c b/archival/ar.c
index 3bb75879b..fd2ab99a0 100644
--- a/archival/ar.c
+++ b/archival/ar.c
@@ -88,7 +88,7 @@ int ar_main(int argc, char **argv)
88 llist_add_to(&(archive_handle->accept), argv[optind++]); 88 llist_add_to(&(archive_handle->accept), argv[optind++]);
89 } 89 }
90 90
91 archive_xread_all(archive_handle, magic, 7); 91 xread(archive_handle->src_fd, magic, 7);
92 if (strncmp(magic, "!<arch>", 7) != 0) { 92 if (strncmp(magic, "!<arch>", 7) != 0) {
93 bb_error_msg_and_die("Invalid ar magic"); 93 bb_error_msg_and_die("Invalid ar magic");
94 } 94 }
diff --git a/archival/gunzip.c b/archival/gunzip.c
index 069666f58..bd6047e13 100644
--- a/archival/gunzip.c
+++ b/archival/gunzip.c
@@ -112,10 +112,10 @@ int gunzip_main(int argc, char **argv)
112 } 112 }
113 113
114 /* do the decompression, and cleanup */ 114 /* do the decompression, and cleanup */
115 if (bb_xread_char(src_fd) == 0x1f) { 115 if (xread_char(src_fd) == 0x1f) {
116 unsigned char magic2; 116 unsigned char magic2;
117 117
118 magic2 = bb_xread_char(src_fd); 118 magic2 = xread_char(src_fd);
119#ifdef CONFIG_FEATURE_GUNZIP_UNCOMPRESS 119#ifdef CONFIG_FEATURE_GUNZIP_UNCOMPRESS
120 if (magic2 == 0x9d) { 120 if (magic2 == 0x9d) {
121 status = uncompress(src_fd, dst_fd); 121 status = uncompress(src_fd, dst_fd);
diff --git a/archival/libunarchive/Makefile.in b/archival/libunarchive/Makefile.in
index 928e5bf8b..46c50f81d 100644
--- a/archival/libunarchive/Makefile.in
+++ b/archival/libunarchive/Makefile.in
@@ -29,7 +29,6 @@ LIBUNARCHIVE-y:= \
29 header_list.o \ 29 header_list.o \
30 header_verbose_list.o \ 30 header_verbose_list.o \
31\ 31\
32 archive_xread_all.o \
33 archive_xread_all_eof.o \ 32 archive_xread_all_eof.o \
34\ 33\
35 seek_by_char.o \ 34 seek_by_char.o \
diff --git a/archival/libunarchive/archive_xread_all.c b/archival/libunarchive/archive_xread_all.c
deleted file mode 100644
index bed8641a2..000000000
--- a/archival/libunarchive/archive_xread_all.c
+++ /dev/null
@@ -1,21 +0,0 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
4 */
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include "unarchive.h"
10#include "libbb.h"
11
12void archive_xread_all(const archive_handle_t *archive_handle, void *buf, const size_t count)
13{
14 ssize_t size;
15
16 size = bb_full_read(archive_handle->src_fd, buf, count);
17 if (size != count) {
18 bb_error_msg_and_die("Short read");
19 }
20 return;
21}
diff --git a/archival/libunarchive/archive_xread_all_eof.c b/archival/libunarchive/archive_xread_all_eof.c
index df9c88a56..e03fb0caa 100644
--- a/archival/libunarchive/archive_xread_all_eof.c
+++ b/archival/libunarchive/archive_xread_all_eof.c
@@ -13,7 +13,7 @@ ssize_t archive_xread_all_eof(archive_handle_t *archive_handle, unsigned char *b
13{ 13{
14 ssize_t size; 14 ssize_t size;
15 15
16 size = bb_full_read(archive_handle->src_fd, buf, count); 16 size = full_read(archive_handle->src_fd, buf, count);
17 if ((size != 0) && (size != count)) { 17 if ((size != 0) && (size != count)) {
18 bb_perror_msg_and_die("Short read, read %ld of %ld", (long)size, (long)count); 18 bb_perror_msg_and_die("Short read, read %ld of %ld", (long)size, (long)count);
19 } 19 }
diff --git a/archival/libunarchive/check_header_gzip.c b/archival/libunarchive/check_header_gzip.c
index 79477c610..77e1e6a46 100644
--- a/archival/libunarchive/check_header_gzip.c
+++ b/archival/libunarchive/check_header_gzip.c
@@ -20,7 +20,7 @@ void check_header_gzip(int src_fd)
20 } formated; 20 } formated;
21 } header; 21 } header;
22 22
23 bb_xread_all(src_fd, header.raw, 8); 23 xread(src_fd, header.raw, 8);
24 24
25 /* Check the compression method */ 25 /* Check the compression method */
26 if (header.formated.method != 8) { 26 if (header.formated.method != 8) {
@@ -32,10 +32,10 @@ void check_header_gzip(int src_fd)
32 /* bit 2 set: extra field present */ 32 /* bit 2 set: extra field present */
33 unsigned char extra_short; 33 unsigned char extra_short;
34 34
35 extra_short = bb_xread_char(src_fd) + (bb_xread_char(src_fd) << 8); 35 extra_short = xread_char(src_fd) + (xread_char(src_fd) << 8);
36 while (extra_short > 0) { 36 while (extra_short > 0) {
37 /* Ignore extra field */ 37 /* Ignore extra field */
38 bb_xread_char(src_fd); 38 xread_char(src_fd);
39 extra_short--; 39 extra_short--;
40 } 40 }
41 } 41 }
@@ -43,19 +43,19 @@ void check_header_gzip(int src_fd)
43 /* Discard original name if any */ 43 /* Discard original name if any */
44 if (header.formated.flags & 0x08) { 44 if (header.formated.flags & 0x08) {
45 /* bit 3 set: original file name present */ 45 /* bit 3 set: original file name present */
46 while(bb_xread_char(src_fd) != 0); 46 while(xread_char(src_fd) != 0);
47 } 47 }
48 48
49 /* Discard file comment if any */ 49 /* Discard file comment if any */
50 if (header.formated.flags & 0x10) { 50 if (header.formated.flags & 0x10) {
51 /* bit 4 set: file comment present */ 51 /* bit 4 set: file comment present */
52 while(bb_xread_char(src_fd) != 0); 52 while(xread_char(src_fd) != 0);
53 } 53 }
54 54
55 /* Read the header checksum */ 55 /* Read the header checksum */
56 if (header.formated.flags & 0x02) { 56 if (header.formated.flags & 0x02) {
57 bb_xread_char(src_fd); 57 xread_char(src_fd);
58 bb_xread_char(src_fd); 58 xread_char(src_fd);
59 } 59 }
60 60
61 return; 61 return;
diff --git a/archival/libunarchive/data_extract_to_buffer.c b/archival/libunarchive/data_extract_to_buffer.c
index fe76971df..95cb8f576 100644
--- a/archival/libunarchive/data_extract_to_buffer.c
+++ b/archival/libunarchive/data_extract_to_buffer.c
@@ -14,5 +14,5 @@ void data_extract_to_buffer(archive_handle_t *archive_handle)
14 14
15 archive_handle->buffer = xzalloc(size + 1); 15 archive_handle->buffer = xzalloc(size + 1);
16 16
17 archive_xread_all(archive_handle, archive_handle->buffer, size); 17 xread(archive_handle->src_fd, archive_handle->buffer, size);
18} 18}
diff --git a/archival/libunarchive/decompress_uncompress.c b/archival/libunarchive/decompress_uncompress.c
index 81764a47f..0c4ab6dda 100644
--- a/archival/libunarchive/decompress_uncompress.c
+++ b/archival/libunarchive/decompress_uncompress.c
@@ -116,7 +116,7 @@ int uncompress(int fd_in, int fd_out)
116 116
117 insize = 0; 117 insize = 0;
118 118
119 inbuf[0] = bb_xread_char(fd_in); 119 inbuf[0] = xread_char(fd_in);
120 120
121 maxbits = inbuf[0] & BIT_MASK; 121 maxbits = inbuf[0] & BIT_MASK;
122 block_mode = inbuf[0] & BLOCK_MODE; 122 block_mode = inbuf[0] & BLOCK_MODE;
diff --git a/archival/libunarchive/decompress_unzip.c b/archival/libunarchive/decompress_unzip.c
index 46a26933b..8f33e6e6c 100644
--- a/archival/libunarchive/decompress_unzip.c
+++ b/archival/libunarchive/decompress_unzip.c
@@ -116,9 +116,8 @@ static unsigned int fill_bitbuffer(unsigned int bitbuffer, unsigned int *current
116 /* Leave the first 4 bytes empty so we can always unwind the bitbuffer 116 /* Leave the first 4 bytes empty so we can always unwind the bitbuffer
117 * to the front of the bytebuffer, leave 4 bytes free at end of tail 117 * to the front of the bytebuffer, leave 4 bytes free at end of tail
118 * so we can easily top up buffer in check_trailer_gzip() */ 118 * so we can easily top up buffer in check_trailer_gzip() */
119 if (!(bytebuffer_size = bb_xread(gunzip_src_fd, &bytebuffer[4], bytebuffer_max - 8))) { 119 if (1 > (bytebuffer_size = safe_read(gunzip_src_fd, &bytebuffer[4], bytebuffer_max - 8)))
120 bb_error_msg_and_die("unexpected end of file"); 120 bb_error_msg_and_die("unexpected end of file");
121 }
122 bytebuffer_size += 4; 121 bytebuffer_size += 4;
123 bytebuffer_offset = 4; 122 bytebuffer_offset = 4;
124 } 123 }
@@ -862,7 +861,7 @@ int inflate_unzip(int in, int out)
862 861
863 while(1) { 862 while(1) {
864 int ret = inflate_get_next_window(); 863 int ret = inflate_get_next_window();
865 nwrote = bb_full_write(out, gunzip_window, gunzip_outbuf_count); 864 nwrote = full_write(out, gunzip_window, gunzip_outbuf_count);
866 if (nwrote == -1) { 865 if (nwrote == -1) {
867 bb_perror_msg("write"); 866 bb_perror_msg("write");
868 return -1; 867 return -1;
@@ -896,7 +895,7 @@ int inflate_gunzip(int in, int out)
896 /* top up the input buffer with the rest of the trailer */ 895 /* top up the input buffer with the rest of the trailer */
897 count = bytebuffer_size - bytebuffer_offset; 896 count = bytebuffer_size - bytebuffer_offset;
898 if (count < 8) { 897 if (count < 8) {
899 bb_xread_all(in, &bytebuffer[bytebuffer_size], 8 - count); 898 xread(in, &bytebuffer[bytebuffer_size], 8 - count);
900 bytebuffer_size += 8 - count; 899 bytebuffer_size += 8 - count;
901 } 900 }
902 for (count = 0; count != 4; count++) { 901 for (count = 0; count != 4; count++) {
diff --git a/archival/libunarchive/get_header_ar.c b/archival/libunarchive/get_header_ar.c
index 44d964287..48d7a5ad8 100644
--- a/archival/libunarchive/get_header_ar.c
+++ b/archival/libunarchive/get_header_ar.c
@@ -43,7 +43,7 @@ char get_header_ar(archive_handle_t *archive_handle)
43 if (ar.raw[0] == '\n') { 43 if (ar.raw[0] == '\n') {
44 /* fix up the header, we started reading 1 byte too early */ 44 /* fix up the header, we started reading 1 byte too early */
45 memmove(ar.raw, &ar.raw[1], 59); 45 memmove(ar.raw, &ar.raw[1], 59);
46 ar.raw[59] = bb_xread_char(archive_handle->src_fd); 46 ar.raw[59] = xread_char(archive_handle->src_fd);
47 archive_handle->offset++; 47 archive_handle->offset++;
48 } 48 }
49 archive_handle->offset += 60; 49 archive_handle->offset += 60;
@@ -68,7 +68,7 @@ char get_header_ar(archive_handle_t *archive_handle)
68 * in static variable long_names for use in future entries */ 68 * in static variable long_names for use in future entries */
69 ar_long_name_size = typed->size; 69 ar_long_name_size = typed->size;
70 ar_long_names = xmalloc(ar_long_name_size); 70 ar_long_names = xmalloc(ar_long_name_size);
71 bb_xread_all(archive_handle->src_fd, ar_long_names, ar_long_name_size); 71 xread(archive_handle->src_fd, ar_long_names, ar_long_name_size);
72 archive_handle->offset += ar_long_name_size; 72 archive_handle->offset += ar_long_name_size;
73 /* This ar entries data section only contained filenames for other records 73 /* This ar entries data section only contained filenames for other records
74 * they are stored in the static ar_long_names for future reference */ 74 * they are stored in the static ar_long_names for future reference */
diff --git a/archival/libunarchive/get_header_cpio.c b/archival/libunarchive/get_header_cpio.c
index 725c4911a..28c743589 100644
--- a/archival/libunarchive/get_header_cpio.c
+++ b/archival/libunarchive/get_header_cpio.c
@@ -76,7 +76,8 @@ char get_header_cpio(archive_handle_t *archive_handle)
76 } 76 }
77 77
78 file_header->name = (char *) xzalloc(namesize + 1); 78 file_header->name = (char *) xzalloc(namesize + 1);
79 archive_xread_all(archive_handle, file_header->name, namesize); /* Read in filename */ 79 /* Read in filename */
80 xread(archive_handle->src_fd, file_header->name, namesize);
80 archive_handle->offset += namesize; 81 archive_handle->offset += namesize;
81 82
82 /* Update offset amount and skip padding before file contents */ 83 /* Update offset amount and skip padding before file contents */
@@ -103,7 +104,7 @@ char get_header_cpio(archive_handle_t *archive_handle)
103 104
104 if (S_ISLNK(file_header->mode)) { 105 if (S_ISLNK(file_header->mode)) {
105 file_header->link_name = (char *) xzalloc(file_header->size + 1); 106 file_header->link_name = (char *) xzalloc(file_header->size + 1);
106 archive_xread_all(archive_handle, file_header->link_name, file_header->size); 107 xread(archive_handle->src_fd, file_header->link_name, file_header->size);
107 archive_handle->offset += file_header->size; 108 archive_handle->offset += file_header->size;
108 file_header->size = 0; /* Stop possible seeks in future */ 109 file_header->size = 0; /* Stop possible seeks in future */
109 } else { 110 } else {
diff --git a/archival/libunarchive/get_header_tar.c b/archival/libunarchive/get_header_tar.c
index 1cbde9543..4394d23ee 100644
--- a/archival/libunarchive/get_header_tar.c
+++ b/archival/libunarchive/get_header_tar.c
@@ -56,11 +56,7 @@ char get_header_tar(archive_handle_t *archive_handle)
56 /* Align header */ 56 /* Align header */
57 data_align(archive_handle, 512); 57 data_align(archive_handle, 512);
58 58
59 if (bb_full_read(archive_handle->src_fd, tar.raw, 512) != 512) { 59 xread(archive_handle->src_fd, tar.raw, 512);
60 /* Assume end of file */
61 bb_error_msg_and_die("Short header");
62 //return(EXIT_FAILURE);
63 }
64 archive_handle->offset += 512; 60 archive_handle->offset += 512;
65 61
66 /* If there is no filename its an empty header */ 62 /* If there is no filename its an empty header */
@@ -69,7 +65,7 @@ char get_header_tar(archive_handle_t *archive_handle)
69 /* This is the second consecutive empty header! End of archive! 65 /* This is the second consecutive empty header! End of archive!
70 * Read until the end to empty the pipe from gz or bz2 66 * Read until the end to empty the pipe from gz or bz2
71 */ 67 */
72 while (bb_full_read(archive_handle->src_fd, tar.raw, 512) == 512); 68 while (full_read(archive_handle->src_fd, tar.raw, 512) == 512);
73 return(EXIT_FAILURE); 69 return(EXIT_FAILURE);
74 } 70 }
75 end = 1; 71 end = 1;
@@ -166,14 +162,14 @@ char get_header_tar(archive_handle_t *archive_handle)
166#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS 162#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
167 case 'L': { 163 case 'L': {
168 longname = xzalloc(file_header->size + 1); 164 longname = xzalloc(file_header->size + 1);
169 archive_xread_all(archive_handle, longname, file_header->size); 165 xread(archive_handle->src_fd, longname, file_header->size);
170 archive_handle->offset += file_header->size; 166 archive_handle->offset += file_header->size;
171 167
172 return(get_header_tar(archive_handle)); 168 return(get_header_tar(archive_handle));
173 } 169 }
174 case 'K': { 170 case 'K': {
175 linkname = xzalloc(file_header->size + 1); 171 linkname = xzalloc(file_header->size + 1);
176 archive_xread_all(archive_handle, linkname, file_header->size); 172 xread(archive_handle->src_fd, linkname, file_header->size);
177 archive_handle->offset += file_header->size; 173 archive_handle->offset += file_header->size;
178 174
179 file_header->name = linkname; 175 file_header->name = linkname;
diff --git a/archival/libunarchive/get_header_tar_gz.c b/archival/libunarchive/get_header_tar_gz.c
index 3e1f466a7..ad26f465a 100644
--- a/archival/libunarchive/get_header_tar_gz.c
+++ b/archival/libunarchive/get_header_tar_gz.c
@@ -15,7 +15,7 @@ char get_header_tar_gz(archive_handle_t *archive_handle)
15 /* Cant lseek over pipe's */ 15 /* Cant lseek over pipe's */
16 archive_handle->seek = seek_by_char; 16 archive_handle->seek = seek_by_char;
17 17
18 archive_xread_all(archive_handle, &magic, 2); 18 xread(archive_handle->src_fd, &magic, 2);
19 if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) { 19 if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
20 bb_error_msg_and_die("Invalid gzip magic"); 20 bb_error_msg_and_die("Invalid gzip magic");
21 } 21 }
diff --git a/archival/libunarchive/unpack_ar_archive.c b/archival/libunarchive/unpack_ar_archive.c
index 47cf812ef..eed528391 100644
--- a/archival/libunarchive/unpack_ar_archive.c
+++ b/archival/libunarchive/unpack_ar_archive.c
@@ -12,7 +12,7 @@ void unpack_ar_archive(archive_handle_t *ar_archive)
12{ 12{
13 char magic[7]; 13 char magic[7];
14 14
15 archive_xread_all(ar_archive, magic, 7); 15 xread(ar_archive->src_fd, magic, 7);
16 if (strncmp(magic, "!<arch>", 7) != 0) { 16 if (strncmp(magic, "!<arch>", 7) != 0) {
17 bb_error_msg_and_die("Invalid ar magic"); 17 bb_error_msg_and_die("Invalid ar magic");
18 } 18 }
diff --git a/archival/rpm.c b/archival/rpm.c
index 075578971..3b70439a7 100644
--- a/archival/rpm.c
+++ b/archival/rpm.c
@@ -193,7 +193,7 @@ void extract_cpio_gz(int fd) {
193 archive_handle->src_fd = fd; 193 archive_handle->src_fd = fd;
194 archive_handle->offset = 0; 194 archive_handle->offset = 0;
195 195
196 bb_xread_all(archive_handle->src_fd, &magic, 2); 196 xread(archive_handle->src_fd, &magic, 2);
197 if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) { 197 if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
198 bb_error_msg_and_die("Invalid gzip magic"); 198 bb_error_msg_and_die("Invalid gzip magic");
199 } 199 }
diff --git a/archival/rpm2cpio.c b/archival/rpm2cpio.c
index 51807c7e2..6aae150e2 100644
--- a/archival/rpm2cpio.c
+++ b/archival/rpm2cpio.c
@@ -40,7 +40,7 @@ static void skip_header(int rpm_fd)
40{ 40{
41 struct rpm_header header; 41 struct rpm_header header;
42 42
43 bb_xread_all(rpm_fd, &header, sizeof(struct rpm_header)); 43 xread(rpm_fd, &header, sizeof(struct rpm_header));
44 if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0) { 44 if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0) {
45 bb_error_msg_and_die("Invalid RPM header magic"); /* Invalid magic */ 45 bb_error_msg_and_die("Invalid RPM header magic"); /* Invalid magic */
46 } 46 }
@@ -66,7 +66,7 @@ int rpm2cpio_main(int argc, char **argv)
66 rpm_fd = bb_xopen(argv[1], O_RDONLY); 66 rpm_fd = bb_xopen(argv[1], O_RDONLY);
67 } 67 }
68 68
69 bb_xread_all(rpm_fd, &lead, sizeof(struct rpm_lead)); 69 xread(rpm_fd, &lead, sizeof(struct rpm_lead));
70 if (strncmp((char *) &lead.magic, RPM_MAGIC, 4) != 0) { 70 if (strncmp((char *) &lead.magic, RPM_MAGIC, 4) != 0) {
71 bb_error_msg_and_die("Invalid RPM magic"); /* Just check the magic, the rest is irrelevant */ 71 bb_error_msg_and_die("Invalid RPM magic"); /* Just check the magic, the rest is irrelevant */
72 } 72 }
@@ -78,7 +78,7 @@ int rpm2cpio_main(int argc, char **argv)
78 /* Skip the main header */ 78 /* Skip the main header */
79 skip_header(rpm_fd); 79 skip_header(rpm_fd);
80 80
81 bb_xread_all(rpm_fd, &magic, 2); 81 xread(rpm_fd, &magic, 2);
82 if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) { 82 if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
83 bb_error_msg_and_die("Invalid gzip magic"); 83 bb_error_msg_and_die("Invalid gzip magic");
84 } 84 }
diff --git a/archival/tar.c b/archival/tar.c
index 5b7c1425a..426176bd2 100644
--- a/archival/tar.c
+++ b/archival/tar.c
@@ -269,9 +269,9 @@ static inline int writeTarHeader(struct TarBallInfo *tbInfo,
269 putOctal(header.chksum, 7, chksum); 269 putOctal(header.chksum, 7, chksum);
270 270
271 /* Now write the header out to disk */ 271 /* Now write the header out to disk */
272 if ((size = 272 if ((size = full_write(tbInfo->tarFd, (char *) &header,
273 bb_full_write(tbInfo->tarFd, (char *) &header, 273 sizeof(struct TarHeader))) < 0)
274 sizeof(struct TarHeader))) < 0) { 274 {
275 bb_error_msg(bb_msg_io_error, real_name); 275 bb_error_msg(bb_msg_io_error, real_name);
276 return (FALSE); 276 return (FALSE);
277 } 277 }
@@ -475,7 +475,7 @@ static inline int writeTarFile(const int tar_fd, const int verboseFlag,
475 while (1) { 475 while (1) {
476 char buf; 476 char buf;
477 477
478 int n = bb_full_read(gzipStatusPipe[0], &buf, 1); 478 int n = full_read(gzipStatusPipe[0], &buf, 1);
479 479
480 if (n == 0 && vfork_exec_errno != 0) { 480 if (n == 0 && vfork_exec_errno != 0) {
481 errno = vfork_exec_errno; 481 errno = vfork_exec_errno;
@@ -562,8 +562,8 @@ static char get_header_tar_Z(archive_handle_t *archive_handle)
562 archive_handle->seek = seek_by_char; 562 archive_handle->seek = seek_by_char;
563 563
564 /* do the decompression, and cleanup */ 564 /* do the decompression, and cleanup */
565 if (bb_xread_char(archive_handle->src_fd) != 0x1f || 565 if (xread_char(archive_handle->src_fd) != 0x1f ||
566 bb_xread_char(archive_handle->src_fd) != 0x9d) 566 xread_char(archive_handle->src_fd) != 0x9d)
567 { 567 {
568 bb_error_msg_and_die("Invalid magic"); 568 bb_error_msg_and_die("Invalid magic");
569 } 569 }
diff --git a/archival/uncompress.c b/archival/uncompress.c
index b282fe811..801293fd9 100644
--- a/archival/uncompress.c
+++ b/archival/uncompress.c
@@ -70,7 +70,7 @@ int uncompress_main(int argc, char **argv)
70 } 70 }
71 71
72 /* do the decompression, and cleanup */ 72 /* do the decompression, and cleanup */
73 if ((bb_xread_char(src_fd) != 0x1f) || (bb_xread_char(src_fd) != 0x9d)) { 73 if ((xread_char(src_fd) != 0x1f) || (xread_char(src_fd) != 0x9d)) {
74 bb_error_msg_and_die("Invalid magic"); 74 bb_error_msg_and_die("Invalid magic");
75 } 75 }
76 76
diff --git a/archival/unzip.c b/archival/unzip.c
index 8ba39e9af..632cc8551 100644
--- a/archival/unzip.c
+++ b/archival/unzip.c
@@ -32,28 +32,10 @@
32#include "unarchive.h" 32#include "unarchive.h"
33#include "busybox.h" 33#include "busybox.h"
34 34
35#if BB_BIG_ENDIAN 35#define ZIP_FILEHEADER_MAGIC SWAP_LE32(0x04034b50)
36static inline unsigned short 36#define ZIP_CDS_MAGIC SWAP_LE32(0x02014b50)
37__swap16(unsigned short x) { 37#define ZIP_CDS_END_MAGIC SWAP_LE32(0x06054b50)
38 return (((uint16_t)(x) & 0xFF) << 8) | (((uint16_t)(x) & 0xFF00) >> 8); 38#define ZIP_DD_MAGIC SWAP_LE32(0x08074b50)
39}
40
41static inline uint32_t
42__swap32(uint32_t x) {
43 return (((x & 0xFF) << 24) |
44 ((x & 0xFF00) << 8) |
45 ((x & 0xFF0000) >> 8) |
46 ((x & 0xFF000000) >> 24));
47}
48#else /* it's little-endian */
49# define __swap16(x) (x)
50# define __swap32(x) (x)
51#endif /* BB_BIG_ENDIAN */
52
53#define ZIP_FILEHEADER_MAGIC __swap32(0x04034b50)
54#define ZIP_CDS_MAGIC __swap32(0x02014b50)
55#define ZIP_CDS_END_MAGIC __swap32(0x06054b50)
56#define ZIP_DD_MAGIC __swap32(0x08074b50)
57 39
58extern unsigned int gunzip_crc; 40extern unsigned int gunzip_crc;
59extern unsigned int gunzip_bytes_out; 41extern unsigned int gunzip_bytes_out;
@@ -83,13 +65,6 @@ static void unzip_skip(int fd, off_t skip)
83 } 65 }
84} 66}
85 67
86static void unzip_read(int fd, void *buf, size_t count)
87{
88 if (bb_xread(fd, buf, count) != count) {
89 bb_error_msg_and_die(bb_msg_read_error);
90 }
91}
92
93static void unzip_create_leading_dirs(char *fn) 68static void unzip_create_leading_dirs(char *fn)
94{ 69{
95 /* Create all leading directories */ 70 /* Create all leading directories */
@@ -248,7 +223,7 @@ int unzip_main(int argc, char **argv)
248 unsigned int magic; 223 unsigned int magic;
249 224
250 /* Check magic number */ 225 /* Check magic number */
251 unzip_read(src_fd, &magic, 4); 226 xread(src_fd, &magic, 4);
252 if (magic == ZIP_CDS_MAGIC) { 227 if (magic == ZIP_CDS_MAGIC) {
253 break; 228 break;
254 } else if (magic != ZIP_FILEHEADER_MAGIC) { 229 } else if (magic != ZIP_FILEHEADER_MAGIC) {
@@ -256,19 +231,17 @@ int unzip_main(int argc, char **argv)
256 } 231 }
257 232
258 /* Read the file header */ 233 /* Read the file header */
259 unzip_read(src_fd, zip_header.raw, 26); 234 xread(src_fd, zip_header.raw, 26);
260#if BB_BIG_ENDIAN 235 zip_header.formated.version = SWAP_LE32(zip_header.formated.version);
261 zip_header.formated.version = __swap16(zip_header.formated.version); 236 zip_header.formated.flags = SWAP_LE32(zip_header.formated.flags);
262 zip_header.formated.flags = __swap16(zip_header.formated.flags); 237 zip_header.formated.method = SWAP_LE32(zip_header.formated.method);
263 zip_header.formated.method = __swap16(zip_header.formated.method); 238 zip_header.formated.modtime = SWAP_LE32(zip_header.formated.modtime);
264 zip_header.formated.modtime = __swap16(zip_header.formated.modtime); 239 zip_header.formated.moddate = SWAP_LE32(zip_header.formated.moddate);
265 zip_header.formated.moddate = __swap16(zip_header.formated.moddate); 240 zip_header.formated.crc32 = SWAP_LE32(zip_header.formated.crc32);
266 zip_header.formated.crc32 = __swap32(zip_header.formated.crc32); 241 zip_header.formated.cmpsize = SWAP_LE32(zip_header.formated.cmpsize);
267 zip_header.formated.cmpsize = __swap32(zip_header.formated.cmpsize); 242 zip_header.formated.ucmpsize = SWAP_LE32(zip_header.formated.ucmpsize);
268 zip_header.formated.ucmpsize = __swap32(zip_header.formated.ucmpsize); 243 zip_header.formated.filename_len = SWAP_LE32(zip_header.formated.filename_len);
269 zip_header.formated.filename_len = __swap16(zip_header.formated.filename_len); 244 zip_header.formated.extra_len = SWAP_LE32(zip_header.formated.extra_len);
270 zip_header.formated.extra_len = __swap16(zip_header.formated.extra_len);
271#endif /* BB_BIG_ENDIAN */
272 if ((zip_header.formated.method != 0) && (zip_header.formated.method != 8)) { 245 if ((zip_header.formated.method != 0) && (zip_header.formated.method != 8)) {
273 bb_error_msg_and_die("Unsupported compression method %d", zip_header.formated.method); 246 bb_error_msg_and_die("Unsupported compression method %d", zip_header.formated.method);
274 } 247 }
@@ -276,7 +249,7 @@ int unzip_main(int argc, char **argv)
276 /* Read filename */ 249 /* Read filename */
277 free(dst_fn); 250 free(dst_fn);
278 dst_fn = xzalloc(zip_header.formated.filename_len + 1); 251 dst_fn = xzalloc(zip_header.formated.filename_len + 1);
279 unzip_read(src_fd, dst_fn, zip_header.formated.filename_len); 252 xread(src_fd, dst_fn, zip_header.formated.filename_len);
280 253
281 /* Skip extra header bytes */ 254 /* Skip extra header bytes */
282 unzip_skip(src_fd, zip_header.formated.extra_len); 255 unzip_skip(src_fd, zip_header.formated.extra_len);
diff --git a/console-tools/loadkmap.c b/console-tools/loadkmap.c
index 499d34622..69d33bd95 100644
--- a/console-tools/loadkmap.c
+++ b/console-tools/loadkmap.c
@@ -45,15 +45,15 @@ int loadkmap_main(int argc, char **argv)
45 45
46 fd = bb_xopen(CURRENT_VC, O_RDWR); 46 fd = bb_xopen(CURRENT_VC, O_RDWR);
47 47
48 if ((bb_full_read(0, buff, 7) != 7) || (strncmp(buff, BINARY_KEYMAP_MAGIC, 7) != 0)) 48 xread(0, buff, 7);
49 if (strncmp(buff, BINARY_KEYMAP_MAGIC, 7))
49 bb_error_msg_and_die("This is not a valid binary keymap."); 50 bb_error_msg_and_die("This is not a valid binary keymap.");
50 51
51 if (bb_full_read(0, flags, MAX_NR_KEYMAPS) != MAX_NR_KEYMAPS) 52 xread(0, flags, MAX_NR_KEYMAPS);
52 bb_perror_msg_and_die("Error reading keymap flags");
53 53
54 for (i = 0; i < MAX_NR_KEYMAPS; i++) { 54 for (i = 0; i < MAX_NR_KEYMAPS; i++) {
55 if (flags[i] == 1) { 55 if (flags[i] == 1) {
56 bb_full_read(0, ibuff, NR_KEYS * sizeof(u_short)); 56 xread(0, ibuff, NR_KEYS * sizeof(u_short));
57 for (j = 0; j < NR_KEYS; j++) { 57 for (j = 0; j < NR_KEYS; j++) {
58 ke.kb_index = j; 58 ke.kb_index = j;
59 ke.kb_table = i; 59 ke.kb_table = i;
@@ -63,8 +63,6 @@ int loadkmap_main(int argc, char **argv)
63 } 63 }
64 } 64 }
65 65
66 /* Don't bother to close files. Exit does that 66 if (ENABLE_FEATURE_CLEAN_UP) close(fd);
67 * automagically, so we can save a few bytes */ 67 return 0;
68 /* close(fd); */
69 return EXIT_SUCCESS;
70} 68}
diff --git a/coreutils/dd.c b/coreutils/dd.c
index 33e789311..3d6f7cd2d 100644
--- a/coreutils/dd.c
+++ b/coreutils/dd.c
@@ -196,26 +196,20 @@ int dd_main(int argc, char **argv)
196 tmp += d; 196 tmp += d;
197 oc += d; 197 oc += d;
198 if (oc == obs) { 198 if (oc == obs) {
199 if (bb_full_write(ofd, obuf, obs) < 0) { 199 xwrite(ofd, obuf, obs);
200 bb_perror_msg_and_die("%s", outfile);
201 }
202 out_full++; 200 out_full++;
203 oc = 0; 201 oc = 0;
204 } 202 }
205 } 203 }
206 } else { 204 } else {
207 if ((n = bb_full_write(ofd, ibuf, n)) < 0) { 205 xwrite(ofd, ibuf, n);
208 bb_perror_msg_and_die("%s", outfile);
209 }
210 if (n == ibs) out_full++; 206 if (n == ibs) out_full++;
211 else out_part++; 207 else out_part++;
212 } 208 }
213 } 209 }
214 210
215 if (ENABLE_FEATURE_DD_IBS_OBS && oc) { 211 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
216 if (bb_full_write(ofd, obuf, oc) < 0) { 212 xwrite(ofd, obuf, oc);
217 bb_perror_msg_and_die("%s", outfile);
218 }
219 out_part++; 213 out_part++;
220 } 214 }
221 if (close (ifd) < 0) { 215 if (close (ifd) < 0) {
diff --git a/coreutils/tail.c b/coreutils/tail.c
index e63406e31..80a66fbf5 100644
--- a/coreutils/tail.c
+++ b/coreutils/tail.c
@@ -54,9 +54,8 @@ static void tail_xprint_header(const char *fmt, const char *filename)
54static void tail_xbb_full_write(const char *buf, size_t len) 54static void tail_xbb_full_write(const char *buf, size_t len)
55{ 55{
56 /* If we get a write error, there is really no sense in continuing. */ 56 /* If we get a write error, there is really no sense in continuing. */
57 if (bb_full_write(STDOUT_FILENO, buf, len) < 0) { 57 if (full_write(STDOUT_FILENO, buf, len) < 0)
58 bb_perror_nomsg_and_die(); 58 bb_perror_nomsg_and_die();
59 }
60} 59}
61 60
62static ssize_t tail_read(int fd, char *buf, size_t count) 61static ssize_t tail_read(int fd, char *buf, size_t count)
diff --git a/include/libbb.h b/include/libbb.h
index 549b4fc0c..ddf965fbf 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -133,9 +133,9 @@ extern DIR *bb_xopendir(const char *path);
133extern int remove_file(const char *path, int flags); 133extern int remove_file(const char *path, int flags);
134extern int copy_file(const char *source, const char *dest, int flags); 134extern int copy_file(const char *source, const char *dest, int flags);
135extern ssize_t safe_read(int fd, void *buf, size_t count); 135extern ssize_t safe_read(int fd, void *buf, size_t count);
136extern ssize_t bb_full_read(int fd, void *buf, size_t len); 136extern ssize_t full_read(int fd, void *buf, size_t len);
137extern ssize_t safe_write(int fd, const void *buf, size_t count); 137extern ssize_t safe_write(int fd, const void *buf, size_t count);
138extern ssize_t bb_full_write(int fd, const void *buf, size_t len); 138extern ssize_t full_write(int fd, const void *buf, size_t len);
139extern int recursive_action(const char *fileName, int recurse, 139extern int recursive_action(const char *fileName, int recurse,
140 int followLinks, int depthFirst, 140 int followLinks, int depthFirst,
141 int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData), 141 int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData),
@@ -187,6 +187,7 @@ extern void itoa_to_buf(int n, char *buf, unsigned buflen);
187extern char *itoa(int n); 187extern char *itoa(int n);
188extern void xsetgid(gid_t gid); 188extern void xsetgid(gid_t gid);
189extern void xsetuid(uid_t uid); 189extern void xsetuid(uid_t uid);
190extern off_t fdlength(int fd);
190 191
191#define BB_GETOPT_ERROR 0x80000000UL 192#define BB_GETOPT_ERROR 0x80000000UL
192extern const char *bb_opt_complementally; 193extern const char *bb_opt_complementally;
@@ -477,9 +478,10 @@ extern int obscure(const char *old, const char *newval, const struct passwd *pwd
477 478
478extern int bb_xopen(const char *pathname, int flags); 479extern int bb_xopen(const char *pathname, int flags);
479extern int bb_xopen3(const char *pathname, int flags, int mode); 480extern int bb_xopen3(const char *pathname, int flags, int mode);
480extern ssize_t bb_xread(int fd, void *buf, size_t count); 481extern void xread(int fd, void *buf, size_t count);
481extern void bb_xread_all(int fd, void *buf, size_t count); 482extern unsigned char xread_char(int fd);
482extern unsigned char bb_xread_char(int fd); 483extern void xlseek(int fd, off_t offset, int whence);
484extern void xwrite(int fd, void *buf, size_t count);
483 485
484#ifndef COMM_LEN 486#ifndef COMM_LEN
485#ifdef TASK_COMM_LEN 487#ifdef TASK_COMM_LEN
diff --git a/include/unarchive.h b/include/unarchive.h
index 752a05c86..05ab0c16a 100644
--- a/include/unarchive.h
+++ b/include/unarchive.h
@@ -96,7 +96,6 @@ extern char get_header_tar_gz(archive_handle_t *archive_handle);
96extern void seek_by_jump(const archive_handle_t *archive_handle, const unsigned int amount); 96extern void seek_by_jump(const archive_handle_t *archive_handle, const unsigned int amount);
97extern void seek_by_char(const archive_handle_t *archive_handle, const unsigned int amount); 97extern void seek_by_char(const archive_handle_t *archive_handle, const unsigned int amount);
98 98
99extern void archive_xread_all(const archive_handle_t *archive_handle, void *buf, const size_t count);
100extern ssize_t archive_xread_all_eof(archive_handle_t *archive_handle, unsigned char *buf, size_t count); 99extern ssize_t archive_xread_all_eof(archive_handle_t *archive_handle, unsigned char *buf, size_t count);
101 100
102extern void data_align(archive_handle_t *archive_handle, const unsigned short boundary); 101extern void data_align(archive_handle_t *archive_handle, const unsigned short boundary);
diff --git a/init/init.c b/init/init.c
index c9200bd6c..cad64f63a 100644
--- a/init/init.c
+++ b/init/init.c
@@ -221,7 +221,7 @@ static void message(int device, const char *fmt, ...)
221 } 221 }
222 } 222 }
223 if ((device & LOG) && (log_fd >= 0)) { 223 if ((device & LOG) && (log_fd >= 0)) {
224 bb_full_write(log_fd, msg, l); 224 full_write(log_fd, msg, l);
225 } 225 }
226#endif 226#endif
227 227
@@ -230,7 +230,7 @@ static void message(int device, const char *fmt, ...)
230 O_WRONLY | O_NOCTTY | O_NONBLOCK); 230 O_WRONLY | O_NOCTTY | O_NONBLOCK);
231 /* Always send console messages to /dev/console so people will see them. */ 231 /* Always send console messages to /dev/console so people will see them. */
232 if (fd >= 0) { 232 if (fd >= 0) {
233 bb_full_write(fd, msg, l); 233 full_write(fd, msg, l);
234 close(fd); 234 close(fd);
235#if ENABLE_DEBUG_INIT 235#if ENABLE_DEBUG_INIT
236 /* all descriptors may be closed */ 236 /* all descriptors may be closed */
@@ -536,7 +536,7 @@ static pid_t run(const struct init_action *a)
536 messageD(LOG, "Waiting for enter to start '%s'" 536 messageD(LOG, "Waiting for enter to start '%s'"
537 "(pid %d, terminal %s)\n", 537 "(pid %d, terminal %s)\n",
538 cmdpath, getpid(), a->terminal); 538 cmdpath, getpid(), a->terminal);
539 bb_full_write(1, press_enter, sizeof(press_enter) - 1); 539 full_write(1, press_enter, sizeof(press_enter) - 1);
540 while(read(0, &c, 1) == 1 && c != '\n') 540 while(read(0, &c, 1) == 1 && c != '\n')
541 ; 541 ;
542 } 542 }
diff --git a/libbb/change_identity.c b/libbb/change_identity.c
index adebad8ed..74ffccbd3 100644
--- a/libbb/change_identity.c
+++ b/libbb/change_identity.c
@@ -46,10 +46,8 @@ const char *change_identity_e2str ( const struct passwd *pw )
46 return "cannot set groups"; 46 return "cannot set groups";
47 endgrent ( ); 47 endgrent ( );
48 48
49 if ( setgid ( pw-> pw_gid )) 49 xsetgid(pw-> pw_gid);
50 return "cannot set group id"; 50 xsetuid(pw->pw_uid);
51 if ( setuid ( pw->pw_uid ))
52 return "cannot set user id";
53 return NULL; 51 return NULL;
54} 52}
55 53
diff --git a/libbb/copyfd.c b/libbb/copyfd.c
index e2c542e32..0c4f7a054 100644
--- a/libbb/copyfd.c
+++ b/libbb/copyfd.c
@@ -30,24 +30,24 @@ static ssize_t bb_full_fd_action(int src_fd, int dst_fd, size_t size)
30 if (src_fd < 0) goto out; 30 if (src_fd < 0) goto out;
31 while (!size || total < size) 31 while (!size || total < size)
32 { 32 {
33 ssize_t wrote, xread; 33 ssize_t wr, rd;
34 34
35 xread = safe_read(src_fd, buffer, 35 rd = safe_read(src_fd, buffer,
36 (!size || size - total > BUFSIZ) ? BUFSIZ : size - total); 36 (!size || size - total > BUFSIZ) ? BUFSIZ : size - total);
37 37
38 if (xread > 0) { 38 if (rd > 0) {
39 /* A -1 dst_fd means we need to fake it... */ 39 /* A -1 dst_fd means we need to fake it... */
40 wrote = (dst_fd < 0) ? xread : bb_full_write(dst_fd, buffer, xread); 40 wr = (dst_fd < 0) ? rd : full_write(dst_fd, buffer, rd);
41 if (wrote < xread) { 41 if (wr < rd) {
42 bb_perror_msg(bb_msg_write_error); 42 bb_perror_msg(bb_msg_write_error);
43 break; 43 break;
44 } 44 }
45 total += wrote; 45 total += wr;
46 if (total == size) status = 0; 46 if (total == size) status = 0;
47 } else if (xread < 0) { 47 } else if (rd < 0) {
48 bb_perror_msg(bb_msg_read_error); 48 bb_perror_msg(bb_msg_read_error);
49 break; 49 break;
50 } else if (xread == 0) { 50 } else if (rd == 0) {
51 /* All done. */ 51 /* All done. */
52 status = 0; 52 status = 0;
53 break; 53 break;
diff --git a/libbb/create_icmp6_socket.c b/libbb/create_icmp6_socket.c
index d8ff35a0a..c3d1b5578 100644
--- a/libbb/create_icmp6_socket.c
+++ b/libbb/create_icmp6_socket.c
@@ -32,7 +32,7 @@ int create_icmp6_socket(void)
32 } 32 }
33 33
34 /* drop root privs if running setuid */ 34 /* drop root privs if running setuid */
35 setuid(getuid()); 35 xsetuid(getuid());
36 36
37 return sock; 37 return sock;
38} 38}
diff --git a/libbb/create_icmp_socket.c b/libbb/create_icmp_socket.c
index 26120a66d..431c4d8a7 100644
--- a/libbb/create_icmp_socket.c
+++ b/libbb/create_icmp_socket.c
@@ -31,7 +31,7 @@ int create_icmp_socket(void)
31 } 31 }
32 32
33 /* drop root privs if running setuid */ 33 /* drop root privs if running setuid */
34 setuid(getuid()); 34 xsetuid(getuid());
35 35
36 return sock; 36 return sock;
37} 37}
diff --git a/libbb/full_read.c b/libbb/full_read.c
index 8d64bfb90..b5837d5bd 100644
--- a/libbb/full_read.c
+++ b/libbb/full_read.c
@@ -17,7 +17,7 @@
17 * Returns the amount read, or -1 on an error. 17 * Returns the amount read, or -1 on an error.
18 * A short read is returned on an end of file. 18 * A short read is returned on an end of file.
19 */ 19 */
20ssize_t bb_full_read(int fd, void *buf, size_t len) 20ssize_t full_read(int fd, void *buf, size_t len)
21{ 21{
22 ssize_t cc; 22 ssize_t cc;
23 ssize_t total; 23 ssize_t total;
diff --git a/libbb/full_write.c b/libbb/full_write.c
index 3d6d40184..d812d04b4 100644
--- a/libbb/full_write.c
+++ b/libbb/full_write.c
@@ -16,7 +16,7 @@
16 * This does multiple writes as necessary. 16 * This does multiple writes as necessary.
17 * Returns the amount written, or -1 on an error. 17 * Returns the amount written, or -1 on an error.
18 */ 18 */
19ssize_t bb_full_write(int fd, const void *buf, size_t len) 19ssize_t full_write(int fd, const void *buf, size_t len)
20{ 20{
21 ssize_t cc; 21 ssize_t cc;
22 ssize_t total; 22 ssize_t total;
diff --git a/libbb/loop.c b/libbb/loop.c
index b9caa973b..0b05cd75f 100644
--- a/libbb/loop.c
+++ b/libbb/loop.c
@@ -3,6 +3,7 @@
3 * Utility routines. 3 * Utility routines.
4 * 4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> 5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 * Copyright (C) 2005 by Rob Landley <rob@landley.net>
6 * 7 *
7 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. 8 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
8 */ 9 */
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index d843414f9..8562a4fcb 100644
--- a/libbb/xfuncs.c
+++ b/libbb/xfuncs.c
@@ -120,40 +120,55 @@ int bb_xopen3(const char *pathname, int flags, int mode)
120#endif 120#endif
121 121
122#ifdef L_xread 122#ifdef L_xread
123ssize_t bb_xread(int fd, void *buf, size_t count) 123
124// Die with an error message if we can't read the entire buffer.
125
126void xread(int fd, void *buf, size_t count)
124{ 127{
125 ssize_t size; 128 while (count) {
129 ssize_t size;
126 130
127 size = read(fd, buf, count); 131 if ((size = safe_read(fd, buf, count)) < 1)
128 if (size < 0) { 132 bb_error_msg_and_die("Short read");
129 bb_perror_msg_and_die(bb_msg_read_error); 133 count -= size;
134 buf = ((char *) buf) + size;
130 } 135 }
131 return(size);
132} 136}
133#endif 137#endif
134 138
135#ifdef L_xread_all 139#ifdef L_xwrite
136void bb_xread_all(int fd, void *buf, size_t count)
137{
138 ssize_t size;
139 140
141// Die with an error message if we can't write the entire buffer.
142
143void xwrite(int fd, void *buf, size_t count)
144{
140 while (count) { 145 while (count) {
141 if ((size = bb_xread(fd, buf, count)) == 0) { /* EOF */ 146 ssize_t size;
142 bb_error_msg_and_die("Short read"); 147
143 } 148 if ((size = safe_write(fd, buf, count)) < 1)
149 bb_error_msg_and_die("Short write");
144 count -= size; 150 count -= size;
145 buf = ((char *) buf) + size; 151 buf = ((char *) buf) + size;
146 } 152 }
147 return; 153}
154#endif
155
156#ifdef L_xlseek
157
158// Die if we can't lseek to the right spot.
159
160void xlseek(int fd, off_t offset, int whence)
161{
162 if (whence != lseek(fd, offset, whence)) bb_error_msg_and_die("lseek");
148} 163}
149#endif 164#endif
150 165
151#ifdef L_xread_char 166#ifdef L_xread_char
152unsigned char bb_xread_char(int fd) 167unsigned char xread_char(int fd)
153{ 168{
154 char tmp; 169 char tmp;
155 170
156 bb_xread_all(fd, &tmp, 1); 171 xread(fd, &tmp, 1);
157 172
158 return(tmp); 173 return(tmp);
159} 174}
@@ -294,3 +309,41 @@ void xsetuid(uid_t uid)
294 if (setuid(uid)) bb_error_msg_and_die("setuid"); 309 if (setuid(uid)) bb_error_msg_and_die("setuid");
295} 310}
296#endif 311#endif
312
313#ifdef L_fdlength
314off_t fdlength(int fd)
315{
316 off_t bottom = 0, top = 0, pos;
317 long size;
318
319 // If the ioctl works for this, return it.
320
321 if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512;
322
323 // If not, do a binary search for the last location we can read.
324
325 do {
326 char temp;
327
328 pos = bottom + (top - bottom) / 2;;
329
330 // If we can read from the current location, it's bigger.
331
332 if (lseek(fd, pos, 0)>=0 && safe_read(fd, &temp, 1)==1) {
333 if (bottom == top) bottom = top = (top+1) * 2;
334 else bottom = pos;
335
336 // If we can't, it's smaller.
337
338 } else {
339 if (bottom == top) {
340 if (!top) return 0;
341 bottom = top/2;
342 }
343 else top = pos;
344 }
345 } while (bottom + 1 != top);
346
347 return pos + 1;
348}
349#endif
diff --git a/miscutils/rx.c b/miscutils/rx.c
index cff5a627b..41673b60e 100644
--- a/miscutils/rx.c
+++ b/miscutils/rx.c
@@ -212,7 +212,7 @@ static int receive(char *error_buf, size_t error_buf_size,
212 wantBlockNo++; 212 wantBlockNo++;
213 length += blockLength; 213 length += blockLength;
214 214
215 if (bb_full_write(filefd, blockBuf, blockLength) < 0) { 215 if (full_write(filefd, blockBuf, blockLength) < 0) {
216 note_error("write to file failed: %m"); 216 note_error("write to file failed: %m");
217 goto fatal; 217 goto fatal;
218 } 218 }
diff --git a/networking/httpd.c b/networking/httpd.c
index 4cd09448c..452b56d19 100644
--- a/networking/httpd.c
+++ b/networking/httpd.c
@@ -961,7 +961,7 @@ static int sendHeaders(HttpResponseNum responseNum)
961#if DEBUG 961#if DEBUG
962 fprintf(stderr, "Headers: '%s'", buf); 962 fprintf(stderr, "Headers: '%s'", buf);
963#endif 963#endif
964 return bb_full_write(a_c_w, buf, len); 964 return full_write(a_c_w, buf, len);
965} 965}
966 966
967/**************************************************************************** 967/****************************************************************************
@@ -1222,7 +1222,7 @@ static int sendCgi(const char *url,
1222 break; 1222 break;
1223 } 1223 }
1224 } else if(post_readed_size > 0 && FD_ISSET(outFd, &writeSet)) { 1224 } else if(post_readed_size > 0 && FD_ISSET(outFd, &writeSet)) {
1225 count = bb_full_write(outFd, wbuf + post_readed_idx, post_readed_size); 1225 count = full_write(outFd, wbuf + post_readed_idx, post_readed_size);
1226 if(count > 0) { 1226 if(count > 0) {
1227 post_readed_size -= count; 1227 post_readed_size -= count;
1228 post_readed_idx += count; 1228 post_readed_idx += count;
@@ -1263,14 +1263,14 @@ static int sendCgi(const char *url,
1263 rbuf[count] = 0; 1263 rbuf[count] = 0;
1264 /* check to see if the user script added headers */ 1264 /* check to see if the user script added headers */
1265 if(strncmp(rbuf, "HTTP/1.0 200 OK\r\n", 4) != 0) { 1265 if(strncmp(rbuf, "HTTP/1.0 200 OK\r\n", 4) != 0) {
1266 bb_full_write(s, "HTTP/1.0 200 OK\r\n", 17); 1266 full_write(s, "HTTP/1.0 200 OK\r\n", 17);
1267 } 1267 }
1268 if (strstr(rbuf, "ontent-") == 0) { 1268 if (strstr(rbuf, "ontent-") == 0) {
1269 bb_full_write(s, "Content-type: text/plain\r\n\r\n", 28); 1269 full_write(s, "Content-type: text/plain\r\n\r\n", 28);
1270 } 1270 }
1271 firstLine = 0; 1271 firstLine = 0;
1272 } 1272 }
1273 if (bb_full_write(s, rbuf, count) != count) 1273 if (full_write(s, rbuf, count) != count)
1274 break; 1274 break;
1275 1275
1276#if DEBUG 1276#if DEBUG
@@ -1337,8 +1337,8 @@ static int sendFile(const char *url)
1337 char *buf = config->buf; 1337 char *buf = config->buf;
1338 1338
1339 sendHeaders(HTTP_OK); 1339 sendHeaders(HTTP_OK);
1340 while ((count = bb_full_read(f, buf, MAX_MEMORY_BUFF)) > 0) { 1340 while ((count = full_read(f, buf, MAX_MEMORY_BUFF)) > 0) {
1341 if (bb_full_write(a_c_w, buf, count) != count) 1341 if (full_write(a_c_w, buf, count) != count)
1342 break; 1342 break;
1343 } 1343 }
1344 close(f); 1344 close(f);
@@ -2000,7 +2000,7 @@ int httpd_main(int argc, char *argv[])
2000# ifdef CONFIG_FEATURE_HTTPD_SETUID 2000# ifdef CONFIG_FEATURE_HTTPD_SETUID
2001 /* drop privileges */ 2001 /* drop privileges */
2002 if(uid > 0) 2002 if(uid > 0)
2003 setuid(uid); 2003 xsetuid(uid);
2004# endif 2004# endif
2005#endif 2005#endif
2006 2006
diff --git a/networking/nc.c b/networking/nc.c
index bda0c407b..117bbe20e 100644
--- a/networking/nc.c
+++ b/networking/nc.c
@@ -9,8 +9,6 @@
9 9
10#include "busybox.h" 10#include "busybox.h"
11 11
12#define xread bb_xread
13
14static void timeout(int signum) 12static void timeout(int signum)
15{ 13{
16 bb_error_msg_and_die("Timed out"); 14 bb_error_msg_and_die("Timed out");
@@ -151,13 +149,14 @@ repeatyness:
151 149
152 for (fd = 0; fd < FD_SETSIZE; fd++) { 150 for (fd = 0; fd < FD_SETSIZE; fd++) {
153 if (FD_ISSET(fd, &testfds)) { 151 if (FD_ISSET(fd, &testfds)) {
154 nread = xread(fd, bb_common_bufsiz1, sizeof(bb_common_bufsiz1)); 152 nread = safe_read(fd, bb_common_bufsiz1,
153 sizeof(bb_common_bufsiz1));
155 154
156 if (fd == cfd) { 155 if (fd == cfd) {
157 if (!nread) exit(0); 156 if (nread<1) exit(0);
158 ofd = STDOUT_FILENO; 157 ofd = STDOUT_FILENO;
159 } else { 158 } else {
160 if (!nread) { 159 if (nread<1) {
161 // Close outgoing half-connection so they get EOF, but 160 // Close outgoing half-connection so they get EOF, but
162 // leave incoming alone so we can see response. 161 // leave incoming alone so we can see response.
163 shutdown(cfd, 1); 162 shutdown(cfd, 1);
@@ -166,8 +165,7 @@ repeatyness:
166 ofd = cfd; 165 ofd = cfd;
167 } 166 }
168 167
169 if (bb_full_write(ofd, bb_common_bufsiz1, nread) < 0) 168 xwrite(ofd, bb_common_bufsiz1, nread);
170 bb_perror_msg_and_die(bb_msg_write_error);
171 if (delay > 0) sleep(delay); 169 if (delay > 0) sleep(delay);
172 } 170 }
173 } 171 }
diff --git a/networking/tftp.c b/networking/tftp.c
index b0572c890..dfa599ab5 100644
--- a/networking/tftp.c
+++ b/networking/tftp.c
@@ -249,7 +249,7 @@ static int tftp(const int cmd, const struct hostent *host,
249 block_nr++; 249 block_nr++;
250 250
251 if ((cmd & tftp_cmd_put) && (opcode == TFTP_DATA)) { 251 if ((cmd & tftp_cmd_put) && (opcode == TFTP_DATA)) {
252 len = bb_full_read(localfd, cp, tftp_bufsize - 4); 252 len = full_read(localfd, cp, tftp_bufsize - 4);
253 253
254 if (len < 0) { 254 if (len < 0) {
255 bb_perror_msg(bb_msg_read_error); 255 bb_perror_msg(bb_msg_read_error);
@@ -420,7 +420,7 @@ static int tftp(const int cmd, const struct hostent *host,
420 420
421 if (tmp == block_nr) { 421 if (tmp == block_nr) {
422 422
423 len = bb_full_write(localfd, &buf[4], len - 4); 423 len = full_write(localfd, &buf[4], len - 4);
424 424
425 if (len < 0) { 425 if (len < 0) {
426 bb_perror_msg(bb_msg_write_error); 426 bb_perror_msg(bb_msg_write_error);
diff --git a/shell/ash.c b/shell/ash.c
index de8d06e90..5031ae153 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -3322,7 +3322,7 @@ evalcommand(union node *cmd, int flags)
3322 } 3322 }
3323 sp = arglist.list; 3323 sp = arglist.list;
3324 } 3324 }
3325 bb_full_write(preverrout_fd, "\n", 1); 3325 full_write(preverrout_fd, "\n", 1);
3326 } 3326 }
3327 3327
3328 cmd_is_exec = 0; 3328 cmd_is_exec = 0;
@@ -4559,7 +4559,7 @@ expandhere(union node *arg, int fd)
4559{ 4559{
4560 herefd = fd; 4560 herefd = fd;
4561 expandarg(arg, (struct arglist *)NULL, 0); 4561 expandarg(arg, (struct arglist *)NULL, 0);
4562 bb_full_write(fd, stackblock(), expdest - (char *)stackblock()); 4562 full_write(fd, stackblock(), expdest - (char *)stackblock());
4563} 4563}
4564 4564
4565 4565
@@ -8378,7 +8378,7 @@ growstackstr(void)
8378{ 8378{
8379 size_t len = stackblocksize(); 8379 size_t len = stackblocksize();
8380 if (herefd >= 0 && len >= 1024) { 8380 if (herefd >= 0 && len >= 1024) {
8381 bb_full_write(herefd, stackblock(), len); 8381 full_write(herefd, stackblock(), len);
8382 return stackblock(); 8382 return stackblock();
8383 } 8383 }
8384 growstackblock(); 8384 growstackblock();
@@ -10973,7 +10973,7 @@ openhere(union node *redir)
10973 if (redir->type == NHERE) { 10973 if (redir->type == NHERE) {
10974 len = strlen(redir->nhere.doc->narg.text); 10974 len = strlen(redir->nhere.doc->narg.text);
10975 if (len <= PIPESIZE) { 10975 if (len <= PIPESIZE) {
10976 bb_full_write(pip[1], redir->nhere.doc->narg.text, len); 10976 full_write(pip[1], redir->nhere.doc->narg.text, len);
10977 goto out; 10977 goto out;
10978 } 10978 }
10979 } 10979 }
@@ -10987,7 +10987,7 @@ openhere(union node *redir)
10987#endif 10987#endif
10988 signal(SIGPIPE, SIG_DFL); 10988 signal(SIGPIPE, SIG_DFL);
10989 if (redir->type == NHERE) 10989 if (redir->type == NHERE)
10990 bb_full_write(pip[1], redir->nhere.doc->narg.text, len); 10990 full_write(pip[1], redir->nhere.doc->narg.text, len);
10991 else 10991 else
10992 expandhere(redir->nhere.doc, pip[1]); 10992 expandhere(redir->nhere.doc, pip[1]);
10993 _exit(0); 10993 _exit(0);