aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2009-11-15 23:28:11 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2009-11-15 23:28:11 +0100
commita7ccdeef396700d1ed78b9f97de0d10c706b169f (patch)
tree4ef45efdaaac81d2a4c31b16d9cc36ba2d3a22e1
parent647553a4fcbbc169b4390d9ef8e4657f0ffe1a5f (diff)
downloadbusybox-w32-a7ccdeef396700d1ed78b9f97de0d10c706b169f.tar.gz
busybox-w32-a7ccdeef396700d1ed78b9f97de0d10c706b169f.tar.bz2
busybox-w32-a7ccdeef396700d1ed78b9f97de0d10c706b169f.zip
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>
-rw-r--r--coreutils/dos2unix.c2
-rw-r--r--e2fsprogs/old_e2fsprogs/blkid/read.c4
-rw-r--r--e2fsprogs/old_e2fsprogs/blkid/save.c2
-rw-r--r--editors/sed.c2
-rw-r--r--include/libbb.h28
-rw-r--r--libbb/update_passwd.c7
-rw-r--r--libbb/wfopen.c16
-rw-r--r--mailutils/mime.c2
-rw-r--r--mailutils/sendmail.c2
-rw-r--r--miscutils/fbsplash.c2
-rw-r--r--modutils/modprobe-small.c6
-rw-r--r--networking/ftpd.c4
-rw-r--r--networking/ifupdown.c4
-rw-r--r--runit/svlogd.c4
-rw-r--r--shell/hush.c15
15 files changed, 51 insertions, 49 deletions
diff --git a/coreutils/dos2unix.c b/coreutils/dos2unix.c
index 309cbc3b8..baf879e54 100644
--- a/coreutils/dos2unix.c
+++ b/coreutils/dos2unix.c
@@ -42,10 +42,10 @@ static void convert(char *fn, int conv_type)
42 i = mkstemp(temp_fn); 42 i = mkstemp(temp_fn);
43 if (i == -1 43 if (i == -1
44 || fchmod(i, st.st_mode) == -1 44 || fchmod(i, st.st_mode) == -1
45 || !(out = fdopen(i, "w+"))
46 ) { 45 ) {
47 bb_simple_perror_msg_and_die(temp_fn); 46 bb_simple_perror_msg_and_die(temp_fn);
48 } 47 }
48 out = xfdopen_for_write(i);
49 } 49 }
50 50
51 while ((i = fgetc(in)) != EOF) { 51 while ((i = fgetc(in)) != EOF) {
diff --git a/e2fsprogs/old_e2fsprogs/blkid/read.c b/e2fsprogs/old_e2fsprogs/blkid/read.c
index 67bc8ee44..f795a5d14 100644
--- a/e2fsprogs/old_e2fsprogs/blkid/read.c
+++ b/e2fsprogs/old_e2fsprogs/blkid/read.c
@@ -374,9 +374,7 @@ void blkid_read_cache(blkid_cache cache)
374 DBG(DEBUG_CACHE, printf("reading cache file %s\n", 374 DBG(DEBUG_CACHE, printf("reading cache file %s\n",
375 cache->bic_filename)); 375 cache->bic_filename));
376 376
377 file = fdopen(fd, "r"); 377 file = xfdopen_for_read(fd);
378 if (!file)
379 goto errout;
380 378
381 while (fgets(buf, sizeof(buf), file)) { 379 while (fgets(buf, sizeof(buf), file)) {
382 blkid_dev dev; 380 blkid_dev dev;
diff --git a/e2fsprogs/old_e2fsprogs/blkid/save.c b/e2fsprogs/old_e2fsprogs/blkid/save.c
index 3600260e2..e60cca445 100644
--- a/e2fsprogs/old_e2fsprogs/blkid/save.c
+++ b/e2fsprogs/old_e2fsprogs/blkid/save.c
@@ -95,7 +95,7 @@ int blkid_flush_cache(blkid_cache cache)
95 sprintf(tmp, "%s-XXXXXX", filename); 95 sprintf(tmp, "%s-XXXXXX", filename);
96 fd = mkstemp(tmp); 96 fd = mkstemp(tmp);
97 if (fd >= 0) { 97 if (fd >= 0) {
98 file = fdopen(fd, "w"); 98 file = xfdopen_for_write(fd);
99 opened = tmp; 99 opened = tmp;
100 } 100 }
101 fchmod(fd, 0644); 101 fchmod(fd, 0644);
diff --git a/editors/sed.c b/editors/sed.c
index c1ee7502a..19e768355 100644
--- a/editors/sed.c
+++ b/editors/sed.c
@@ -1338,7 +1338,7 @@ int sed_main(int argc UNUSED_PARAM, char **argv)
1338 nonstdoutfd = mkstemp(G.outname); 1338 nonstdoutfd = mkstemp(G.outname);
1339 if (-1 == nonstdoutfd) 1339 if (-1 == nonstdoutfd)
1340 bb_perror_msg_and_die("can't create temp file %s", G.outname); 1340 bb_perror_msg_and_die("can't create temp file %s", G.outname);
1341 G.nonstdout = fdopen(nonstdoutfd, "w"); 1341 G.nonstdout = xfdopen_for_write(nonstdoutfd);
1342 1342
1343 /* Set permissions/owner of output file */ 1343 /* Set permissions/owner of output file */
1344 fstat(fileno(file), &statbuf); 1344 fstat(fileno(file), &statbuf);
diff --git a/include/libbb.h b/include/libbb.h
index 89d7a7b1b..d95be5c51 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -658,21 +658,23 @@ extern char *xmalloc_fgetline(FILE *file) FAST_FUNC RETURNS_MALLOC;
658/* Same, but doesn't try to conserve space (may have some slack after the end) */ 658/* Same, but doesn't try to conserve space (may have some slack after the end) */
659/* extern char *xmalloc_fgetline_fast(FILE *file) FAST_FUNC RETURNS_MALLOC; */ 659/* extern char *xmalloc_fgetline_fast(FILE *file) FAST_FUNC RETURNS_MALLOC; */
660 660
661extern void die_if_ferror(FILE *file, const char *msg) FAST_FUNC; 661void die_if_ferror(FILE *file, const char *msg) FAST_FUNC;
662extern void die_if_ferror_stdout(void) FAST_FUNC; 662void die_if_ferror_stdout(void) FAST_FUNC;
663extern int fflush_all(void) FAST_FUNC; 663int fflush_all(void) FAST_FUNC;
664extern void fflush_stdout_and_exit(int retval) NORETURN FAST_FUNC; 664void fflush_stdout_and_exit(int retval) NORETURN FAST_FUNC;
665extern int fclose_if_not_stdin(FILE *file) FAST_FUNC; 665int fclose_if_not_stdin(FILE *file) FAST_FUNC;
666extern FILE *xfopen(const char *filename, const char *mode) FAST_FUNC; 666FILE* xfopen(const char *filename, const char *mode) FAST_FUNC;
667/* Prints warning to stderr and returns NULL on failure: */ 667/* Prints warning to stderr and returns NULL on failure: */
668extern FILE *fopen_or_warn(const char *filename, const char *mode) FAST_FUNC; 668FILE* fopen_or_warn(const char *filename, const char *mode) FAST_FUNC;
669/* "Opens" stdin if filename is special, else just opens file: */ 669/* "Opens" stdin if filename is special, else just opens file: */
670extern FILE *xfopen_stdin(const char *filename) FAST_FUNC; 670FILE* xfopen_stdin(const char *filename) FAST_FUNC;
671extern FILE *fopen_or_warn_stdin(const char *filename) FAST_FUNC; 671FILE* fopen_or_warn_stdin(const char *filename) FAST_FUNC;
672extern FILE* fopen_for_read(const char *path) FAST_FUNC; 672FILE* fopen_for_read(const char *path) FAST_FUNC;
673extern FILE* xfopen_for_read(const char *path) FAST_FUNC; 673FILE* xfopen_for_read(const char *path) FAST_FUNC;
674extern FILE* fopen_for_write(const char *path) FAST_FUNC; 674FILE* fopen_for_write(const char *path) FAST_FUNC;
675extern FILE* xfopen_for_write(const char *path) FAST_FUNC; 675FILE* xfopen_for_write(const char *path) FAST_FUNC;
676FILE* xfdopen_for_read(int fd) FAST_FUNC;
677FILE* xfdopen_for_write(int fd) FAST_FUNC;
676 678
677int bb_pstrcmp(const void *a, const void *b) /* not FAST_FUNC! */; 679int bb_pstrcmp(const void *a, const void *b) /* not FAST_FUNC! */;
678void qsort_string_vector(char **sv, unsigned count) FAST_FUNC; 680void qsort_string_vector(char **sv, unsigned count) FAST_FUNC;
diff --git a/libbb/update_passwd.c b/libbb/update_passwd.c
index ba773fcb2..301893be1 100644
--- a/libbb/update_passwd.c
+++ b/libbb/update_passwd.c
@@ -137,12 +137,7 @@ int FAST_FUNC update_passwd(const char *filename,
137 fchown(new_fd, sb.st_uid, sb.st_gid); 137 fchown(new_fd, sb.st_uid, sb.st_gid);
138 } 138 }
139 errno = 0; 139 errno = 0;
140 new_fp = fdopen(new_fd, "w"); 140 new_fp = xfdopen_for_write(new_fd);
141 if (!new_fp) {
142 bb_perror_nomsg();
143 close(new_fd);
144 goto unlink_new;
145 }
146 141
147 /* Backup file is "/etc/passwd-" */ 142 /* Backup file is "/etc/passwd-" */
148 *sfx_char = '-'; 143 *sfx_char = '-';
diff --git a/libbb/wfopen.c b/libbb/wfopen.c
index 1cb871ef5..deec79a28 100644
--- a/libbb/wfopen.c
+++ b/libbb/wfopen.c
@@ -38,3 +38,19 @@ FILE* FAST_FUNC xfopen_for_write(const char *path)
38{ 38{
39 return xfopen(path, "w"); 39 return xfopen(path, "w");
40} 40}
41
42static FILE* xfdopen_helper(unsigned fd_and_rw_bit)
43{
44 FILE* fp = fdopen(fd_and_rw_bit >> 1, fd_and_rw_bit & 1 ? "w" : "r");
45 if (!fp)
46 bb_error_msg_and_die(bb_msg_memory_exhausted);
47 return fp;
48}
49FILE* FAST_FUNC xfdopen_for_read(int fd)
50{
51 return xfdopen_helper(fd << 1);
52}
53FILE* FAST_FUNC xfdopen_for_write(int fd)
54{
55 return xfdopen_helper((fd << 1) + 1);
56}
diff --git a/mailutils/mime.c b/mailutils/mime.c
index dd81139f2..17d0f9d80 100644
--- a/mailutils/mime.c
+++ b/mailutils/mime.c
@@ -293,7 +293,7 @@ static int parse(const char *boundary, char **argv)
293 } 293 }
294 // parent dumps to fd[1] 294 // parent dumps to fd[1]
295 close(fd[0]); 295 close(fd[0]);
296 fp = fdopen(fd[1], "w"); 296 fp = xfdopen_for_write(fd[1]);
297 signal(SIGPIPE, SIG_IGN); // ignore EPIPE 297 signal(SIGPIPE, SIG_IGN); // ignore EPIPE
298 // or create a file for dump 298 // or create a file for dump
299 } else { 299 } else {
diff --git a/mailutils/sendmail.c b/mailutils/sendmail.c
index 2f99df6c0..4b58a78eb 100644
--- a/mailutils/sendmail.c
+++ b/mailutils/sendmail.c
@@ -93,7 +93,7 @@ int sendmail_main(int argc UNUSED_PARAM, char **argv)
93 93
94 // save initial stdin since body is piped! 94 // save initial stdin since body is piped!
95 xdup2(STDIN_FILENO, 3); 95 xdup2(STDIN_FILENO, 3);
96 G.fp0 = fdopen(3, "r"); 96 G.fp0 = xfdopen_for_read(3);
97 97
98 // parse options 98 // parse options
99 // -f is required. -H and -S are mutually exclusive 99 // -f is required. -H and -S are mutually exclusive
diff --git a/miscutils/fbsplash.c b/miscutils/fbsplash.c
index 0afd189ca..4560bb2e9 100644
--- a/miscutils/fbsplash.c
+++ b/miscutils/fbsplash.c
@@ -227,7 +227,7 @@ static void fb_drawimage(void)
227 int fd = open_zipped(G.image_filename); 227 int fd = open_zipped(G.image_filename);
228 if (fd < 0) 228 if (fd < 0)
229 bb_simple_perror_msg_and_die(G.image_filename); 229 bb_simple_perror_msg_and_die(G.image_filename);
230 theme_file = fdopen(fd, "r"); 230 theme_file = xfdopen_for_read(fd);
231 } 231 }
232 head = xmalloc(256); 232 head = xmalloc(256);
233 233
diff --git a/modutils/modprobe-small.c b/modutils/modprobe-small.c
index 8ef9101e7..53b7c9468 100644
--- a/modutils/modprobe-small.c
+++ b/modutils/modprobe-small.c
@@ -384,11 +384,7 @@ static void write_out_dep_bb(int fd)
384 FILE *fp; 384 FILE *fp;
385 385
386 /* We want good error reporting. fdprintf is not good enough. */ 386 /* We want good error reporting. fdprintf is not good enough. */
387 fp = fdopen(fd, "w"); 387 fp = xfdopen_for_write(fd);
388 if (!fp) {
389 close(fd);
390 goto err;
391 }
392 i = 0; 388 i = 0;
393 while (modinfo[i].pathname) { 389 while (modinfo[i].pathname) {
394 fprintf(fp, "%s%s%s\n" "%s%s\n", 390 fprintf(fp, "%s%s%s\n" "%s%s\n",
diff --git a/networking/ftpd.c b/networking/ftpd.c
index 9937cc3ea..df8188cba 100644
--- a/networking/ftpd.c
+++ b/networking/ftpd.c
@@ -691,9 +691,7 @@ handle_dir_common(int opts)
691 /* -n prevents user/groupname display, 691 /* -n prevents user/groupname display,
692 * which can be problematic in chroot */ 692 * which can be problematic in chroot */
693 ls_fd = popen_ls((opts & LONG_LISTING) ? "-l" : "-1"); 693 ls_fd = popen_ls((opts & LONG_LISTING) ? "-l" : "-1");
694 ls_fp = fdopen(ls_fd, "r"); 694 ls_fp = xfdopen_for_read(ls_fd);
695 if (!ls_fp) /* never happens. paranoia */
696 bb_perror_msg_and_die("fdopen");
697 695
698 if (opts & USE_CTRL_CONN) { 696 if (opts & USE_CTRL_CONN) {
699 /* STAT <filename> */ 697 /* STAT <filename> */
diff --git a/networking/ifupdown.c b/networking/ifupdown.c
index 88d1944da..b16186ec7 100644
--- a/networking/ifupdown.c
+++ b/networking/ifupdown.c
@@ -1056,8 +1056,8 @@ static int popen2(FILE **in, FILE **out, char *command, char *param)
1056 /* parent */ 1056 /* parent */
1057 close(infd.rd); 1057 close(infd.rd);
1058 close(outfd.wr); 1058 close(outfd.wr);
1059 *in = fdopen(infd.wr, "w"); 1059 *in = xfdopen_for_write(infd.wr);
1060 *out = fdopen(outfd.rd, "r"); 1060 *out = xfdopen_for_read(outfd.rd);
1061 return pid; 1061 return pid;
1062} 1062}
1063 1063
diff --git a/runit/svlogd.c b/runit/svlogd.c
index 9a00fadee..fe40cd890 100644
--- a/runit/svlogd.c
+++ b/runit/svlogd.c
@@ -761,8 +761,8 @@ static NOINLINE unsigned logdir_open(struct logdir *ld, const char *fn)
761 } 761 }
762 while ((ld->fdcur = open("current", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600)) == -1) 762 while ((ld->fdcur = open("current", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600)) == -1)
763 pause2cannot("open current", ld->name); 763 pause2cannot("open current", ld->name);
764 /* we presume this cannot fail */ 764 while ((ld->filecur = fdopen(ld->fdcur, "a")) == NULL)
765 ld->filecur = fdopen(ld->fdcur, "a"); //// 765 pause2cannot("open current", ld->name); ////
766 setvbuf(ld->filecur, NULL, _IOFBF, linelen); //// 766 setvbuf(ld->filecur, NULL, _IOFBF, linelen); ////
767 767
768 close_on_exec_on(ld->fdcur); 768 close_on_exec_on(ld->fdcur);
diff --git a/shell/hush.c b/shell/hush.c
index 6f394d1d5..ede8d680e 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -5301,25 +5301,22 @@ static FILE *generate_stream_from_string(const char *s, pid_t *pid_p)
5301 free(to_free); 5301 free(to_free);
5302# endif 5302# endif
5303 close(channel[1]); 5303 close(channel[1]);
5304//TODO: libbb: fdopen_or_die? 5304 close_on_exec_on(channel[0]);
5305 return fdopen(channel[0], "r"); 5305 return xfdopen_for_read(channel[0]);
5306} 5306}
5307 5307
5308/* Return code is exit status of the process that is run. */ 5308/* Return code is exit status of the process that is run. */
5309static int process_command_subs(o_string *dest, const char *s) 5309static int process_command_subs(o_string *dest, const char *s)
5310{ 5310{
5311 FILE *pf; 5311 FILE *fp;
5312 struct in_str pipe_str; 5312 struct in_str pipe_str;
5313 pid_t pid; 5313 pid_t pid;
5314 int status, ch, eol_cnt; 5314 int status, ch, eol_cnt;
5315 5315
5316 pf = generate_stream_from_string(s, &pid); 5316 fp = generate_stream_from_string(s, &pid);
5317 if (pf == NULL)
5318 return 1;
5319 close_on_exec_on(fileno(pf));
5320 5317
5321 /* Now send results of command back into original context */ 5318 /* Now send results of command back into original context */
5322 setup_file_in_str(&pipe_str, pf); 5319 setup_file_in_str(&pipe_str, fp);
5323 eol_cnt = 0; 5320 eol_cnt = 0;
5324 while ((ch = i_getch(&pipe_str)) != EOF) { 5321 while ((ch = i_getch(&pipe_str)) != EOF) {
5325 if (ch == '\n') { 5322 if (ch == '\n') {
@@ -5334,7 +5331,7 @@ static int process_command_subs(o_string *dest, const char *s)
5334 } 5331 }
5335 5332
5336 debug_printf("done reading from `cmd` pipe, closing it\n"); 5333 debug_printf("done reading from `cmd` pipe, closing it\n");
5337 fclose(pf); 5334 fclose(fp);
5338 /* We need to extract exitcode. Test case 5335 /* We need to extract exitcode. Test case
5339 * "true; echo `sleep 1; false` $?" 5336 * "true; echo `sleep 1; false` $?"
5340 * should print 1 */ 5337 * should print 1 */