diff options
author | Denis Vlasenko | 2006-10-14 02:23:43 +0000 |
---|---|---|
committer | Denis Vlasenko | 2006-10-14 02:23:43 +0000 |
commit | ea62077b850076c4d7dc3cf78ebd1888928c6ddf (patch) | |
tree | 37b7584ae40b99edb5583fbc4392b62ffdadf278 /libbb | |
parent | 88ca06769028e442bf873b270c176ca0e9f021f8 (diff) | |
download | busybox-ea62077b850076c4d7dc3cf78ebd1888928c6ddf.zip busybox-ea62077b850076c4d7dc3cf78ebd1888928c6ddf.tar.gz |
add open_read_close() and similar stuff
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/Kbuild | 4 | ||||
-rw-r--r-- | libbb/full_read.c | 42 | ||||
-rw-r--r-- | libbb/procps.c | 10 | ||||
-rw-r--r-- | libbb/read.c | 133 | ||||
-rw-r--r-- | libbb/safe_read.c | 26 | ||||
-rw-r--r-- | libbb/xfuncs.c | 32 |
6 files changed, 144 insertions, 103 deletions
diff --git a/libbb/Kbuild b/libbb/Kbuild index 27ed68c..3b16f5c 100644 --- a/libbb/Kbuild +++ b/libbb/Kbuild @@ -9,7 +9,7 @@ lib-y:= \ compare_string_array.o concat_path_file.o copy_file.o copyfd.o \ crc32.o create_icmp_socket.o create_icmp6_socket.o \ device_open.o dump.o error_msg.o error_msg_and_die.o \ - find_pid_by_name.o find_root_device.o fgets_str.o full_read.o \ + find_pid_by_name.o find_root_device.o fgets_str.o \ full_write.o get_last_path_component.o get_line_from_file.o \ herror_msg.o herror_msg_and_die.o \ human_readable.o inet_common.o inode_hash.o isdirectory.o \ @@ -19,7 +19,7 @@ lib-y:= \ perror_msg_and_die.o get_console.o \ process_escape_sequence.o procps.o \ recursive_action.o remove_file.o \ - restricted_shell.o run_parts.o run_shell.o safe_read.o safe_write.o \ + restricted_shell.o run_parts.o run_shell.o read.o safe_write.o \ safe_strncpy.o setup_environment.o sha1.o simplify_path.o \ trim.o u_signal_names.o vdprintf.o verror_msg.o \ vherror_msg.o vperror_msg.o wfopen.o xconnect.o xgetcwd.o \ diff --git a/libbb/full_read.c b/libbb/full_read.c deleted file mode 100644 index 068d166..0000000 --- a/libbb/full_read.c +++ /dev/null @@ -1,42 +0,0 @@ -/* vi: set sw=4 ts=4: */ -/* - * Utility routines. - * - * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> - * - * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. - */ - -#include <stdio.h> -#include <unistd.h> -#include "libbb.h" - -/* - * Read all of the supplied buffer from a file. - * This does multiple reads as necessary. - * Returns the amount read, or -1 on an error. - * A short read is returned on an end of file. - */ -ssize_t full_read(int fd, void *buf, size_t len) -{ - ssize_t cc; - ssize_t total; - - total = 0; - - while (len) { - cc = safe_read(fd, buf, len); - - if (cc < 0) - return cc; /* read() returns -1 on failure. */ - - if (cc == 0) - break; - - buf = ((char *)buf) + cc; - total += cc; - len -= cc; - } - - return total; -} diff --git a/libbb/procps.c b/libbb/procps.c index 2bcd2cc..eba9070 100644 --- a/libbb/procps.c +++ b/libbb/procps.c @@ -22,15 +22,9 @@ static int read_to_buf(const char *filename, void *buf) { - int fd; ssize_t ret; - - fd = open(filename, O_RDONLY); - if (fd < 0) - return -1; - ret = read(fd, buf, PROCPS_BUFSIZE-1); - ((char *)buf)[ret > 0 ? ret : 0] = 0; - close(fd); + ret = open_read_close(filename, buf, PROCPS_BUFSIZE-1); + ((char *)buf)[ret > 0 ? ret : 0] = '\0'; return ret; } diff --git a/libbb/read.c b/libbb/read.c new file mode 100644 index 0000000..1c2945f --- /dev/null +++ b/libbb/read.c @@ -0,0 +1,133 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> + * + * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. + */ + +#include "libbb.h" + +ssize_t safe_read(int fd, void *buf, size_t count) +{ + ssize_t n; + + do { + n = read(fd, buf, count); + } while (n < 0 && errno == EINTR); + + return n; +} + +/* + * Read all of the supplied buffer from a file. + * This does multiple reads as necessary. + * Returns the amount read, or -1 on an error. + * A short read is returned on an end of file. + */ +ssize_t full_read(int fd, void *buf, size_t len) +{ + ssize_t cc; + ssize_t total; + + total = 0; + + while (len) { + cc = safe_read(fd, buf, len); + + if (cc < 0) + return cc; /* read() returns -1 on failure. */ + + if (cc == 0) + break; + + buf = ((char *)buf) + cc; + total += cc; + len -= cc; + } + + return total; +} + +// Die with an error message if we can't read the entire buffer. +void xread(int fd, void *buf, size_t count) +{ + if (count) { + ssize_t size = full_read(fd, buf, count); + if (size != count) + bb_error_msg_and_die("short read"); + } +} + +// Die with an error message if we can't read one character. +unsigned char xread_char(int fd) +{ + char tmp; + + xread(fd, &tmp, 1); + + return tmp; +} + +// Read one line a-la fgets. Works only on seekable streams +char *reads(int fd, char *buffer, size_t size) +{ + char *p; + + if (size < 2) + return NULL; + size = full_read(fd, buffer, size-1); + if ((ssize_t)size <= 0) + return NULL; + + buffer[size] = '\0'; + p = strchr(buffer, '\n'); + if (p) { + off_t offset; + *p++ = '\0'; + offset = (p-buffer) - size; + // set fd position the right after the \n + if (offset && lseek(fd, offset, SEEK_CUR) == (off_t)-1) + return NULL; + } + return buffer; +} + +ssize_t read_close(int fd, void *buf, size_t size) +{ + int e; + size = full_read(fd, buf, size); + e = errno; + close(fd); + errno = e; + return size; +} + +ssize_t open_read_close(const char *filename, void *buf, size_t size) +{ + int fd = open(filename, O_RDONLY); + if (fd < 0) + return fd; + return read_close(fd, buf, size); +} + +void *xmalloc_open_read_close(const char *filename, size_t *sizep) +{ + char *buf; + size_t size = sizep ? *sizep : INT_MAX; + int fd = xopen(filename, O_RDONLY); + off_t len = xlseek(fd, 0, SEEK_END); + xlseek(fd, 0, SEEK_SET); + + if (len > size) + bb_error_msg_and_die("file '%s' is too big", filename); + size = len; + buf = xmalloc(size+1); + size = read_close(fd, buf, size); + if ((ssize_t)size < 0) + bb_perror_msg_and_die("'%s'", filename); + buf[size] = '\0'; + if (sizep) *sizep = size; + return buf; +} diff --git a/libbb/safe_read.c b/libbb/safe_read.c deleted file mode 100644 index a59934a..0000000 --- a/libbb/safe_read.c +++ /dev/null @@ -1,26 +0,0 @@ -/* vi: set sw=4 ts=4: */ -/* - * Utility routines. - * - * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> - * - * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. - */ - -#include <stdio.h> -#include <errno.h> -#include <unistd.h> -#include "libbb.h" - - - -ssize_t safe_read(int fd, void *buf, size_t count) -{ - ssize_t n; - - do { - n = read(fd, buf, count); - } while (n < 0 && errno == EINTR); - - return n; -} diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index efc9194..0a5abb8 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c @@ -95,7 +95,7 @@ int xopen(const char *pathname, int flags) if (ENABLE_DEBUG && (flags & O_CREAT)) bb_error_msg_and_die("xopen() with O_CREAT"); - return xopen3(pathname, flags, 0777); + return xopen3(pathname, flags, 0666); } // Die if we can't open a new file and return an fd. @@ -110,16 +110,6 @@ int xopen3(const char *pathname, int flags, int mode) return ret; } -// Die with an error message if we can't read the entire buffer. -void xread(int fd, void *buf, size_t count) -{ - if (count) { - ssize_t size = full_read(fd, buf, count); - if (size != count) - bb_error_msg_and_die("short read"); - } -} - // Die with an error message if we can't write the entire buffer. void xwrite(int fd, void *buf, size_t count) { @@ -131,20 +121,12 @@ void xwrite(int fd, void *buf, size_t count) } // Die with an error message if we can't lseek to the right spot. -void xlseek(int fd, off_t offset, int whence) +off_t xlseek(int fd, off_t offset, int whence) { - if (offset != lseek(fd, offset, whence)) - bb_error_msg_and_die("lseek"); -} - -// Die with an error message if we can't read one character. -unsigned char xread_char(int fd) -{ - char tmp; - - xread(fd, &tmp, 1); - - return tmp; + off_t off = lseek(fd, offset, whence); + if (off == (off_t)-1) + bb_perror_msg_and_die("lseek"); + return off; } // Die with supplied error message if this FILE * has ferror set. @@ -309,7 +291,7 @@ off_t fdlength(int fd) // If we can read from the current location, it's bigger. - if (lseek(fd, pos, 0)>=0 && safe_read(fd, &temp, 1)==1) { + if (lseek(fd, pos, SEEK_SET)>=0 && safe_read(fd, &temp, 1)==1) { if (bottom == top) bottom = top = (top+1) * 2; else bottom = pos; |