mirror of https://github.com/mirror/busybox.git
libbb: added xfdopen_for_read/write
function old new delta xfdopen_helper - 40 +40 logdir_open 1163 1184 +21 process_stdin 433 443 +10 xfdopen_for_write - 9 +9 doCommands 2465 2474 +9 patch_main 1214 1222 +8 bbunpack 457 465 +8 xfdopen_for_read - 7 +7 scan_tree 258 262 +4 xstrtoul_range_sfx 230 231 +1 sendmail_main 957 955 -2 passwd_main 1027 1023 -4 parse 969 964 -5 test_main 253 247 -6 sed_main 655 649 -6 dos2unix_main 437 429 -8 fbsplash_main 950 938 -12 handle_dir_common 371 354 -17 expand_vars_to_list 2197 2169 -28 update_passwd 1275 1246 -29 ------------------------------------------------------------------------------ (add/remove: 3/0 grow/shrink: 7/10 up/down: 117/-117) Total: 0 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>1_16_stable
parent
647553a4fc
commit
a7ccdeef39
|
@ -42,10 +42,10 @@ static void convert(char *fn, int conv_type)
|
||||||
i = mkstemp(temp_fn);
|
i = mkstemp(temp_fn);
|
||||||
if (i == -1
|
if (i == -1
|
||||||
|| fchmod(i, st.st_mode) == -1
|
|| fchmod(i, st.st_mode) == -1
|
||||||
|| !(out = fdopen(i, "w+"))
|
|
||||||
) {
|
) {
|
||||||
bb_simple_perror_msg_and_die(temp_fn);
|
bb_simple_perror_msg_and_die(temp_fn);
|
||||||
}
|
}
|
||||||
|
out = xfdopen_for_write(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((i = fgetc(in)) != EOF) {
|
while ((i = fgetc(in)) != EOF) {
|
||||||
|
|
|
@ -374,9 +374,7 @@ void blkid_read_cache(blkid_cache cache)
|
||||||
DBG(DEBUG_CACHE, printf("reading cache file %s\n",
|
DBG(DEBUG_CACHE, printf("reading cache file %s\n",
|
||||||
cache->bic_filename));
|
cache->bic_filename));
|
||||||
|
|
||||||
file = fdopen(fd, "r");
|
file = xfdopen_for_read(fd);
|
||||||
if (!file)
|
|
||||||
goto errout;
|
|
||||||
|
|
||||||
while (fgets(buf, sizeof(buf), file)) {
|
while (fgets(buf, sizeof(buf), file)) {
|
||||||
blkid_dev dev;
|
blkid_dev dev;
|
||||||
|
|
|
@ -95,7 +95,7 @@ int blkid_flush_cache(blkid_cache cache)
|
||||||
sprintf(tmp, "%s-XXXXXX", filename);
|
sprintf(tmp, "%s-XXXXXX", filename);
|
||||||
fd = mkstemp(tmp);
|
fd = mkstemp(tmp);
|
||||||
if (fd >= 0) {
|
if (fd >= 0) {
|
||||||
file = fdopen(fd, "w");
|
file = xfdopen_for_write(fd);
|
||||||
opened = tmp;
|
opened = tmp;
|
||||||
}
|
}
|
||||||
fchmod(fd, 0644);
|
fchmod(fd, 0644);
|
||||||
|
|
|
@ -1338,7 +1338,7 @@ int sed_main(int argc UNUSED_PARAM, char **argv)
|
||||||
nonstdoutfd = mkstemp(G.outname);
|
nonstdoutfd = mkstemp(G.outname);
|
||||||
if (-1 == nonstdoutfd)
|
if (-1 == nonstdoutfd)
|
||||||
bb_perror_msg_and_die("can't create temp file %s", G.outname);
|
bb_perror_msg_and_die("can't create temp file %s", G.outname);
|
||||||
G.nonstdout = fdopen(nonstdoutfd, "w");
|
G.nonstdout = xfdopen_for_write(nonstdoutfd);
|
||||||
|
|
||||||
/* Set permissions/owner of output file */
|
/* Set permissions/owner of output file */
|
||||||
fstat(fileno(file), &statbuf);
|
fstat(fileno(file), &statbuf);
|
||||||
|
|
|
@ -658,21 +658,23 @@ extern char *xmalloc_fgetline(FILE *file) FAST_FUNC RETURNS_MALLOC;
|
||||||
/* Same, but doesn't try to conserve space (may have some slack after the end) */
|
/* Same, but doesn't try to conserve space (may have some slack after the end) */
|
||||||
/* extern char *xmalloc_fgetline_fast(FILE *file) FAST_FUNC RETURNS_MALLOC; */
|
/* extern char *xmalloc_fgetline_fast(FILE *file) FAST_FUNC RETURNS_MALLOC; */
|
||||||
|
|
||||||
extern void die_if_ferror(FILE *file, const char *msg) FAST_FUNC;
|
void die_if_ferror(FILE *file, const char *msg) FAST_FUNC;
|
||||||
extern void die_if_ferror_stdout(void) FAST_FUNC;
|
void die_if_ferror_stdout(void) FAST_FUNC;
|
||||||
extern int fflush_all(void) FAST_FUNC;
|
int fflush_all(void) FAST_FUNC;
|
||||||
extern void fflush_stdout_and_exit(int retval) NORETURN FAST_FUNC;
|
void fflush_stdout_and_exit(int retval) NORETURN FAST_FUNC;
|
||||||
extern int fclose_if_not_stdin(FILE *file) FAST_FUNC;
|
int fclose_if_not_stdin(FILE *file) FAST_FUNC;
|
||||||
extern FILE *xfopen(const char *filename, const char *mode) FAST_FUNC;
|
FILE* xfopen(const char *filename, const char *mode) FAST_FUNC;
|
||||||
/* Prints warning to stderr and returns NULL on failure: */
|
/* Prints warning to stderr and returns NULL on failure: */
|
||||||
extern FILE *fopen_or_warn(const char *filename, const char *mode) FAST_FUNC;
|
FILE* fopen_or_warn(const char *filename, const char *mode) FAST_FUNC;
|
||||||
/* "Opens" stdin if filename is special, else just opens file: */
|
/* "Opens" stdin if filename is special, else just opens file: */
|
||||||
extern FILE *xfopen_stdin(const char *filename) FAST_FUNC;
|
FILE* xfopen_stdin(const char *filename) FAST_FUNC;
|
||||||
extern FILE *fopen_or_warn_stdin(const char *filename) FAST_FUNC;
|
FILE* fopen_or_warn_stdin(const char *filename) FAST_FUNC;
|
||||||
extern FILE* fopen_for_read(const char *path) FAST_FUNC;
|
FILE* fopen_for_read(const char *path) FAST_FUNC;
|
||||||
extern FILE* xfopen_for_read(const char *path) FAST_FUNC;
|
FILE* xfopen_for_read(const char *path) FAST_FUNC;
|
||||||
extern FILE* fopen_for_write(const char *path) FAST_FUNC;
|
FILE* fopen_for_write(const char *path) FAST_FUNC;
|
||||||
extern FILE* xfopen_for_write(const char *path) FAST_FUNC;
|
FILE* xfopen_for_write(const char *path) FAST_FUNC;
|
||||||
|
FILE* xfdopen_for_read(int fd) FAST_FUNC;
|
||||||
|
FILE* xfdopen_for_write(int fd) FAST_FUNC;
|
||||||
|
|
||||||
int bb_pstrcmp(const void *a, const void *b) /* not FAST_FUNC! */;
|
int bb_pstrcmp(const void *a, const void *b) /* not FAST_FUNC! */;
|
||||||
void qsort_string_vector(char **sv, unsigned count) FAST_FUNC;
|
void qsort_string_vector(char **sv, unsigned count) FAST_FUNC;
|
||||||
|
|
|
@ -137,12 +137,7 @@ int FAST_FUNC update_passwd(const char *filename,
|
||||||
fchown(new_fd, sb.st_uid, sb.st_gid);
|
fchown(new_fd, sb.st_uid, sb.st_gid);
|
||||||
}
|
}
|
||||||
errno = 0;
|
errno = 0;
|
||||||
new_fp = fdopen(new_fd, "w");
|
new_fp = xfdopen_for_write(new_fd);
|
||||||
if (!new_fp) {
|
|
||||||
bb_perror_nomsg();
|
|
||||||
close(new_fd);
|
|
||||||
goto unlink_new;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Backup file is "/etc/passwd-" */
|
/* Backup file is "/etc/passwd-" */
|
||||||
*sfx_char = '-';
|
*sfx_char = '-';
|
||||||
|
|
|
@ -38,3 +38,19 @@ FILE* FAST_FUNC xfopen_for_write(const char *path)
|
||||||
{
|
{
|
||||||
return xfopen(path, "w");
|
return xfopen(path, "w");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static FILE* xfdopen_helper(unsigned fd_and_rw_bit)
|
||||||
|
{
|
||||||
|
FILE* fp = fdopen(fd_and_rw_bit >> 1, fd_and_rw_bit & 1 ? "w" : "r");
|
||||||
|
if (!fp)
|
||||||
|
bb_error_msg_and_die(bb_msg_memory_exhausted);
|
||||||
|
return fp;
|
||||||
|
}
|
||||||
|
FILE* FAST_FUNC xfdopen_for_read(int fd)
|
||||||
|
{
|
||||||
|
return xfdopen_helper(fd << 1);
|
||||||
|
}
|
||||||
|
FILE* FAST_FUNC xfdopen_for_write(int fd)
|
||||||
|
{
|
||||||
|
return xfdopen_helper((fd << 1) + 1);
|
||||||
|
}
|
||||||
|
|
|
@ -293,7 +293,7 @@ static int parse(const char *boundary, char **argv)
|
||||||
}
|
}
|
||||||
// parent dumps to fd[1]
|
// parent dumps to fd[1]
|
||||||
close(fd[0]);
|
close(fd[0]);
|
||||||
fp = fdopen(fd[1], "w");
|
fp = xfdopen_for_write(fd[1]);
|
||||||
signal(SIGPIPE, SIG_IGN); // ignore EPIPE
|
signal(SIGPIPE, SIG_IGN); // ignore EPIPE
|
||||||
// or create a file for dump
|
// or create a file for dump
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -93,7 +93,7 @@ int sendmail_main(int argc UNUSED_PARAM, char **argv)
|
||||||
|
|
||||||
// save initial stdin since body is piped!
|
// save initial stdin since body is piped!
|
||||||
xdup2(STDIN_FILENO, 3);
|
xdup2(STDIN_FILENO, 3);
|
||||||
G.fp0 = fdopen(3, "r");
|
G.fp0 = xfdopen_for_read(3);
|
||||||
|
|
||||||
// parse options
|
// parse options
|
||||||
// -f is required. -H and -S are mutually exclusive
|
// -f is required. -H and -S are mutually exclusive
|
||||||
|
|
|
@ -227,7 +227,7 @@ static void fb_drawimage(void)
|
||||||
int fd = open_zipped(G.image_filename);
|
int fd = open_zipped(G.image_filename);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
bb_simple_perror_msg_and_die(G.image_filename);
|
bb_simple_perror_msg_and_die(G.image_filename);
|
||||||
theme_file = fdopen(fd, "r");
|
theme_file = xfdopen_for_read(fd);
|
||||||
}
|
}
|
||||||
head = xmalloc(256);
|
head = xmalloc(256);
|
||||||
|
|
||||||
|
|
|
@ -384,11 +384,7 @@ static void write_out_dep_bb(int fd)
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
|
|
||||||
/* We want good error reporting. fdprintf is not good enough. */
|
/* We want good error reporting. fdprintf is not good enough. */
|
||||||
fp = fdopen(fd, "w");
|
fp = xfdopen_for_write(fd);
|
||||||
if (!fp) {
|
|
||||||
close(fd);
|
|
||||||
goto err;
|
|
||||||
}
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (modinfo[i].pathname) {
|
while (modinfo[i].pathname) {
|
||||||
fprintf(fp, "%s%s%s\n" "%s%s\n",
|
fprintf(fp, "%s%s%s\n" "%s%s\n",
|
||||||
|
|
|
@ -691,9 +691,7 @@ handle_dir_common(int opts)
|
||||||
/* -n prevents user/groupname display,
|
/* -n prevents user/groupname display,
|
||||||
* which can be problematic in chroot */
|
* which can be problematic in chroot */
|
||||||
ls_fd = popen_ls((opts & LONG_LISTING) ? "-l" : "-1");
|
ls_fd = popen_ls((opts & LONG_LISTING) ? "-l" : "-1");
|
||||||
ls_fp = fdopen(ls_fd, "r");
|
ls_fp = xfdopen_for_read(ls_fd);
|
||||||
if (!ls_fp) /* never happens. paranoia */
|
|
||||||
bb_perror_msg_and_die("fdopen");
|
|
||||||
|
|
||||||
if (opts & USE_CTRL_CONN) {
|
if (opts & USE_CTRL_CONN) {
|
||||||
/* STAT <filename> */
|
/* STAT <filename> */
|
||||||
|
|
|
@ -1056,8 +1056,8 @@ static int popen2(FILE **in, FILE **out, char *command, char *param)
|
||||||
/* parent */
|
/* parent */
|
||||||
close(infd.rd);
|
close(infd.rd);
|
||||||
close(outfd.wr);
|
close(outfd.wr);
|
||||||
*in = fdopen(infd.wr, "w");
|
*in = xfdopen_for_write(infd.wr);
|
||||||
*out = fdopen(outfd.rd, "r");
|
*out = xfdopen_for_read(outfd.rd);
|
||||||
return pid;
|
return pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -761,8 +761,8 @@ static NOINLINE unsigned logdir_open(struct logdir *ld, const char *fn)
|
||||||
}
|
}
|
||||||
while ((ld->fdcur = open("current", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600)) == -1)
|
while ((ld->fdcur = open("current", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600)) == -1)
|
||||||
pause2cannot("open current", ld->name);
|
pause2cannot("open current", ld->name);
|
||||||
/* we presume this cannot fail */
|
while ((ld->filecur = fdopen(ld->fdcur, "a")) == NULL)
|
||||||
ld->filecur = fdopen(ld->fdcur, "a"); ////
|
pause2cannot("open current", ld->name); ////
|
||||||
setvbuf(ld->filecur, NULL, _IOFBF, linelen); ////
|
setvbuf(ld->filecur, NULL, _IOFBF, linelen); ////
|
||||||
|
|
||||||
close_on_exec_on(ld->fdcur);
|
close_on_exec_on(ld->fdcur);
|
||||||
|
|
15
shell/hush.c
15
shell/hush.c
|
@ -5301,25 +5301,22 @@ static FILE *generate_stream_from_string(const char *s, pid_t *pid_p)
|
||||||
free(to_free);
|
free(to_free);
|
||||||
# endif
|
# endif
|
||||||
close(channel[1]);
|
close(channel[1]);
|
||||||
//TODO: libbb: fdopen_or_die?
|
close_on_exec_on(channel[0]);
|
||||||
return fdopen(channel[0], "r");
|
return xfdopen_for_read(channel[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Return code is exit status of the process that is run. */
|
/* Return code is exit status of the process that is run. */
|
||||||
static int process_command_subs(o_string *dest, const char *s)
|
static int process_command_subs(o_string *dest, const char *s)
|
||||||
{
|
{
|
||||||
FILE *pf;
|
FILE *fp;
|
||||||
struct in_str pipe_str;
|
struct in_str pipe_str;
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
int status, ch, eol_cnt;
|
int status, ch, eol_cnt;
|
||||||
|
|
||||||
pf = generate_stream_from_string(s, &pid);
|
fp = generate_stream_from_string(s, &pid);
|
||||||
if (pf == NULL)
|
|
||||||
return 1;
|
|
||||||
close_on_exec_on(fileno(pf));
|
|
||||||
|
|
||||||
/* Now send results of command back into original context */
|
/* Now send results of command back into original context */
|
||||||
setup_file_in_str(&pipe_str, pf);
|
setup_file_in_str(&pipe_str, fp);
|
||||||
eol_cnt = 0;
|
eol_cnt = 0;
|
||||||
while ((ch = i_getch(&pipe_str)) != EOF) {
|
while ((ch = i_getch(&pipe_str)) != EOF) {
|
||||||
if (ch == '\n') {
|
if (ch == '\n') {
|
||||||
|
@ -5334,7 +5331,7 @@ static int process_command_subs(o_string *dest, const char *s)
|
||||||
}
|
}
|
||||||
|
|
||||||
debug_printf("done reading from `cmd` pipe, closing it\n");
|
debug_printf("done reading from `cmd` pipe, closing it\n");
|
||||||
fclose(pf);
|
fclose(fp);
|
||||||
/* We need to extract exitcode. Test case
|
/* We need to extract exitcode. Test case
|
||||||
* "true; echo `sleep 1; false` $?"
|
* "true; echo `sleep 1; false` $?"
|
||||||
* should print 1 */
|
* should print 1 */
|
||||||
|
|
Loading…
Reference in New Issue