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);
}
}
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) {
if (s == NULL)
return NULL;
- t = strdup (s);
+ t = strdup(s);
if (t == NULL)
bb_error_msg_and_die(bb_msg_memory_exhausted);
// 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;
}
// 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);
}
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)) {
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'
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.
break;
case EDIT:
{
+/* FIXME: messy code here! we have file copying helpers for this! */
FILE *fi;
int fd;
int n;
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);
/* fall through */
case REPLACE:
{
+/* same here */
char path[1024];
int fd;
int n;
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);
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");
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);
}
}
/* 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);
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);
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 */
char *s, *s2;
/* Find : */
- for(s=pos; s<end2 && *s!=':'; s++)
+ for (s=pos; s<end2 && *s!=':'; s++)
;
if (s == end2) break;