From a15cdc358e5f9d1cb517e2b2e490ac209b0dfa17 Mon Sep 17 00:00:00 2001
From: landley <landley@69ca8d6d-28ef-0310-b511-8ec308f3f277>
Date: Sun, 16 Jul 2006 08:14:35 +0000
Subject: Cleaup read() and write() variants, plus a couple of new functions
 like xlseek and fdlength() for the new mkswap.

git-svn-id: svn://busybox.net/trunk/busybox@15703 69ca8d6d-28ef-0310-b511-8ec308f3f277
---
 applets/applets.c                              | 57 ++++++++---------
 archival/ar.c                                  |  2 +-
 archival/gunzip.c                              |  4 +-
 archival/libunarchive/Makefile.in              |  1 -
 archival/libunarchive/archive_xread_all.c      | 21 -------
 archival/libunarchive/archive_xread_all_eof.c  |  2 +-
 archival/libunarchive/check_header_gzip.c      | 14 ++---
 archival/libunarchive/data_extract_to_buffer.c |  2 +-
 archival/libunarchive/decompress_uncompress.c  |  2 +-
 archival/libunarchive/decompress_unzip.c       |  7 +--
 archival/libunarchive/get_header_ar.c          |  4 +-
 archival/libunarchive/get_header_cpio.c        |  5 +-
 archival/libunarchive/get_header_tar.c         | 12 ++--
 archival/libunarchive/get_header_tar_gz.c      |  2 +-
 archival/libunarchive/unpack_ar_archive.c      |  2 +-
 archival/rpm.c                                 |  2 +-
 archival/rpm2cpio.c                            |  6 +-
 archival/tar.c                                 | 12 ++--
 archival/uncompress.c                          |  2 +-
 archival/unzip.c                               | 61 ++++++------------
 console-tools/loadkmap.c                       | 14 ++---
 coreutils/dd.c                                 | 12 +---
 coreutils/tail.c                               |  3 +-
 include/libbb.h                                | 12 ++--
 include/unarchive.h                            |  1 -
 init/init.c                                    |  6 +-
 libbb/change_identity.c                        |  6 +-
 libbb/copyfd.c                                 | 16 ++---
 libbb/create_icmp6_socket.c                    |  2 +-
 libbb/create_icmp_socket.c                     |  2 +-
 libbb/full_read.c                              |  2 +-
 libbb/full_write.c                             |  2 +-
 libbb/loop.c                                   |  1 +
 libbb/xfuncs.c                                 | 85 +++++++++++++++++++++-----
 miscutils/rx.c                                 |  2 +-
 networking/httpd.c                             | 16 ++---
 networking/nc.c                                | 12 ++--
 networking/tftp.c                              |  4 +-
 shell/ash.c                                    | 10 +--
 39 files changed, 204 insertions(+), 224 deletions(-)
 delete mode 100644 archival/libunarchive/archive_xread_all.c

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)
 		bb_error_msg_and_die ("You have no permission to run this applet!");
 
 	  if ((sct->m_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {     /* *both* have to be set for sgid */
-		if (setegid (sct->m_gid))
-		  bb_error_msg_and_die
-			("BusyBox binary has insufficient rights to set proper GID for applet!");
-	  } else
-		setgid (rgid);                  /* no sgid -> drop */
-
-	  if (sct->m_mode & S_ISUID) {
-		if (seteuid (sct->m_uid))
-		  bb_error_msg_and_die
-			("BusyBox binary has insufficient rights to set proper UID for applet!");
-	  } else
-		setuid (ruid);                  /* no suid -> drop */
+		xsetgid(sct->m_gid);
+	  } else xsetgid(rgid);                /* no sgid -> drop */
+
+	  if (sct->m_mode & S_ISUID) xsetuid(sct->m_uid);
+	  else xsetuid(ruid);                  /* no suid -> drop */
 	} else {
 		/* default: drop all privileges */
-	  setgid (rgid);
-	  setuid (ruid);
+	  xsetgid(rgid);
+	  xsetuid(ruid);
 	}
 	return;
   } else {
@@ -374,11 +367,10 @@ static void check_suid (struct BB_applet *applet)
 #endif
 
   if (applet->need_suid == _BB_SUID_ALWAYS) {
-	if (geteuid () != 0)
-	  bb_error_msg_and_die ("This applet requires root privileges!");
+	if (geteuid()) bb_error_msg_and_die("Applet requires root privileges!");
   } else if (applet->need_suid == _BB_SUID_NEVER) {
-	setgid (rgid);                          /* drop all privileges */
-	setuid (ruid);
+	xsetgid(rgid);                          /* drop all privileges */
+	xsetuid(ruid);
   }
 }
 #else
@@ -419,14 +411,14 @@ static const char *unpack_usage_messages(void)
 	case -1: /* error */
 		exit(1);
 	case 0: /* child */
-		bb_full_write(input[1], packed_usage, sizeof(packed_usage));
+		full_write(input[1], packed_usage, sizeof(packed_usage));
 		exit(0);
 	}
 	/* parent */
 	close(input[1]);
 
 	buf = xmalloc(SIZEOF_usage_messages);
-	bb_full_read(output[0], buf, SIZEOF_usage_messages);
+	full_read(output[0], buf, SIZEOF_usage_messages);
 	return buf;
 }
 
@@ -454,33 +446,32 @@ void bb_show_usage (void)
   exit (bb_default_error_retval);
 }
 
-static int applet_name_compare (const void *x, const void *y)
+static int applet_name_compare(const void *name, const void *vapplet)
 {
-  const char *name = x;
-  const struct BB_applet *applet = y;
+  const struct BB_applet *applet = vapplet;
 
-  return strcmp (name, applet->name);
+  return strcmp(name, applet->name);
 }
 
 extern const size_t NUM_APPLETS;
 
-struct BB_applet *find_applet_by_name (const char *name)
+struct BB_applet *find_applet_by_name(const char *name)
 {
-  return bsearch (name, applets, NUM_APPLETS, sizeof (struct BB_applet),
+  return bsearch(name, applets, NUM_APPLETS, sizeof(struct BB_applet),
 				  applet_name_compare);
 }
 
-void run_applet_by_name (const char *name, int argc, char **argv)
+void run_applet_by_name(const char *name, int argc, char **argv)
 {
-	if(ENABLE_FEATURE_SUID_CONFIG) parse_config_file ();
+	if (ENABLE_FEATURE_SUID_CONFIG) parse_config_file();
 
-	if(!strncmp(name, "busybox", 7)) busybox_main(argc, argv);
+	if (!strncmp(name, "busybox", 7)) busybox_main(argc, argv);
 	/* Do a binary search to find the applet entry given the name. */
 	applet_using = find_applet_by_name(name);
-	if(applet_using) {
+	if (applet_using) {
 		bb_applet_name = applet_using->name;
-		if(argc==2 && !strcmp(argv[1], "--help")) bb_show_usage ();
-		if(ENABLE_FEATURE_SUID) check_suid (applet_using);
-		exit ((*(applet_using->main)) (argc, argv));
+		if(argc==2 && !strcmp(argv[1], "--help")) bb_show_usage();
+		if(ENABLE_FEATURE_SUID) check_suid(applet_using);
+		exit((*(applet_using->main))(argc, argv));
 	}
 }
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)
 		llist_add_to(&(archive_handle->accept), argv[optind++]);
 	}
 
-	archive_xread_all(archive_handle, magic, 7);
+	xread(archive_handle->src_fd, magic, 7);
 	if (strncmp(magic, "!<arch>", 7) != 0) {
 		bb_error_msg_and_die("Invalid ar magic");
 	}
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)
 		}
 
 		/* do the decompression, and cleanup */
-		if (bb_xread_char(src_fd) == 0x1f) {
+		if (xread_char(src_fd) == 0x1f) {
 			unsigned char magic2;
 
-			magic2 = bb_xread_char(src_fd);
+			magic2 = xread_char(src_fd);
 #ifdef CONFIG_FEATURE_GUNZIP_UNCOMPRESS
 			if (magic2 == 0x9d) {
 				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:= \
 	header_list.o \
 	header_verbose_list.o \
 \
-	archive_xread_all.o \
 	archive_xread_all_eof.o \
 \
 	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 @@
-/* vi: set sw=4 ts=4: */
-/*
- * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include "unarchive.h"
-#include "libbb.h"
-
-void archive_xread_all(const archive_handle_t *archive_handle, void *buf, const size_t count)
-{
-	ssize_t size;
-
-	size = bb_full_read(archive_handle->src_fd, buf, count);
-	if (size != count) {
-		bb_error_msg_and_die("Short read");
-	}
-	return;
-}
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
 {
 	ssize_t size;
 
-	size = bb_full_read(archive_handle->src_fd, buf, count);
+	size = full_read(archive_handle->src_fd, buf, count);
 	if ((size != 0) && (size != count)) {
 		bb_perror_msg_and_die("Short read, read %ld of %ld", (long)size, (long)count);
 	}
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)
 		} formated;
 	} header;
 
-	bb_xread_all(src_fd, header.raw, 8);
+	xread(src_fd, header.raw, 8);
 
 	/* Check the compression method */
 	if (header.formated.method != 8) {
@@ -32,10 +32,10 @@ void check_header_gzip(int src_fd)
 		/* bit 2 set: extra field present */
 		unsigned char extra_short;
 
-		extra_short = bb_xread_char(src_fd) + (bb_xread_char(src_fd) << 8);
+		extra_short = xread_char(src_fd) + (xread_char(src_fd) << 8);
 		while (extra_short > 0) {
 			/* Ignore extra field */
-			bb_xread_char(src_fd);
+			xread_char(src_fd);
 			extra_short--;
 		}
 	}
@@ -43,19 +43,19 @@ void check_header_gzip(int src_fd)
 	/* Discard original name if any */
 	if (header.formated.flags & 0x08) {
 		/* bit 3 set: original file name present */
-		while(bb_xread_char(src_fd) != 0);
+		while(xread_char(src_fd) != 0);
 	}
 
 	/* Discard file comment if any */
 	if (header.formated.flags & 0x10) {
 		/* bit 4 set: file comment present */
-		while(bb_xread_char(src_fd) != 0);
+		while(xread_char(src_fd) != 0);
 	}
 
 	/* Read the header checksum */
 	if (header.formated.flags & 0x02) {
-		bb_xread_char(src_fd);
-		bb_xread_char(src_fd);
+		xread_char(src_fd);
+		xread_char(src_fd);
 	}
 
 	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)
 
 	archive_handle->buffer = xzalloc(size + 1);
 
-	archive_xread_all(archive_handle, archive_handle->buffer, size);
+	xread(archive_handle->src_fd, archive_handle->buffer, size);
 }
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)
 
 	insize = 0;
 
-	inbuf[0] = bb_xread_char(fd_in);
+	inbuf[0] = xread_char(fd_in);
 
 	maxbits = inbuf[0] & BIT_MASK;
 	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
 			/* Leave the first 4 bytes empty so we can always unwind the bitbuffer
 			 * to the front of the bytebuffer, leave 4 bytes free at end of tail
 			 * so we can easily top up buffer in check_trailer_gzip() */
-			if (!(bytebuffer_size = bb_xread(gunzip_src_fd, &bytebuffer[4], bytebuffer_max - 8))) {
+			if (1 > (bytebuffer_size = safe_read(gunzip_src_fd, &bytebuffer[4], bytebuffer_max - 8)))
 				bb_error_msg_and_die("unexpected end of file");
-			}
 			bytebuffer_size += 4;
 			bytebuffer_offset = 4;
 		}
@@ -862,7 +861,7 @@ int inflate_unzip(int in, int out)
 
 	while(1) {
 		int ret = inflate_get_next_window();
-		nwrote = bb_full_write(out, gunzip_window, gunzip_outbuf_count);
+		nwrote = full_write(out, gunzip_window, gunzip_outbuf_count);
 		if (nwrote == -1) {
 			bb_perror_msg("write");
 			return -1;
@@ -896,7 +895,7 @@ int inflate_gunzip(int in, int out)
 	/* top up the input buffer with the rest of the trailer */
 	count = bytebuffer_size - bytebuffer_offset;
 	if (count < 8) {
-		bb_xread_all(in, &bytebuffer[bytebuffer_size], 8 - count);
+		xread(in, &bytebuffer[bytebuffer_size], 8 - count);
 		bytebuffer_size += 8 - count;
 	}
 	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)
 	if (ar.raw[0] == '\n') {
 		/* fix up the header, we started reading 1 byte too early */
 		memmove(ar.raw, &ar.raw[1], 59);
-		ar.raw[59] = bb_xread_char(archive_handle->src_fd);
+		ar.raw[59] = xread_char(archive_handle->src_fd);
 		archive_handle->offset++;
 	}
 	archive_handle->offset += 60;
@@ -68,7 +68,7 @@ char get_header_ar(archive_handle_t *archive_handle)
 			 * in static variable long_names for use in future entries */
 			ar_long_name_size = typed->size;
 			ar_long_names = xmalloc(ar_long_name_size);
-			bb_xread_all(archive_handle->src_fd, ar_long_names, ar_long_name_size);
+			xread(archive_handle->src_fd, ar_long_names, ar_long_name_size);
 			archive_handle->offset += ar_long_name_size;
 			/* This ar entries data section only contained filenames for other records
 			 * 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)
 	}
 
 	file_header->name = (char *) xzalloc(namesize + 1);
-	archive_xread_all(archive_handle, file_header->name, namesize); /* Read in filename */
+	/* Read in filename */
+	xread(archive_handle->src_fd, file_header->name, namesize);
 	archive_handle->offset += namesize;
 
 	/* Update offset amount and skip padding before file contents */
@@ -103,7 +104,7 @@ char get_header_cpio(archive_handle_t *archive_handle)
 
 	if (S_ISLNK(file_header->mode)) {
 		file_header->link_name = (char *) xzalloc(file_header->size + 1);
-		archive_xread_all(archive_handle, file_header->link_name, file_header->size);
+		xread(archive_handle->src_fd, file_header->link_name, file_header->size);
 		archive_handle->offset += file_header->size;
 		file_header->size = 0; /* Stop possible seeks in future */
 	} 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)
 	/* Align header */
 	data_align(archive_handle, 512);
 
-	if (bb_full_read(archive_handle->src_fd, tar.raw, 512) != 512) {
-		/* Assume end of file */
-		bb_error_msg_and_die("Short header");
-		//return(EXIT_FAILURE);
-	}
+	xread(archive_handle->src_fd, tar.raw, 512);
 	archive_handle->offset += 512;
 
 	/* If there is no filename its an empty header */
@@ -69,7 +65,7 @@ char get_header_tar(archive_handle_t *archive_handle)
 			/* This is the second consecutive empty header! End of archive!
 			 * Read until the end to empty the pipe from gz or bz2
 			 */
-			while (bb_full_read(archive_handle->src_fd, tar.raw, 512) == 512);
+			while (full_read(archive_handle->src_fd, tar.raw, 512) == 512);
 			return(EXIT_FAILURE);
 		}
 		end = 1;
@@ -166,14 +162,14 @@ char get_header_tar(archive_handle_t *archive_handle)
 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
 	case 'L': {
 			longname = xzalloc(file_header->size + 1);
-			archive_xread_all(archive_handle, longname, file_header->size);
+			xread(archive_handle->src_fd, longname, file_header->size);
 			archive_handle->offset += file_header->size;
 
 			return(get_header_tar(archive_handle));
 		}
 	case 'K': {
 			linkname = xzalloc(file_header->size + 1);
-			archive_xread_all(archive_handle, linkname, file_header->size);
+			xread(archive_handle->src_fd, linkname, file_header->size);
 			archive_handle->offset += file_header->size;
 
 			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)
 	/* Cant lseek over pipe's */
 	archive_handle->seek = seek_by_char;
 
-	archive_xread_all(archive_handle, &magic, 2);
+	xread(archive_handle->src_fd, &magic, 2);
 	if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
 		bb_error_msg_and_die("Invalid gzip magic");
 	}
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)
 {
 	char magic[7];
 
-	archive_xread_all(ar_archive, magic, 7);
+	xread(ar_archive->src_fd, magic, 7);
 	if (strncmp(magic, "!<arch>", 7) != 0) {
 		bb_error_msg_and_die("Invalid ar magic");
 	}
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) {
 	archive_handle->src_fd = fd;
 	archive_handle->offset = 0;
 
-	bb_xread_all(archive_handle->src_fd, &magic, 2);
+	xread(archive_handle->src_fd, &magic, 2);
 	if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
 		bb_error_msg_and_die("Invalid gzip magic");
 	}
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)
 {
 	struct rpm_header header;
 
-	bb_xread_all(rpm_fd, &header, sizeof(struct rpm_header));
+	xread(rpm_fd, &header, sizeof(struct rpm_header));
 	if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0) {
 		bb_error_msg_and_die("Invalid RPM header magic"); /* Invalid magic */
 	}
@@ -66,7 +66,7 @@ int rpm2cpio_main(int argc, char **argv)
 		rpm_fd = bb_xopen(argv[1], O_RDONLY);
 	}
 
-	bb_xread_all(rpm_fd, &lead, sizeof(struct rpm_lead));
+	xread(rpm_fd, &lead, sizeof(struct rpm_lead));
 	if (strncmp((char *) &lead.magic, RPM_MAGIC, 4) != 0) {
 		bb_error_msg_and_die("Invalid RPM magic"); /* Just check the magic, the rest is irrelevant */
 	}
@@ -78,7 +78,7 @@ int rpm2cpio_main(int argc, char **argv)
 	/* Skip the main header */
 	skip_header(rpm_fd);
 
-	bb_xread_all(rpm_fd, &magic, 2);
+	xread(rpm_fd, &magic, 2);
 	if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
 		bb_error_msg_and_die("Invalid gzip magic");
 	}
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,
 	putOctal(header.chksum, 7, chksum);
 
 	/* Now write the header out to disk */
-	if ((size =
-		 bb_full_write(tbInfo->tarFd, (char *) &header,
-					sizeof(struct TarHeader))) < 0) {
+	if ((size = full_write(tbInfo->tarFd, (char *) &header,
+					sizeof(struct TarHeader))) < 0)
+	{
 		bb_error_msg(bb_msg_io_error, real_name);
 		return (FALSE);
 	}
@@ -475,7 +475,7 @@ static inline int writeTarFile(const int tar_fd, const int verboseFlag,
 			while (1) {
 				char buf;
 
-				int n = bb_full_read(gzipStatusPipe[0], &buf, 1);
+				int n = full_read(gzipStatusPipe[0], &buf, 1);
 
 				if (n == 0 && vfork_exec_errno != 0) {
 					errno = vfork_exec_errno;
@@ -562,8 +562,8 @@ static char get_header_tar_Z(archive_handle_t *archive_handle)
 	archive_handle->seek = seek_by_char;
 
 	/* do the decompression, and cleanup */
-	if (bb_xread_char(archive_handle->src_fd) != 0x1f ||
-		bb_xread_char(archive_handle->src_fd) != 0x9d)
+	if (xread_char(archive_handle->src_fd) != 0x1f ||
+		xread_char(archive_handle->src_fd) != 0x9d)
 	{
 		bb_error_msg_and_die("Invalid magic");
 	}
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)
 		}
 
 		/* do the decompression, and cleanup */
-		if ((bb_xread_char(src_fd) != 0x1f) || (bb_xread_char(src_fd) != 0x9d)) {
+		if ((xread_char(src_fd) != 0x1f) || (xread_char(src_fd) != 0x9d)) {
 			bb_error_msg_and_die("Invalid magic");
 		}
 
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 @@
 #include "unarchive.h"
 #include "busybox.h"
 
-#if BB_BIG_ENDIAN
-static inline unsigned short
-__swap16(unsigned short x) {
-	return (((uint16_t)(x) & 0xFF) << 8) | (((uint16_t)(x) & 0xFF00) >> 8);
-}
-
-static inline uint32_t
-__swap32(uint32_t x) {
-	 return (((x & 0xFF) << 24) |
-		((x & 0xFF00) << 8) |
-		((x & 0xFF0000) >> 8) |
-		((x & 0xFF000000) >> 24));
-}
-#else /* it's little-endian */
-# define __swap16(x) (x)
-# define __swap32(x) (x)
-#endif /* BB_BIG_ENDIAN */
-
-#define ZIP_FILEHEADER_MAGIC		__swap32(0x04034b50)
-#define ZIP_CDS_MAGIC			__swap32(0x02014b50)
-#define ZIP_CDS_END_MAGIC		__swap32(0x06054b50)
-#define ZIP_DD_MAGIC			__swap32(0x08074b50)
+#define ZIP_FILEHEADER_MAGIC		SWAP_LE32(0x04034b50)
+#define ZIP_CDS_MAGIC			SWAP_LE32(0x02014b50)
+#define ZIP_CDS_END_MAGIC		SWAP_LE32(0x06054b50)
+#define ZIP_DD_MAGIC			SWAP_LE32(0x08074b50)
 
 extern unsigned int gunzip_crc;
 extern unsigned int gunzip_bytes_out;
@@ -83,13 +65,6 @@ static void unzip_skip(int fd, off_t skip)
 	}
 }
 
-static void unzip_read(int fd, void *buf, size_t count)
-{
-	if (bb_xread(fd, buf, count) != count) {
-		bb_error_msg_and_die(bb_msg_read_error);
-	}
-}
-
 static void unzip_create_leading_dirs(char *fn)
 {
 	/* Create all leading directories */
@@ -248,7 +223,7 @@ int unzip_main(int argc, char **argv)
 		unsigned int magic;
 
 		/* Check magic number */
-		unzip_read(src_fd, &magic, 4);
+		xread(src_fd, &magic, 4);
 		if (magic == ZIP_CDS_MAGIC) {
 			break;
 		} else if (magic != ZIP_FILEHEADER_MAGIC) {
@@ -256,19 +231,17 @@ int unzip_main(int argc, char **argv)
 		}
 
 		/* Read the file header */
-		unzip_read(src_fd, zip_header.raw, 26);
-#if BB_BIG_ENDIAN
-		zip_header.formated.version = __swap16(zip_header.formated.version);
-		zip_header.formated.flags = __swap16(zip_header.formated.flags);
-		zip_header.formated.method = __swap16(zip_header.formated.method);
-		zip_header.formated.modtime = __swap16(zip_header.formated.modtime);
-		zip_header.formated.moddate = __swap16(zip_header.formated.moddate);
-		zip_header.formated.crc32 = __swap32(zip_header.formated.crc32);
-		zip_header.formated.cmpsize = __swap32(zip_header.formated.cmpsize);
-		zip_header.formated.ucmpsize = __swap32(zip_header.formated.ucmpsize);
-		zip_header.formated.filename_len = __swap16(zip_header.formated.filename_len);
-		zip_header.formated.extra_len = __swap16(zip_header.formated.extra_len);
-#endif /* BB_BIG_ENDIAN */
+		xread(src_fd, zip_header.raw, 26);
+		zip_header.formated.version = SWAP_LE32(zip_header.formated.version);
+		zip_header.formated.flags = SWAP_LE32(zip_header.formated.flags);
+		zip_header.formated.method = SWAP_LE32(zip_header.formated.method);
+		zip_header.formated.modtime = SWAP_LE32(zip_header.formated.modtime);
+		zip_header.formated.moddate = SWAP_LE32(zip_header.formated.moddate);
+		zip_header.formated.crc32 = SWAP_LE32(zip_header.formated.crc32);
+		zip_header.formated.cmpsize = SWAP_LE32(zip_header.formated.cmpsize);
+		zip_header.formated.ucmpsize = SWAP_LE32(zip_header.formated.ucmpsize);
+		zip_header.formated.filename_len = SWAP_LE32(zip_header.formated.filename_len);
+		zip_header.formated.extra_len = SWAP_LE32(zip_header.formated.extra_len);
 		if ((zip_header.formated.method != 0) && (zip_header.formated.method != 8)) {
 			bb_error_msg_and_die("Unsupported compression method %d", zip_header.formated.method);
 		}
@@ -276,7 +249,7 @@ int unzip_main(int argc, char **argv)
 		/* Read filename */
 		free(dst_fn);
 		dst_fn = xzalloc(zip_header.formated.filename_len + 1);
-		unzip_read(src_fd, dst_fn, zip_header.formated.filename_len);
+		xread(src_fd, dst_fn, zip_header.formated.filename_len);
 
 		/* Skip extra header bytes */
 		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)
 
 	fd = bb_xopen(CURRENT_VC, O_RDWR);
 
-	if ((bb_full_read(0, buff, 7) != 7) || (strncmp(buff, BINARY_KEYMAP_MAGIC, 7) != 0))
+	xread(0, buff, 7);
+	if (strncmp(buff, BINARY_KEYMAP_MAGIC, 7))
 		bb_error_msg_and_die("This is not a valid binary keymap.");
 
-	if (bb_full_read(0, flags, MAX_NR_KEYMAPS) != MAX_NR_KEYMAPS)
-		bb_perror_msg_and_die("Error reading keymap flags");
+	xread(0, flags, MAX_NR_KEYMAPS);
 
 	for (i = 0; i < MAX_NR_KEYMAPS; i++) {
 		if (flags[i] == 1) {
-			bb_full_read(0, ibuff, NR_KEYS * sizeof(u_short));
+			xread(0, ibuff, NR_KEYS * sizeof(u_short));
 			for (j = 0; j < NR_KEYS; j++) {
 				ke.kb_index = j;
 				ke.kb_table = i;
@@ -63,8 +63,6 @@ int loadkmap_main(int argc, char **argv)
 		}
 	}
 
-	/* Don't bother to close files.  Exit does that
-	 * automagically, so we can save a few bytes */
-	/* close(fd); */
-	return EXIT_SUCCESS;
+	if (ENABLE_FEATURE_CLEAN_UP) close(fd);
+	return 0;
 }
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)
 				tmp += d;
 				oc += d;
 				if (oc == obs) {
-					if (bb_full_write(ofd, obuf, obs) < 0) {
-						bb_perror_msg_and_die("%s", outfile);
-					}
+					xwrite(ofd, obuf, obs);
 					out_full++;
 					oc = 0;
 				}
 			}
 		} else {
-			if ((n = bb_full_write(ofd, ibuf, n)) < 0) {
-				bb_perror_msg_and_die("%s", outfile);
-			}
+			xwrite(ofd, ibuf, n);
 			if (n == ibs) out_full++;
 			else out_part++;
 		}
 	}
 	
 	if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
-		if (bb_full_write(ofd, obuf, oc) < 0) {
-			bb_perror_msg_and_die("%s", outfile);
-		}
+		xwrite(ofd, obuf, oc);
 		out_part++;
 	}
 	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)
 static void tail_xbb_full_write(const char *buf, size_t len)
 {
 	/* If we get a write error, there is really no sense in continuing. */
-	if (bb_full_write(STDOUT_FILENO, buf, len) < 0) {
+	if (full_write(STDOUT_FILENO, buf, len) < 0)
 		bb_perror_nomsg_and_die();
-	}
 }
 
 static 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);
 extern int remove_file(const char *path, int flags);
 extern int copy_file(const char *source, const char *dest, int flags);
 extern ssize_t safe_read(int fd, void *buf, size_t count);
-extern ssize_t bb_full_read(int fd, void *buf, size_t len);
+extern ssize_t full_read(int fd, void *buf, size_t len);
 extern ssize_t safe_write(int fd, const void *buf, size_t count);
-extern ssize_t bb_full_write(int fd, const void *buf, size_t len);
+extern ssize_t full_write(int fd, const void *buf, size_t len);
 extern int recursive_action(const char *fileName, int recurse,
 	  int followLinks, int depthFirst,
 	  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);
 extern char *itoa(int n);
 extern void xsetgid(gid_t gid);
 extern void xsetuid(uid_t uid);
+extern off_t fdlength(int fd);
 
 #define BB_GETOPT_ERROR 0x80000000UL
 extern const char *bb_opt_complementally;
@@ -477,9 +478,10 @@ extern int obscure(const char *old, const char *newval, const struct passwd *pwd
 
 extern int bb_xopen(const char *pathname, int flags);
 extern int bb_xopen3(const char *pathname, int flags, int mode);
-extern ssize_t bb_xread(int fd, void *buf, size_t count);
-extern void bb_xread_all(int fd, void *buf, size_t count);
-extern unsigned char bb_xread_char(int fd);
+extern void xread(int fd, void *buf, size_t count);
+extern unsigned char xread_char(int fd);
+extern void xlseek(int fd, off_t offset, int whence);
+extern void xwrite(int fd, void *buf, size_t count);
 
 #ifndef COMM_LEN
 #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);
 extern void seek_by_jump(const archive_handle_t *archive_handle, const unsigned int amount);
 extern void seek_by_char(const archive_handle_t *archive_handle, const unsigned int amount);
 
-extern void archive_xread_all(const archive_handle_t *archive_handle, void *buf, const size_t count);
 extern ssize_t archive_xread_all_eof(archive_handle_t *archive_handle, unsigned char *buf, size_t count);
 
 extern 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, ...)
 		}
 	}
 	if ((device & LOG) && (log_fd >= 0)) {
-		bb_full_write(log_fd, msg, l);
+		full_write(log_fd, msg, l);
 	}
 #endif
 
@@ -230,7 +230,7 @@ static void message(int device, const char *fmt, ...)
 					O_WRONLY | O_NOCTTY | O_NONBLOCK);
 		/* Always send console messages to /dev/console so people will see them. */
 		if (fd >= 0) {
-			bb_full_write(fd, msg, l);
+			full_write(fd, msg, l);
 			close(fd);
 #if ENABLE_DEBUG_INIT
 		/* all descriptors may be closed */
@@ -536,7 +536,7 @@ static pid_t run(const struct init_action *a)
 			messageD(LOG, "Waiting for enter to start '%s'"
 						"(pid %d, terminal %s)\n",
 					  cmdpath, getpid(), a->terminal);
-			bb_full_write(1, press_enter, sizeof(press_enter) - 1);
+			full_write(1, press_enter, sizeof(press_enter) - 1);
 			while(read(0, &c, 1) == 1 && c != '\n')
 				;
 		}
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 )
 		return "cannot set groups";
 	endgrent ( );
 
-	if ( setgid ( pw-> pw_gid ))
-		return "cannot set group id";
-	if ( setuid ( pw->pw_uid ))
-		return "cannot set user id";
+	xsetgid(pw-> pw_gid);
+	xsetuid(pw->pw_uid);
 	return NULL;
 }
 
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)
 	if (src_fd < 0) goto out;
 	while (!size || total < size)
 	{
-		ssize_t wrote, xread;
+		ssize_t wr, rd;
 
-		xread = safe_read(src_fd, buffer,
+		rd = safe_read(src_fd, buffer,
 				(!size || size - total > BUFSIZ) ? BUFSIZ : size - total);
 
-		if (xread > 0) {
+		if (rd > 0) {
 			/* A -1 dst_fd means we need to fake it... */
-			wrote = (dst_fd < 0) ? xread : bb_full_write(dst_fd, buffer, xread);
-			if (wrote < xread) {
+			wr = (dst_fd < 0) ? rd : full_write(dst_fd, buffer, rd);
+			if (wr < rd) {
 				bb_perror_msg(bb_msg_write_error);
 				break;
 			}
-			total += wrote;
+			total += wr;
 			if (total == size) status = 0;
-		} else if (xread < 0) {
+		} else if (rd < 0) {
 			bb_perror_msg(bb_msg_read_error);
 			break;
-		} else if (xread == 0) {
+		} else if (rd == 0) {
 			/* All done. */
 			status = 0;
 			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)
 	}
 
 	/* drop root privs if running setuid */
-	setuid(getuid());
+	xsetuid(getuid());
 
 	return sock;
 }
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)
 	}
 
 	/* drop root privs if running setuid */
-	setuid(getuid());
+	xsetuid(getuid());
 
 	return sock;
 }
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 @@
  * Returns the amount read, or -1 on an error.
  * A short read is returned on an end of file.
  */
-ssize_t bb_full_read(int fd, void *buf, size_t len)
+ssize_t full_read(int fd, void *buf, size_t len)
 {
 	ssize_t cc;
 	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 @@
  * This does multiple writes as necessary.
  * Returns the amount written, or -1 on an error.
  */
-ssize_t bb_full_write(int fd, const void *buf, size_t len)
+ssize_t full_write(int fd, const void *buf, size_t len)
 {
 	ssize_t cc;
 	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 @@
  * Utility routines.
  *
  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
+ * Copyright (C) 2005 by Rob Landley <rob@landley.net>
  *
  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  */
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)
 #endif
 
 #ifdef L_xread
-ssize_t bb_xread(int fd, void *buf, size_t count)
+
+// Die with an error message if we can't read the entire buffer.
+
+void xread(int fd, void *buf, size_t count)
 {
-	ssize_t size;
+	while (count) {
+		ssize_t size;
 
-	size = read(fd, buf, count);
-	if (size < 0) {
-		bb_perror_msg_and_die(bb_msg_read_error);
+		if ((size = safe_read(fd, buf, count)) < 1)
+			bb_error_msg_and_die("Short read");
+		count -= size;
+		buf = ((char *) buf) + size;
 	}
-	return(size);
 }
 #endif
 
-#ifdef L_xread_all
-void bb_xread_all(int fd, void *buf, size_t count)
-{
-	ssize_t size;
+#ifdef L_xwrite
 
+// Die with an error message if we can't write the entire buffer.
+
+void xwrite(int fd, void *buf, size_t count)
+{
 	while (count) {
-		if ((size = bb_xread(fd, buf, count)) == 0) {	/* EOF */
-			bb_error_msg_and_die("Short read");
-		}
+		ssize_t size;
+
+		if ((size = safe_write(fd, buf, count)) < 1)
+			bb_error_msg_and_die("Short write");
 		count -= size;
 		buf = ((char *) buf) + size;
 	}
-	return;
+}
+#endif
+
+#ifdef L_xlseek
+
+// Die if we can't lseek to the right spot.
+
+void xlseek(int fd, off_t offset, int whence)
+{
+	if (whence != lseek(fd, offset, whence)) bb_error_msg_and_die("lseek");
 }
 #endif
 
 #ifdef L_xread_char
-unsigned char bb_xread_char(int fd)
+unsigned char xread_char(int fd)
 {
 	char tmp;
 
-	bb_xread_all(fd, &tmp, 1);
+	xread(fd, &tmp, 1);
 
 	return(tmp);
 }
@@ -294,3 +309,41 @@ void xsetuid(uid_t uid)
 	if (setuid(uid)) bb_error_msg_and_die("setuid");
 }
 #endif
+
+#ifdef L_fdlength
+off_t fdlength(int fd)
+{
+	off_t bottom = 0, top = 0, pos;
+	long size;
+
+	// If the ioctl works for this, return it.
+
+	if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512;
+
+	// If not, do a binary search for the last location we can read.
+
+	do {
+		char temp;
+
+		pos = bottom + (top - bottom) / 2;;
+
+		// If we can read from the current location, it's bigger.
+
+		if (lseek(fd, pos, 0)>=0 && safe_read(fd, &temp, 1)==1) {
+			if (bottom == top) bottom = top = (top+1) * 2;
+			else bottom = pos;
+
+		// If we can't, it's smaller.
+
+		} else {
+			if (bottom == top) {
+				if (!top) return 0;
+				bottom = top/2;
+			}
+			else top = pos;
+		}
+	} while (bottom + 1 != top);
+
+	return pos + 1;
+}
+#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,
 		wantBlockNo++;
 		length += blockLength;
 
-		if (bb_full_write(filefd, blockBuf, blockLength) < 0) {
+		if (full_write(filefd, blockBuf, blockLength) < 0) {
 			note_error("write to file failed: %m");
 			goto fatal;
 		}
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)
 #if DEBUG
   fprintf(stderr, "Headers: '%s'", buf);
 #endif
-  return bb_full_write(a_c_w, buf, len);
+  return full_write(a_c_w, buf, len);
 }
 
 /****************************************************************************
@@ -1222,7 +1222,7 @@ static int sendCgi(const char *url,
 	  break;
 	}
       } else if(post_readed_size > 0 && FD_ISSET(outFd, &writeSet)) {
-		count = bb_full_write(outFd, wbuf + post_readed_idx, post_readed_size);
+		count = full_write(outFd, wbuf + post_readed_idx, post_readed_size);
 		if(count > 0) {
 			post_readed_size -= count;
 			post_readed_idx += count;
@@ -1263,14 +1263,14 @@ static int sendCgi(const char *url,
 	    rbuf[count] = 0;
 	    /* check to see if the user script added headers */
 	    if(strncmp(rbuf, "HTTP/1.0 200 OK\r\n", 4) != 0) {
-	      bb_full_write(s, "HTTP/1.0 200 OK\r\n", 17);
+	      full_write(s, "HTTP/1.0 200 OK\r\n", 17);
 	    }
 	    if (strstr(rbuf, "ontent-") == 0) {
-	      bb_full_write(s, "Content-type: text/plain\r\n\r\n", 28);
+	      full_write(s, "Content-type: text/plain\r\n\r\n", 28);
 	    }
 	    firstLine = 0;
 	  }
-	  if (bb_full_write(s, rbuf, count) != count)
+	  if (full_write(s, rbuf, count) != count)
 	      break;
 
 #if DEBUG
@@ -1337,8 +1337,8 @@ static int sendFile(const char *url)
 	char *buf = config->buf;
 
 	sendHeaders(HTTP_OK);
-	while ((count = bb_full_read(f, buf, MAX_MEMORY_BUFF)) > 0) {
-		if (bb_full_write(a_c_w, buf, count) != count)
+	while ((count = full_read(f, buf, MAX_MEMORY_BUFF)) > 0) {
+		if (full_write(a_c_w, buf, count) != count)
 			break;
 	}
 	close(f);
@@ -2000,7 +2000,7 @@ int httpd_main(int argc, char *argv[])
 # ifdef CONFIG_FEATURE_HTTPD_SETUID
   /* drop privileges */
   if(uid > 0)
-	setuid(uid);
+	xsetuid(uid);
 # endif
 #endif
 
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 @@
 
 #include "busybox.h"
 
-#define xread bb_xread
-
 static void timeout(int signum)
 {
 	bb_error_msg_and_die("Timed out");
@@ -151,13 +149,14 @@ repeatyness:
 
 		for (fd = 0; fd < FD_SETSIZE; fd++) {
 			if (FD_ISSET(fd, &testfds)) {
-				nread = xread(fd, bb_common_bufsiz1, sizeof(bb_common_bufsiz1));
+				nread = safe_read(fd, bb_common_bufsiz1,
+							sizeof(bb_common_bufsiz1));
 
 				if (fd == cfd) {
-					if (!nread) exit(0);
+					if (nread<1) exit(0);
 					ofd = STDOUT_FILENO;
 				} else {
-					if (!nread) {
+					if (nread<1) {
 						// Close outgoing half-connection so they get EOF, but
 						// leave incoming alone so we can see response.
 						shutdown(cfd, 1);
@@ -166,8 +165,7 @@ repeatyness:
 					ofd = cfd;
 				}
 
-				if (bb_full_write(ofd, bb_common_bufsiz1, nread) < 0)
-					bb_perror_msg_and_die(bb_msg_write_error);
+				xwrite(ofd, bb_common_bufsiz1, nread);
 				if (delay > 0) sleep(delay);
 			}
 		}
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,
 			block_nr++;
 
 			if ((cmd & tftp_cmd_put) && (opcode == TFTP_DATA)) {
-				len = bb_full_read(localfd, cp, tftp_bufsize - 4);
+				len = full_read(localfd, cp, tftp_bufsize - 4);
 
 				if (len < 0) {
 					bb_perror_msg(bb_msg_read_error);
@@ -420,7 +420,7 @@ static int tftp(const int cmd, const struct hostent *host,
 
 			if (tmp == block_nr) {
 
-				len = bb_full_write(localfd, &buf[4], len - 4);
+				len = full_write(localfd, &buf[4], len - 4);
 
 				if (len < 0) {
 					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)
 			}
 			sp = arglist.list;
 		}
-		bb_full_write(preverrout_fd, "\n", 1);
+		full_write(preverrout_fd, "\n", 1);
 	}
 
 	cmd_is_exec = 0;
@@ -4559,7 +4559,7 @@ expandhere(union node *arg, int fd)
 {
 	herefd = fd;
 	expandarg(arg, (struct arglist *)NULL, 0);
-	bb_full_write(fd, stackblock(), expdest - (char *)stackblock());
+	full_write(fd, stackblock(), expdest - (char *)stackblock());
 }
 
 
@@ -8378,7 +8378,7 @@ growstackstr(void)
 {
 	size_t len = stackblocksize();
 	if (herefd >= 0 && len >= 1024) {
-		bb_full_write(herefd, stackblock(), len);
+		full_write(herefd, stackblock(), len);
 		return stackblock();
 	}
 	growstackblock();
@@ -10973,7 +10973,7 @@ openhere(union node *redir)
 	if (redir->type == NHERE) {
 		len = strlen(redir->nhere.doc->narg.text);
 		if (len <= PIPESIZE) {
-			bb_full_write(pip[1], redir->nhere.doc->narg.text, len);
+			full_write(pip[1], redir->nhere.doc->narg.text, len);
 			goto out;
 		}
 	}
@@ -10987,7 +10987,7 @@ openhere(union node *redir)
 #endif
 		signal(SIGPIPE, SIG_DFL);
 		if (redir->type == NHERE)
-			bb_full_write(pip[1], redir->nhere.doc->narg.text, len);
+			full_write(pip[1], redir->nhere.doc->narg.text, len);
 		else
 			expandhere(redir->nhere.doc, pip[1]);
 		_exit(0);
-- 
cgit v1.2.3-55-g6feb