diff options
author | Rob Landley <rob@landley.net> | 2006-07-16 12:14:35 +0400 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2006-07-16 12:14:35 +0400 |
commit | 534374755d618c9c36c9940c82756241c4b25a67 (patch) | |
tree | fac906b4fa40a68c53cecf20215a7a25b3b1cab6 /libbb | |
parent | afb94ecf2bb6c53ce2a381d6ce45a426243c76d9 (diff) |
Cleaup read() and write() variants, plus a couple of new functions like
xlseek and fdlength() for the new mkswap.
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/change_identity.c | 6 | ||||
-rw-r--r-- | libbb/copyfd.c | 16 | ||||
-rw-r--r-- | libbb/create_icmp6_socket.c | 2 | ||||
-rw-r--r-- | libbb/create_icmp_socket.c | 2 | ||||
-rw-r--r-- | libbb/full_read.c | 2 | ||||
-rw-r--r-- | libbb/full_write.c | 2 | ||||
-rw-r--r-- | libbb/loop.c | 1 | ||||
-rw-r--r-- | libbb/xfuncs.c | 85 |
8 files changed, 84 insertions, 32 deletions
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 |