small fixes:

fix xstrdup to not grossly overallocate memory
use xopen instean of xopen3 in several places
etc.
This commit is contained in:
Denis Vlasenko 2006-11-26 15:45:17 +00:00
parent c1660fea6d
commit cf749bc10c
9 changed files with 35 additions and 24 deletions

View File

@ -851,7 +851,7 @@ int tar_main(int argc, char **argv)
tar_handle->src_fd = fileno(tar_stream);
tar_handle->seek = seek_by_read;
} else {
tar_handle->src_fd = xopen3(tar_filename, flags, 0666);
tar_handle->src_fd = xopen(tar_filename, flags);
}
}

View File

@ -138,7 +138,7 @@ int dd_main(int argc, char **argv)
if (!seek && (flags & trunc_flag))
oflag |= O_TRUNC;
ofd = xopen3(outfile, oflag, 0666);
ofd = xopen(outfile, oflag);
if (seek && (flags & trunc_flag)) {
if (ftruncate(ofd, seek * obs) < 0) {

View File

@ -57,7 +57,7 @@ char * xstrdup(const char *s)
if (s == NULL)
return NULL;
t = strdup (s);
t = strdup(s);
if (t == NULL)
bb_error_msg_and_die(bb_msg_memory_exhausted);
@ -69,23 +69,33 @@ char * xstrdup(const char *s)
// the (possibly truncated to length n) string into it.
char * xstrndup(const char *s, int n)
{
int m;
char *t;
if (ENABLE_DEBUG && s == NULL)
bb_error_msg_and_die("xstrndup bug");
/* TODO: think about xstrndup("abc", 10000)!!! */
t = xmalloc(++n);
/* We can just xmalloc(n+1) and strncpy into it, */
/* but think about xstrndup("abc", 10000) wastage! */
m = n;
t = (char*) s;
while (m) {
if (!*t) break;
m--; t++;
}
n = n - m;
t = xmalloc(n + 1);
t[n] = '\0';
return safe_strncpy(t,s,n);
return memcpy(t,s,n);
}
// Die if we can't open a file and return a FILE * to it.
// Notice we haven't got xfread(), This is for use with fscanf() and friends.
FILE *xfopen(const char *path, const char *mode)
{
FILE *fp;
if ((fp = fopen(path, mode)) == NULL)
FILE *fp = fopen(path, mode);
if (fp == NULL)
bb_perror_msg_and_die("%s", path);
return fp;
}
@ -93,8 +103,8 @@ FILE *xfopen(const char *path, const char *mode)
// Die if we can't open an existing file and return an fd.
int xopen(const char *pathname, int flags)
{
if (ENABLE_DEBUG && (flags & O_CREAT))
bb_error_msg_and_die("xopen() with O_CREAT");
//if (ENABLE_DEBUG && (flags & O_CREAT))
// bb_error_msg_and_die("xopen() with O_CREAT");
return xopen3(pathname, flags, 0666);
}
@ -142,7 +152,7 @@ off_t xlseek(int fd, off_t offset, int whence)
return off;
}
// Die with supplied error message if this FILE * has ferror set.
// Die with supplied filename if this FILE * has ferror set.
void die_if_ferror(FILE *fp, const char *fn)
{
if (ferror(fp)) {
@ -214,7 +224,6 @@ void xsetenv(const char *key, const char *value)
bb_error_msg_and_die(bb_msg_memory_exhausted);
}
// Converts unsigned long long value into compact 4-char
// representation. Examples: "1234", "1.2k", " 27M", "123T"
// Fifth char is always '\0'
@ -257,7 +266,6 @@ void smart_ulltoa5(unsigned long long ul, char buf[5])
buf[4] = '\0';
}
// Convert unsigned integer to ascii, writing into supplied buffer. A
// truncated result is always null terminated (unless buflen is 0), and
// contains the first few digits of the result ala strncpy.

View File

@ -156,6 +156,7 @@ int crontab_main(int ac, char **av)
break;
case EDIT:
{
/* FIXME: messy code here! we have file copying helpers for this! */
FILE *fi;
int fd;
int n;
@ -163,11 +164,12 @@ int crontab_main(int ac, char **av)
snprintf(tmp, sizeof(tmp), TMPDIR "/crontab.%d", getpid());
fd = xopen3(tmp, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600);
/* race, use fchown */
chown(tmp, getuid(), getgid());
fi = fopen(pas->pw_name, "r");
if (fi) {
while ((n = fread(buf, 1, sizeof(buf), fi)) > 0)
write(fd, buf, n);
full_write(fd, buf, n);
}
EditFile(caller, tmp);
remove(tmp);
@ -178,6 +180,7 @@ int crontab_main(int ac, char **av)
/* fall through */
case REPLACE:
{
/* same here */
char path[1024];
int fd;
int n;
@ -186,7 +189,7 @@ int crontab_main(int ac, char **av)
fd = open(path, O_CREAT|O_TRUNC|O_APPEND|O_WRONLY, 0600);
if (fd >= 0) {
while ((n = read(repFd, buf, sizeof(buf))) > 0) {
write(fd, buf, n);
full_write(fd, buf, n);
}
close(fd);
rename(path, pas->pw_name);

View File

@ -263,7 +263,7 @@ int rx_main(int argc, char **argv)
fn = argv[1];
ttyfd = xopen(CURRENT_TTY, O_RDWR);
filefd = xopen3(fn, O_RDWR|O_CREAT|O_TRUNC, 0666);
filefd = xopen(fn, O_RDWR|O_CREAT|O_TRUNC);
if (tcgetattr(ttyfd, &tty) < 0)
bb_perror_msg_and_die("tcgetattr");

View File

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

View File

@ -452,8 +452,8 @@ int wget_main(int argc, char **argv)
/* Do it before progressmeter (want to have nice error message) */
if (output_fd < 0)
output_fd = xopen3(fname_out,
O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0666);
output_fd = xopen(fname_out,
O_WRONLY|O_CREAT|O_EXCL|O_TRUNC);
if (!(opt & WGET_OPT_QUIET))
progressmeter(-1);

View File

@ -148,19 +148,19 @@ static unsigned processorstart(struct logdir *ld)
if (fd_move(0, fd) == -1)
bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name);
ld->fnsave[26] = 't';
fd = xopen3(ld->fnsave, O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT, 0644);
fd = xopen(ld->fnsave, O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT);
if (fd_move(1, fd) == -1)
bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name);
fd = open_read("state");
if (fd == -1) {
if (errno != ENOENT)
bb_perror_msg_and_die(FATAL"cannot %s processor %s", "open state for", ld->name);
close(xopen3("state", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT, 0644));
close(xopen("state", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT));
fd = xopen("state", O_RDONLY|O_NDELAY);
}
if (fd_move(4, fd) == -1)
bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name);
fd = xopen3("newstate", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT, 0644);
fd = xopen("newstate", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT);
if (fd_move(5, fd) == -1)
bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name);

View File

@ -73,7 +73,7 @@ static void make_device(char *path, int delete)
line++;
/* find end of this line */
for(end=pos; end-conf<len && *end!='\n'; end++)
for (end=pos; end-conf<len && *end!='\n'; end++)
;
/* Three fields: regex, uid:gid, mode */
@ -111,7 +111,7 @@ static void make_device(char *path, int delete)
char *s, *s2;
/* Find : */
for(s=pos; s<end2 && *s!=':'; s++)
for (s=pos; s<end2 && *s!=':'; s++)
;
if (s == end2) break;