add open_read_close() and similar stuff

This commit is contained in:
Denis Vlasenko 2006-10-14 02:23:43 +00:00
parent 88ca067690
commit ea62077b85
38 changed files with 228 additions and 234 deletions

View File

@ -79,7 +79,7 @@ char get_header_ar(archive_handle_t *archive_handle)
(saved in variable long_name) that conatains the real filename */ (saved in variable long_name) that conatains the real filename */
const unsigned int long_offset = atoi(&ar.formatted.name[1]); const unsigned int long_offset = atoi(&ar.formatted.name[1]);
if (long_offset >= ar_long_name_size) { if (long_offset >= ar_long_name_size) {
bb_error_msg_and_die("Cant resolve long filename"); bb_error_msg_and_die("can't resolve long filename");
} }
typed->name = xstrdup(ar_long_names + long_offset); typed->name = xstrdup(ar_long_names + long_offset);
} }

View File

@ -18,23 +18,23 @@ int open_transformer(int src_fd,
int pid; int pid;
if (pipe(fd_pipe) != 0) { if (pipe(fd_pipe) != 0) {
bb_perror_msg_and_die("Can't create pipe"); bb_perror_msg_and_die("can't create pipe");
} }
pid = fork(); pid = fork();
if (pid == -1) { if (pid == -1) {
bb_perror_msg_and_die("Fork failed"); bb_perror_msg_and_die("fork failed");
} }
if (pid == 0) { if (pid == 0) {
/* child process */ /* child process */
close(fd_pipe[0]); /* We don't wan't to read from the parent */ close(fd_pipe[0]); /* We don't wan't to read from the parent */
// FIXME: error check? // FIXME: error check?
transformer(src_fd, fd_pipe[1]); transformer(src_fd, fd_pipe[1]);
close(fd_pipe[1]); /* Send EOF */ close(fd_pipe[1]); /* Send EOF */
close(src_fd); close(src_fd);
exit(0); exit(0);
/* notreached */ /* notreached */
} }
/* parent process */ /* parent process */

View File

@ -457,7 +457,7 @@ static int writeTarFile(const int tar_fd, const int verboseFlag,
if (n == 0 && vfork_exec_errno != 0) { if (n == 0 && vfork_exec_errno != 0) {
errno = vfork_exec_errno; errno = vfork_exec_errno;
bb_perror_msg_and_die("Could not exec %s", zip_exec); bb_perror_msg_and_die("cannot exec %s", zip_exec);
} else if ((n < 0) && (errno == EAGAIN || errno == EINTR)) } else if ((n < 0) && (errno == EAGAIN || errno == EINTR))
continue; /* try it again */ continue; /* try it again */
break; break;

View File

@ -1202,7 +1202,7 @@ int diff_main(int argc, char **argv)
* driver routine. Both drivers use the contents of stb1 and stb2. * driver routine. Both drivers use the contents of stb1 and stb2.
*/ */
if (argc < 2) { if (argc < 2) {
bb_error_msg("Missing filename"); bb_error_msg("missing filename");
bb_show_usage(); bb_show_usage();
} }
if (strcmp(argv[0], "-") == 0) { if (strcmp(argv[0], "-") == 0) {
@ -1216,12 +1216,12 @@ int diff_main(int argc, char **argv)
} else } else
xstat(argv[1], &stb2); xstat(argv[1], &stb2);
if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode))) if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode)))
bb_error_msg_and_die("Can't compare - to a directory"); bb_error_msg_and_die("can't compare - to a directory");
if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) { if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
#if ENABLE_FEATURE_DIFF_DIR #if ENABLE_FEATURE_DIFF_DIR
diffdir(argv[0], argv[1]); diffdir(argv[0], argv[1]);
#else #else
bb_error_msg_and_die("Directory comparison not supported"); bb_error_msg_and_die("directory comparison not supported");
#endif #endif
} else { } else {
if (S_ISDIR(stb1.st_mode)) { if (S_ISDIR(stb1.st_mode)) {

View File

@ -2353,7 +2353,7 @@ static int file_insert(Byte * fn, Byte * p, int size)
#endif #endif
fd = open((char *) fn, O_RDONLY); // try read-only fd = open((char *) fn, O_RDONLY); // try read-only
if (fd < 0) { if (fd < 0) {
psbs("\"%s\" %s", fn, "could not open file"); psbs("\"%s\" %s", fn, "cannot open file");
goto fi0; goto fi0;
} }
#ifdef CONFIG_FEATURE_VI_READONLY #ifdef CONFIG_FEATURE_VI_READONLY
@ -2367,11 +2367,11 @@ static int file_insert(Byte * fn, Byte * p, int size)
if (cnt < 0) { if (cnt < 0) {
cnt = -1; cnt = -1;
p = text_hole_delete(p, p + size - 1); // un-do buffer insert p = text_hole_delete(p, p + size - 1); // un-do buffer insert
psbs("could not read file \"%s\"", fn); psbs("cannot read file \"%s\"", fn);
} else if (cnt < size) { } else if (cnt < size) {
// There was a partial read, shrink unused space text[] // There was a partial read, shrink unused space text[]
p = text_hole_delete(p + cnt, p + (size - cnt) - 1); // un-do buffer insert p = text_hole_delete(p + cnt, p + (size - cnt) - 1); // un-do buffer insert
psbs("could not read all of file \"%s\"", fn); psbs("cannot read all of file \"%s\"", fn);
} }
if (cnt >= size) if (cnt >= size)
file_modified++; file_modified++;

View File

@ -205,12 +205,22 @@ extern int is_directory(const char *name, int followLinks, struct stat *statBuf)
extern DIR *warn_opendir(const char *path); extern DIR *warn_opendir(const char *path);
extern DIR *xopendir(const char *path); extern DIR *xopendir(const char *path);
extern ssize_t safe_read(int fd, void *buf, size_t count);
extern ssize_t full_read(int fd, void *buf, size_t count);
extern void xread(int fd, void *buf, size_t count);
extern unsigned char xread_char(int fd);
extern char *reads(int fd, char *buf, size_t count);
ssize_t read_close(int fd, void *buf, size_t count);
ssize_t open_read_close(const char *filename, void *buf, size_t count);
void *xmalloc_open_read_close(const char *filename, size_t *sizep);
extern ssize_t safe_write(int fd, const void *buf, size_t count);
extern ssize_t full_write(int fd, const void *buf, size_t count);
extern void xwrite(int fd, void *buf, size_t count);
extern int remove_file(const char *path, int flags); extern int remove_file(const char *path, int flags);
extern int copy_file(const char *source, const char *dest, 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 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 full_write(int fd, const void *buf, size_t len);
extern int recursive_action(const char *fileName, int recurse, extern int recursive_action(const char *fileName, int recurse,
int followLinks, int depthFirst, int followLinks, int depthFirst,
int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData), int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData),
@ -592,10 +602,7 @@ extern int obscure(const char *old, const char *newval, const struct passwd *pwd
extern void xsetenv(const char *key, const char *value); extern void xsetenv(const char *key, const char *value);
extern int xopen(const char *pathname, int flags); extern int xopen(const char *pathname, int flags);
extern int xopen3(const char *pathname, int flags, int mode); extern int xopen3(const char *pathname, int flags, int mode);
extern void xread(int fd, void *buf, size_t count); extern off_t xlseek(int fd, off_t offset, int whence);
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);
extern const char bb_uuenc_tbl_base64[]; extern const char bb_uuenc_tbl_base64[];
extern const char bb_uuenc_tbl_std[]; extern const char bb_uuenc_tbl_std[];

View File

@ -561,7 +561,7 @@ static pid_t run(const struct init_action *a)
execv(cmdpath, cmd); execv(cmdpath, cmd);
/* We're still here? Some error happened. */ /* We're still here? Some error happened. */
message(LOG | CONSOLE, "Bummer, could not run '%s': %m", cmdpath); message(LOG | CONSOLE, "Bummer, cannot run '%s': %m", cmdpath);
_exit(-1); _exit(-1);
} }
sigprocmask(SIG_SETMASK, &omask, NULL); sigprocmask(SIG_SETMASK, &omask, NULL);

View File

@ -9,7 +9,7 @@ lib-y:= \
compare_string_array.o concat_path_file.o copy_file.o copyfd.o \ compare_string_array.o concat_path_file.o copy_file.o copyfd.o \
crc32.o create_icmp_socket.o create_icmp6_socket.o \ crc32.o create_icmp_socket.o create_icmp6_socket.o \
device_open.o dump.o error_msg.o error_msg_and_die.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 \ full_write.o get_last_path_component.o get_line_from_file.o \
herror_msg.o herror_msg_and_die.o \ herror_msg.o herror_msg_and_die.o \
human_readable.o inet_common.o inode_hash.o isdirectory.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 \ perror_msg_and_die.o get_console.o \
process_escape_sequence.o procps.o \ process_escape_sequence.o procps.o \
recursive_action.o remove_file.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 \ safe_strncpy.o setup_environment.o sha1.o simplify_path.o \
trim.o u_signal_names.o vdprintf.o verror_msg.o \ trim.o u_signal_names.o vdprintf.o verror_msg.o \
vherror_msg.o vperror_msg.o wfopen.o xconnect.o xgetcwd.o \ vherror_msg.o vperror_msg.o wfopen.o xconnect.o xgetcwd.o \

View File

@ -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;
}

View File

@ -22,15 +22,9 @@
static int read_to_buf(const char *filename, void *buf) static int read_to_buf(const char *filename, void *buf)
{ {
int fd;
ssize_t ret; ssize_t ret;
ret = open_read_close(filename, buf, PROCPS_BUFSIZE-1);
fd = open(filename, O_RDONLY); ((char *)buf)[ret > 0 ? ret : 0] = '\0';
if (fd < 0)
return -1;
ret = read(fd, buf, PROCPS_BUFSIZE-1);
((char *)buf)[ret > 0 ? ret : 0] = 0;
close(fd);
return ret; return ret;
} }

133
libbb/read.c Normal file
View File

@ -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;
}

View File

@ -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;
}

View File

@ -95,7 +95,7 @@ int xopen(const char *pathname, int flags)
if (ENABLE_DEBUG && (flags & O_CREAT)) if (ENABLE_DEBUG && (flags & O_CREAT))
bb_error_msg_and_die("xopen() with 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. // 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; 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. // Die with an error message if we can't write the entire buffer.
void xwrite(int fd, void *buf, size_t count) 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. // 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)) off_t off = lseek(fd, offset, whence);
bb_error_msg_and_die("lseek"); if (off == (off_t)-1)
} bb_perror_msg_and_die("lseek");
return off;
// 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;
} }
// Die with supplied error message if this FILE * has ferror set. // 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 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; if (bottom == top) bottom = top = (top+1) * 2;
else bottom = pos; else bottom = pos;

View File

@ -70,7 +70,7 @@ static void del_line_matching(const char *login, const char *filename)
fclose(passwd); fclose(passwd);
} }
} else { } else {
bb_error_msg("Can't find '%s' in '%s'", login, filename); bb_error_msg("can't find '%s' in '%s'", login, filename);
} }
free(buffer); free(buffer);
} }

View File

@ -120,7 +120,7 @@ static void crondlog(const char *ctl, ...)
close(logfd); close(logfd);
#if ENABLE_DEBUG_CROND_OPTION #if ENABLE_DEBUG_CROND_OPTION
} else { } else {
bb_perror_msg("Can't open log file"); bb_perror_msg("can't open log file");
#endif #endif
} }
} }
@ -924,7 +924,7 @@ static void RunJob(const char *user, CronLine * line)
line->cl_MailFlag = 1; line->cl_MailFlag = 1;
fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", user, fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", user,
line->cl_Shell); line->cl_Shell);
line->cl_MailPos = lseek(mailFd, 0, 1); line->cl_MailPos = lseek(mailFd, 0, SEEK_CUR);
} else { } else {
crondlog("\024unable to create mail file user %s file %s, output to /dev/null\n", user, mailFile); crondlog("\024unable to create mail file user %s file %s, output to /dev/null\n", user, mailFile);
} }

View File

@ -171,7 +171,7 @@ int crontab_main(int ac, char **av)
} }
EditFile(caller, tmp); EditFile(caller, tmp);
remove(tmp); remove(tmp);
lseek(fd, 0L, 0); lseek(fd, 0L, SEEK_SET);
repFd = fd; repFd = fd;
} }
option = REPLACE; option = REPLACE;

View File

@ -187,7 +187,7 @@ int makedevs_main(int argc, char **argv)
sprintf(full_name_inc, "%s%d", full_name, i); sprintf(full_name_inc, "%s%d", full_name, i);
rdev = (major << 8) + minor + (i * increment - start); rdev = (major << 8) + minor + (i * increment - start);
if (mknod(full_name_inc, mode, rdev) == -1) { if (mknod(full_name_inc, mode, rdev) == -1) {
bb_perror_msg("line %d: could not create node %s", linenum, full_name_inc); bb_perror_msg("line %d: cannot create node %s", linenum, full_name_inc);
ret = EXIT_FAILURE; ret = EXIT_FAILURE;
} }
else if (chown(full_name_inc, uid, gid) == -1) { else if (chown(full_name_inc, uid, gid) == -1) {
@ -203,7 +203,7 @@ int makedevs_main(int argc, char **argv)
} else { } else {
rdev = (major << 8) + minor; rdev = (major << 8) + minor;
if (mknod(full_name, mode, rdev) == -1) { if (mknod(full_name, mode, rdev) == -1) {
bb_perror_msg("line %d: could not create node %s", linenum, full_name); bb_perror_msg("line %d: cannot create node %s", linenum, full_name);
ret = EXIT_FAILURE; ret = EXIT_FAILURE;
} }
else if (chown(full_name, uid, gid) == -1) { else if (chown(full_name, uid, gid) == -1) {

View File

@ -88,12 +88,7 @@ static void put_question_marks(int count)
static int readfile_z(char *buf, int sz, const char* fname) static int readfile_z(char *buf, int sz, const char* fname)
{ {
int fd; sz = open_read_close(fname, buf, sz-1);
fd = xopen(fname, O_RDONLY);
// We are not checking for short reads (valid only because
// we are reading /proc files)
sz = read(fd, buf, sz-1);
close(fd);
if (sz < 0) { if (sz < 0) {
buf[0] = '\0'; buf[0] = '\0';
return 1; return 1;
@ -776,15 +771,12 @@ int nmeter_main(int argc, char* argv[])
s_stat *last = NULL; s_stat *last = NULL;
s_stat *s; s_stat *s;
char *cur, *prev; char *cur, *prev;
int fd;
if (argc != 2) if (argc != 2)
bb_show_usage(); bb_show_usage();
fd = xopen("/proc/version", O_RDONLY); if (open_read_close("/proc/version", buf, sizeof(buf)) > 0)
if (read(fd, buf, sizeof(buf)) > 0) is26 = (strstr(buf, " 2.4.")==NULL);
is26 = (strstr(buf, "Linux version 2.4.")==NULL);
close(fd);
// Can use argv[1] directly, but this will mess up // Can use argv[1] directly, but this will mess up
// parameters as seen by e.g. ps. Making a copy... // parameters as seen by e.g. ps. Making a copy...

View File

@ -4186,10 +4186,10 @@ int insmod_main( int argc, char **argv)
m_addr = create_module(m_name, m_size); m_addr = create_module(m_name, m_size);
if (m_addr == -1) switch (errno) { if (m_addr == -1) switch (errno) {
case EEXIST: case EEXIST:
bb_error_msg("A module named %s already exists", m_name); bb_error_msg("a module named %s already exists", m_name);
goto out; goto out;
case ENOMEM: case ENOMEM:
bb_error_msg("Can't allocate kernel memory for module; needed %lu bytes", bb_error_msg("can't allocate kernel memory for module; needed %lu bytes",
m_size); m_size);
goto out; goto out;
default: default:

View File

@ -147,9 +147,8 @@ int lsmod_main(int argc, char **argv)
check_tainted(); check_tainted();
#if defined(CONFIG_FEATURE_LSMOD_PRETTY_2_6_OUTPUT) #if defined(CONFIG_FEATURE_LSMOD_PRETTY_2_6_OUTPUT)
{ {
char line[4096]; char *line;
while ((line = xmalloc_fgets(file)) != NULL) {
while (fgets(line, sizeof(line), file)) {
char *tok; char *tok;
tok = strtok(line, " \t"); tok = strtok(line, " \t");
@ -175,7 +174,8 @@ int lsmod_main(int argc, char **argv)
tok = ""; tok = "";
printf(" %s", tok); printf(" %s", tok);
} }
printf("\n"); puts("");
free(line);
} }
fclose(file); fclose(file);
} }

View File

@ -93,35 +93,6 @@ static int parse_tag_value ( char *buffer, char **ptag, char **pvalue )
return 1; return 1;
} }
/* Jump through hoops to simulate how fgets() grabs just one line at a
* time... Don't use any stdio since modprobe gets called from a kernel
* thread and stdio junk can overflow the limited stack...
*/
static char *reads ( int fd, char *buffer, size_t len )
{
int n = read ( fd, buffer, len );
if ( n > 0 ) {
char *p;
buffer [len-1] = 0;
p = strchr ( buffer, '\n' );
if ( p ) {
off_t offset;
offset = lseek ( fd, 0L, SEEK_CUR ); // Get the current file descriptor offset
lseek ( fd, offset-n + (p-buffer) + 1, SEEK_SET ); // Set the file descriptor offset to right after the \n
p[1] = 0;
}
return buffer;
}
else
return 0;
}
/* /*
* This function appends an option to a list * This function appends an option to a list
*/ */
@ -913,7 +884,7 @@ int modprobe_main(int argc, char** argv)
depend = build_dep ( ); depend = build_dep ( );
if ( !depend ) if ( !depend )
bb_error_msg_and_die ( "could not parse modules.dep" ); bb_error_msg_and_die ( "cannot parse modules.dep" );
if (remove_opt) { if (remove_opt) {
do { do {

View File

@ -140,7 +140,7 @@ static int godaemon(void)
case 0: case 0:
pw = getpwnam(nobodystr); pw = getpwnam(nobodystr);
if (pw == NULL) if (pw == NULL)
bb_error_msg_and_die("Cannot find uid/gid of user '%s'", nobodystr); bb_error_msg_and_die("cannot find uid/gid of user '%s'", nobodystr);
nobody = pw->pw_uid; nobody = pw->pw_uid;
nogrp = pw->pw_gid; nogrp = pw->pw_gid;
writepid(nobody, nogrp); writepid(nobody, nogrp);

View File

@ -166,7 +166,7 @@ int ftp_receive(ftp_host_info_t *server, FILE *control_stream,
if (do_continue) { if (do_continue) {
fd_local = xopen(local_path, O_APPEND | O_WRONLY); fd_local = xopen(local_path, O_APPEND | O_WRONLY);
} else { } else {
fd_local = xopen3(local_path, O_CREAT | O_TRUNC | O_WRONLY, 0777); fd_local = xopen3(local_path, O_CREAT | O_TRUNC | O_WRONLY, 0666);
} }
} }

View File

@ -1371,7 +1371,7 @@ inetd_main(int argc, char *argv[])
socklen_t plen = sizeof(peer); socklen_t plen = sizeof(peer);
if (getpeername(ctrl, (struct sockaddr *) &peer, &plen) < 0) { if (getpeername(ctrl, (struct sockaddr *) &peer, &plen) < 0) {
bb_error_msg("could not getpeername"); bb_error_msg("cannot getpeername");
close(ctrl); close(ctrl);
continue; continue;
} }

View File

@ -126,7 +126,7 @@ int rtnl_dump_filter(struct rtnl_handle *rth,
int (*junk)(struct sockaddr_nl *,struct nlmsghdr *n, void *), int (*junk)(struct sockaddr_nl *,struct nlmsghdr *n, void *),
void *arg2) void *arg2)
{ {
char buf[8192]; char buf[8192];
struct sockaddr_nl nladdr; struct sockaddr_nl nladdr;
struct iovec iov = { buf, sizeof(buf) }; struct iovec iov = { buf, sizeof(buf) };

View File

@ -496,7 +496,6 @@ static void unix_do_one(int nr, const char *line)
static void do_info(const char *file, const char *name, void (*proc)(int, const char *)) static void do_info(const char *file, const char *name, void (*proc)(int, const char *))
{ {
char buffer[8192];
int lnr = 0; int lnr = 0;
FILE *procinfo; FILE *procinfo;
@ -505,12 +504,15 @@ static void do_info(const char *file, const char *name, void (*proc)(int, const
if (errno != ENOENT) { if (errno != ENOENT) {
perror(file); perror(file);
} else { } else {
bb_error_msg("no support for `%s' on this system", name); bb_error_msg("no support for '%s' on this system", name);
} }
} else { } else {
do { do {
if (fgets(buffer, sizeof(buffer), procinfo)) char *buffer = xmalloc_fgets(procinfo);
if (buffer) {
(proc)(lnr++, buffer); (proc)(lnr++, buffer);
free(buffer);
}
} while (!feof(procinfo)); } while (!feof(procinfo));
fclose(procinfo); fclose(procinfo);
} }

View File

@ -417,7 +417,7 @@ ifaddrlist(struct IFADDRLIST **ipaddrp)
++nipaddr; ++nipaddr;
} }
if (nipaddr == 0) if (nipaddr == 0)
bb_error_msg_and_die ("Can't find any network interfaces"); bb_error_msg_and_die ("can't find any network interfaces");
(void)close(fd); (void)close(fd);
*ipaddrp = st_ifaddrlist; *ipaddrp = st_ifaddrlist;
@ -472,7 +472,7 @@ findsaddr(const struct sockaddr_in *to, struct sockaddr_in *from)
fclose(f); fclose(f);
if (device[0] == '\0') if (device[0] == '\0')
bb_error_msg_and_die ("Can't find interface"); bb_error_msg_and_die ("can't find interface");
/* Get the interface address list */ /* Get the interface address list */
n = ifaddrlist(&al); n = ifaddrlist(&al);
@ -482,7 +482,7 @@ findsaddr(const struct sockaddr_in *to, struct sockaddr_in *from)
if (strcmp(device, al->device) == 0) if (strcmp(device, al->device) == 0)
break; break;
if (i <= 0) if (i <= 0)
bb_error_msg_and_die("Can't find interface %s", device); bb_error_msg_and_die("can't find interface %s", device);
setsin(from, al->addr); setsin(from, al->addr);
} }
@ -1156,7 +1156,7 @@ traceroute_main(int argc, char *argv[])
if (strcmp(device, al->device) == 0) if (strcmp(device, al->device) == 0)
break; break;
if (i <= 0) { if (i <= 0) {
bb_error_msg_and_die("Can't find interface %s", device); bb_error_msg_and_die("can't find interface %s", device);
} }
} }

View File

@ -49,7 +49,7 @@ int arpping(uint32_t yiaddr, uint32_t ip, uint8_t *mac, char *interface)
} }
if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)) == -1) { if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)) == -1) {
bb_perror_msg("Could not setsocketopt on raw socket"); bb_perror_msg("cannot setsocketopt on raw socket");
close(s); close(s);
return -1; return -1;
} }

View File

@ -39,16 +39,13 @@ unsigned long random_xid(void)
{ {
static int initialized; static int initialized;
if (!initialized) { if (!initialized) {
int fd;
unsigned long seed; unsigned long seed;
fd = open("/dev/urandom", 0); if (open_read_close("/dev/urandom", &seed, sizeof(seed)) < 0) {
if (fd < 0 || read(fd, &seed, sizeof(seed)) < 0) { bb_info_msg("Cannot load seed "
bb_info_msg("Could not load seed "
"from /dev/urandom: %s", strerror(errno)); "from /dev/urandom: %s", strerror(errno));
seed = time(0); seed = time(0);
} }
if (fd >= 0) close(fd);
srand(seed); srand(seed);
initialized++; initialized++;
} }

View File

@ -171,6 +171,6 @@ int add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data)
} }
} }
bb_error_msg("Could not add option 0x%02x", code); bb_error_msg("cannot add option 0x%02x", code);
return 0; return 0;
} }

View File

@ -36,7 +36,7 @@ static int signal_pipe[2];
static void signal_handler(int sig) static void signal_handler(int sig)
{ {
if (send(signal_pipe[1], &sig, sizeof(sig), MSG_DONTWAIT) < 0) if (send(signal_pipe[1], &sig, sizeof(sig), MSG_DONTWAIT) < 0)
bb_perror_msg("Could not send signal"); bb_perror_msg("cannot send signal");
} }

View File

@ -219,9 +219,7 @@ int wget_main(int argc, char **argv)
} else if (opt & WGET_OPT_CONTINUE) { } else if (opt & WGET_OPT_CONTINUE) {
output_fd = open(fname_out, O_WRONLY); output_fd = open(fname_out, O_WRONLY);
if (output_fd >= 0) { if (output_fd >= 0) {
beg_range = lseek(output_fd, 0, SEEK_END); beg_range = xlseek(output_fd, 0, SEEK_END);
if (beg_range == (off_t)-1)
bb_perror_msg_and_die("lseek");
} }
/* File doesn't exist. We do not create file here yet. /* File doesn't exist. We do not create file here yet.
We are not sure it exists on remove side */ We are not sure it exists on remove side */

View File

@ -292,7 +292,7 @@ static int fuser_kill_pid_list(pid_list *plist, int sig)
if(curr->pid > 0 && curr->pid != mypid) { if(curr->pid > 0 && curr->pid != mypid) {
if (kill(curr->pid, sig) != 0) { if (kill(curr->pid, sig) != 0) {
bb_perror_msg( bb_perror_msg(
"Could not kill pid '%d'", curr->pid); "cannot kill pid '%d'", curr->pid);
success = 0; success = 0;
} }
} }
@ -342,7 +342,7 @@ int fuser_main(int argc, char **argv)
if(!fuser_file_to_dev_inode( if(!fuser_file_to_dev_inode(
argv[fni[i]], &dev, &inode)) { argv[fni[i]], &dev, &inode)) {
if (ENABLE_FEATURE_CLEAN_UP) free(inodes); if (ENABLE_FEATURE_CLEAN_UP) free(inodes);
bb_perror_msg_and_die("Could not open '%s'", argv[fni[i]]); bb_perror_msg_and_die("cannot open '%s'", argv[fni[i]]);
} }
fuser_add_inode(inodes, dev, inode); fuser_add_inode(inodes, dev, inode);
} }

View File

@ -448,7 +448,7 @@ int top_main(int argc, char **argv)
memcpy(top + n, p, sizeof(procps_status_t)); memcpy(top + n, p, sizeof(procps_status_t));
} }
if (ntop == 0) { if (ntop == 0) {
bb_error_msg_and_die("Can't find process info in /proc"); bb_error_msg_and_die("can't find process info in /proc");
} }
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
if (!prev_hist_count) { if (!prev_hist_count) {

View File

@ -2954,7 +2954,7 @@ static int iosetup(struct ioword *iop, int pipein, int pipeout)
case IOWRITE | IOCAT: case IOWRITE | IOCAT:
if ((u = open(cp, 1)) >= 0) { if ((u = open(cp, 1)) >= 0) {
lseek(u, (long) 0, 2); lseek(u, (long) 0, SEEK_END);
break; break;
} }
case IOWRITE: case IOWRITE:
@ -4686,7 +4686,7 @@ static void pushio(struct ioarg *argp, int (*fn) (struct ioarg *))
/* This line appears to be active when running scripts from command line */ /* This line appears to be active when running scripts from command line */
if ((isatty(e.iop->argp->afile) == 0) if ((isatty(e.iop->argp->afile) == 0)
&& (e.iop == &iostack[0] && (e.iop == &iostack[0]
|| lseek(e.iop->argp->afile, 0L, 1) != -1)) { || lseek(e.iop->argp->afile, 0L, SEEK_CUR) != -1)) {
if (++bufid == AFID_NOBUF) /* counter rollover check, AFID_NOBUF = 11111111 */ if (++bufid == AFID_NOBUF) /* counter rollover check, AFID_NOBUF = 11111111 */
bufid = AFID_ID; /* AFID_ID = 0 */ bufid = AFID_ID; /* AFID_ID = 0 */
@ -4831,7 +4831,7 @@ static int filechar(struct ioarg *ap)
if ((i = ap->afid != bp->id) || bp->bufp == bp->ebufp) { if ((i = ap->afid != bp->id) || bp->bufp == bp->ebufp) {
if (i) if (i)
lseek(ap->afile, ap->afpos, 0); lseek(ap->afile, ap->afpos, SEEK_SET);
i = safe_read(ap->afile, bp->buf, sizeof(bp->buf)); i = safe_read(ap->afile, bp->buf, sizeof(bp->buf));

View File

@ -51,11 +51,11 @@ static time_t read_rtc(int utc)
if (( rtc = open ( "/dev/rtc", O_RDONLY )) < 0 ) { if (( rtc = open ( "/dev/rtc", O_RDONLY )) < 0 ) {
if (( rtc = open ( "/dev/misc/rtc", O_RDONLY )) < 0 ) if (( rtc = open ( "/dev/misc/rtc", O_RDONLY )) < 0 )
bb_perror_msg_and_die ( "Could not access RTC" ); bb_perror_msg_and_die ( "cannot access RTC" );
} }
memset ( &tm, 0, sizeof( struct tm )); memset ( &tm, 0, sizeof( struct tm ));
if ( ioctl ( rtc, RTC_RD_TIME, &tm ) < 0 ) if ( ioctl ( rtc, RTC_RD_TIME, &tm ) < 0 )
bb_perror_msg_and_die ( "Could not read time from RTC" ); bb_perror_msg_and_die ( "cannot read time from RTC" );
tm.tm_isdst = -1; /* not known */ tm.tm_isdst = -1; /* not known */
close ( rtc ); close ( rtc );
@ -85,14 +85,14 @@ static void write_rtc(time_t t, int utc)
if (( rtc = open ( "/dev/rtc", O_WRONLY )) < 0 ) { if (( rtc = open ( "/dev/rtc", O_WRONLY )) < 0 ) {
if (( rtc = open ( "/dev/misc/rtc", O_WRONLY )) < 0 ) if (( rtc = open ( "/dev/misc/rtc", O_WRONLY )) < 0 )
bb_perror_msg_and_die ( "Could not access RTC" ); bb_perror_msg_and_die ( "cannot access RTC" );
} }
tm = *( utc ? gmtime ( &t ) : localtime ( &t )); tm = *( utc ? gmtime ( &t ) : localtime ( &t ));
tm.tm_isdst = 0; tm.tm_isdst = 0;
if ( ioctl ( rtc, RTC_SET_TIME, &tm ) < 0 ) if ( ioctl ( rtc, RTC_SET_TIME, &tm ) < 0 )
bb_perror_msg_and_die ( "Could not set the RTC time" ); bb_perror_msg_and_die ( "cannot set the RTC time" );
close ( rtc ); close ( rtc );
} }

View File

@ -39,10 +39,8 @@ static void make_device(char *path, int delete)
if (!delete) { if (!delete) {
strcat(path, "/dev"); strcat(path, "/dev");
fd = open(path, O_RDONLY); len = open_read_close(path, temp + 1, 64);
len = read(fd, temp + 1, 64);
*temp++ = 0; *temp++ = 0;
close(fd);
if (len < 1) return; if (len < 1) return;
} }

View File

@ -44,9 +44,9 @@ static const char defaultpro[] = "/proc/profile";
int readprofile_main(int argc, char **argv) int readprofile_main(int argc, char **argv)
{ {
FILE *map; FILE *map;
int proFd;
const char *mapFile, *proFile, *mult=0; const char *mapFile, *proFile, *mult=0;
unsigned long len=0, indx=1; unsigned long indx=1;
size_t len;
uint64_t add0=0; uint64_t add0=0;
unsigned int step; unsigned int step;
unsigned int *buf, total, fn_len; unsigned int *buf, total, fn_len;
@ -97,20 +97,8 @@ int readprofile_main(int argc, char **argv)
/* /*
* Use an fd for the profiling buffer, to skip stdio overhead * Use an fd for the profiling buffer, to skip stdio overhead
*/ */
len = INT_MAX;
proFd = xopen(proFile,O_RDONLY); buf = xmalloc_open_read_close(proFile, &len);
if (((int)(len=lseek(proFd,0,SEEK_END)) < 0)
|| (lseek(proFd,0,SEEK_SET) < 0))
bb_perror_msg_and_die(proFile);
buf = xmalloc(len);
if (read(proFd,buf,len) != len)
bb_perror_msg_and_die(proFile);
close(proFd);
if (!optNative) { if (!optNative) {
int entries = len/sizeof(*buf); int entries = len/sizeof(*buf);
int big = 0,small = 0,i; int big = 0,small = 0,i;
@ -123,8 +111,8 @@ int readprofile_main(int argc, char **argv)
small++; small++;
} }
if (big > small) { if (big > small) {
bb_error_msg("Assuming reversed byte order. " bb_error_msg("assuming reversed byte order, "
"Use -n to force native byte order."); "use -n to force native byte order");
for (p = buf; p < buf+entries; p++) for (p = buf; p < buf+entries; p++)
for (i = 0; i < sizeof(*buf)/2; i++) { for (i = 0; i < sizeof(*buf)/2; i++) {
unsigned char *b = (unsigned char *) p; unsigned char *b = (unsigned char *) p;