From ddec5af6b0803c7434a1cc2fdee5cb9873fe6bd0 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Thu, 26 Oct 2006 23:25:17 +0000 Subject: [PATCH] rename functions to more understandable names --- archival/dpkg.c | 4 ++-- coreutils/cat.c | 4 ++-- coreutils/cksum.c | 2 +- coreutils/cmp.c | 5 ++--- coreutils/cut.c | 2 +- coreutils/fold.c | 4 ++-- coreutils/head.c | 4 ++-- coreutils/md5_sha1_sum.c | 2 +- coreutils/sum.c | 6 +++--- coreutils/tee.c | 2 +- coreutils/uudecode.c | 2 +- coreutils/wc.c | 4 ++-- editors/sed.c | 2 +- findutils/grep.c | 2 +- include/libbb.h | 32 +++++++++++++++++--------------- libbb/Kbuild | 2 +- libbb/fclose_nonstdin.c | 2 +- libbb/fgets_str.c | 10 +++------- libbb/wfopen.c | 8 +++----- libbb/wfopen_input.c | 13 ++++++------- libbb/xfuncs.c | 1 + loginutils/deluser.c | 4 ++-- miscutils/readahead.c | 2 +- miscutils/strings.c | 4 ++-- shell/lash.c | 4 ++-- util-linux/fdisk.c | 2 +- util-linux/more.c | 2 +- 27 files changed, 63 insertions(+), 68 deletions(-) diff --git a/archival/dpkg.c b/archival/dpkg.c index 2f7372100..2b9c4b82d 100644 --- a/archival/dpkg.c +++ b/archival/dpkg.c @@ -787,7 +787,7 @@ static void index_status_file(const char *filename) unsigned int status_num; status_file = xfopen(filename, "r"); - while ((control_buffer = fgets_str(status_file, "\n\n")) != NULL) { + while ((control_buffer = xmalloc_fgets_str(status_file, "\n\n")) != NULL) { const unsigned int package_num = fill_package_struct(control_buffer); if (package_num != -1) { status_node = xmalloc(sizeof(status_node_t)); @@ -842,7 +842,7 @@ static void write_status_file(deb_file_t **deb_file) int i = 0; /* Update previously known packages */ - while ((control_buffer = fgets_str(old_status_file, "\n\n")) != NULL) { + while ((control_buffer = xmalloc_fgets_str(old_status_file, "\n\n")) != NULL) { if ((tmp_string = strstr(control_buffer, "Package:")) == NULL) { continue; } diff --git a/coreutils/cat.c b/coreutils/cat.c index 10eb29c4d..a95980552 100644 --- a/coreutils/cat.c +++ b/coreutils/cat.c @@ -26,10 +26,10 @@ int cat_main(int argc, char **argv) } do { - f = bb_wfopen_input(*argv); + f = fopen_or_warn_stdin(*argv); if (f) { off_t r = bb_copyfd_eof(fileno(f), STDOUT_FILENO); - bb_fclose_nonstdin(f); + fclose_if_not_stdin(f); if (r >= 0) { continue; } diff --git a/coreutils/cksum.c b/coreutils/cksum.c index 3a9b0b08c..52213328c 100644 --- a/coreutils/cksum.c +++ b/coreutils/cksum.c @@ -22,7 +22,7 @@ int cksum_main(int argc, char **argv) int inp_stdin = (argc == optind) ? 1 : 0; do { - fp = bb_wfopen_input((inp_stdin) ? bb_msg_standard_input : *++argv); + fp = fopen_or_warn_stdin((inp_stdin) ? bb_msg_standard_input : *++argv); crc = 0; length = 0; diff --git a/coreutils/cmp.c b/coreutils/cmp.c index 2b923c845..71007eac1 100644 --- a/coreutils/cmp.c +++ b/coreutils/cmp.c @@ -27,10 +27,9 @@ static FILE *cmp_xfopen_input(const char * const filename) { FILE *fp; - if ((fp = bb_wfopen_input(filename)) != NULL) { + fp = fopen_or_warn_stdin(filename); + if (fp) return fp; - } - exit(xfunc_error_retval); /* We already output an error message. */ } diff --git a/coreutils/cut.c b/coreutils/cut.c index 5dc35434f..a538e3d20 100644 --- a/coreutils/cut.c +++ b/coreutils/cut.c @@ -272,7 +272,7 @@ int cut_main(int argc, char **argv) FILE *file; for (; optind < argc; optind++) { - file = bb_wfopen(argv[optind], "r"); + file = fopen_or_warn(argv[optind], "r"); if (file) { cut_file(file); fclose(file); diff --git a/coreutils/fold.c b/coreutils/fold.c index e33be5594..9ca693dff 100644 --- a/coreutils/fold.c +++ b/coreutils/fold.c @@ -70,7 +70,7 @@ int fold_main(int argc, char **argv) } do { - FILE *istream = bb_wfopen_input(*argv); + FILE *istream = fopen_or_warn_stdin(*argv); int c; int column = 0; /* Screen column where next char will go. */ int offset_out = 0; /* Index in `line_out' for next char. */ @@ -144,7 +144,7 @@ rescan: fwrite(line_out, sizeof(char), (size_t) offset_out, stdout); } - if (ferror(istream) || bb_fclose_nonstdin(istream)) { + if (ferror(istream) || fclose_if_not_stdin(istream)) { bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */ errs |= EXIT_FAILURE; } diff --git a/coreutils/head.c b/coreutils/head.c index 2e9000df4..d732461f7 100644 --- a/coreutils/head.c +++ b/coreutils/head.c @@ -112,7 +112,7 @@ int head_main(int argc, char **argv) #endif do { - fp = bb_wfopen_input(*argv); + fp = fopen_or_warn_stdin(*argv); if (fp) { if (fp == stdin) { *argv = (char *) bb_msg_standard_input; @@ -127,7 +127,7 @@ int head_main(int argc, char **argv) } putchar(c); } - if (bb_fclose_nonstdin(fp)) { + if (fclose_if_not_stdin(fp)) { bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */ retval = EXIT_FAILURE; } diff --git a/coreutils/md5_sha1_sum.c b/coreutils/md5_sha1_sum.c index ca23d8ac4..93d894655 100644 --- a/coreutils/md5_sha1_sum.c +++ b/coreutils/md5_sha1_sum.c @@ -162,7 +162,7 @@ int md5_sha1_sum_main(int argc, char **argv) bb_error_msg("WARNING: %d of %d computed checksums did NOT match", count_failed, count_total); } - if (bb_fclose_nonstdin(pre_computed_stream) == EOF) { + if (fclose_if_not_stdin(pre_computed_stream) == EOF) { bb_perror_msg_and_die("cannot close file %s", file_ptr); } } else { diff --git a/coreutils/sum.c b/coreutils/sum.c index d663e34dd..93f4e22eb 100644 --- a/coreutils/sum.c +++ b/coreutils/sum.c @@ -38,7 +38,7 @@ static int bsd_sum_file(const char *file, int print_name) fp = stdin; have_read_stdin++; } else { - fp = bb_wfopen(file, "r"); + fp = fopen_or_warn(file, "r"); if (fp == NULL) goto out; } @@ -52,11 +52,11 @@ static int bsd_sum_file(const char *file, int print_name) if (ferror(fp)) { bb_perror_msg(file); - bb_fclose_nonstdin(fp); + fclose_if_not_stdin(fp); goto out; } - if (bb_fclose_nonstdin(fp) == EOF) { + if (fclose_if_not_stdin(fp) == EOF) { bb_perror_msg(file); goto out; } diff --git a/coreutils/tee.c b/coreutils/tee.c index f0e1fad86..06c94aba6 100644 --- a/coreutils/tee.c +++ b/coreutils/tee.c @@ -49,7 +49,7 @@ int tee_main(int argc, char **argv) goto GOT_NEW_FILE; do { - if ((*p = bb_wfopen(*argv, mode)) == NULL) { + if ((*p = fopen_or_warn(*argv, mode)) == NULL) { retval = EXIT_FAILURE; continue; } diff --git a/coreutils/uudecode.c b/coreutils/uudecode.c index c8152a808..a08d985ac 100644 --- a/coreutils/uudecode.c +++ b/coreutils/uudecode.c @@ -174,7 +174,7 @@ int uudecode_main(int argc, char **argv) } free(line); ret = decode_fn_ptr(src_stream, dst_stream); - bb_fclose_nonstdin(src_stream); + fclose_if_not_stdin(src_stream); return(ret); } bb_error_msg_and_die("No `begin' line"); diff --git a/coreutils/wc.c b/coreutils/wc.c index ebae5f69f..4b76e5499 100644 --- a/coreutils/wc.c +++ b/coreutils/wc.c @@ -107,7 +107,7 @@ int wc_main(int argc, char **argv) while ((arg = *argv++) != 0) { ++num_files; - fp = bb_wfopen_input(arg); + fp = fopen_or_warn_stdin(arg); if (!fp) { status = EXIT_FAILURE; continue; @@ -172,7 +172,7 @@ int wc_main(int argc, char **argv) } totals[WC_LENGTH] -= counts[WC_LENGTH]; - bb_fclose_nonstdin(fp); + fclose_if_not_stdin(fp); OUTPUT: /* coreutils wc tries hard to print pretty columns diff --git a/editors/sed.c b/editors/sed.c index 3e33529cb..65ca5606b 100644 --- a/editors/sed.c +++ b/editors/sed.c @@ -1194,7 +1194,7 @@ int sed_main(int argc, char **argv) process_files(); continue; } - file = bb_wfopen(argv[i], "r"); + file = fopen_or_warn(argv[i], "r"); if (!file) { status = EXIT_FAILURE; continue; diff --git a/findutils/grep.c b/findutils/grep.c index b7964d1eb..b76a17a41 100644 --- a/findutils/grep.c +++ b/findutils/grep.c @@ -447,7 +447,7 @@ int grep_main(int argc, char **argv) } } matched += grep_file(file); - bb_fclose_nonstdin(file); + fclose_if_not_stdin(file); grep_done: if (matched < 0) { /* we found a match but were told to be quiet, stop here and diff --git a/include/libbb.h b/include/libbb.h index f435a5915..da936d66e 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -240,20 +240,29 @@ extern void erase_mtab(const char * name); extern long *find_pid_by_name( const char* pidName); extern long *pidlist_reverse(long *pidList); extern char *find_block_device(char *path); -extern char *xmalloc_fgets(FILE *file); -/* Chops off '\n' from the end, unlike fgets: */ -extern char *xmalloc_getline(FILE *file); -extern char *bb_get_chunk_from_file(FILE *file, int *end); extern off_t bb_copyfd_size(int fd1, int fd2, off_t size); extern off_t bb_copyfd_eof(int fd1, int fd2); extern char bb_process_escape_sequence(const char **ptr); extern char *bb_get_last_path_component(char *path); -extern FILE *bb_wfopen(const char *path, const char *mode); -extern FILE *bb_wfopen_input(const char *filename); -extern FILE *xfopen(const char *path, const char *mode); -extern int bb_fclose_nonstdin(FILE *f); +/* Prints to stdout closes entire FILE. Exits on error: */ +extern void xprint_and_close_file(FILE *file); +extern char *xmalloc_fgets(FILE *file); +/* /* Read up to (and including) TERMINATING_STRING: */ +extern char *xmalloc_fgets_str(FILE *file, const char *terminating_string); +/* Chops off '\n' from the end, unlike fgets: */ +extern char *xmalloc_getline(FILE *file); +extern char *bb_get_chunk_from_file(FILE *file, int *end); +extern void die_if_ferror(FILE *file, const char *msg); +extern void die_if_ferror_stdout(void); +extern void xfflush_stdout(void); extern void fflush_stdout_and_exit(int retval) ATTRIBUTE_NORETURN; +extern int fclose_if_not_stdin(FILE *file); +extern FILE *xfopen(const char *filename, const char *mode); +/* Prints warning to stderr and returns NULL on failure: */ +extern FILE *fopen_or_warn(const char *filename, const char *mode); +/* "Opens" stdin if filename is special, else just opens file: */ +extern FILE *fopen_or_warn_stdin(const char *filename); extern void xstat(char *filename, struct stat *buf); extern int xsocket(int domain, int type, int protocol); @@ -278,10 +287,6 @@ extern const struct option *applet_long_options; extern uint32_t option_mask32; extern uint32_t getopt32(int argc, char **argv, const char *applet_opts, ...); -extern void die_if_ferror(FILE *fp, const char *fn); -extern void die_if_ferror_stdout(void); -extern void xfflush_stdout(void); - extern void bb_warn_ignoring_args(int n); extern void chomp(char *s); @@ -430,8 +435,6 @@ char *concat_path_file(const char *path, const char *filename); char *concat_subpath_file(const char *path, const char *filename); char *last_char_is(const char *s, int c); -char *fgets_str(FILE *file, const char *terminating_string); - int execable_file(const char *name); char *find_execable(const char *filename); int exists_execable(const char *filename); @@ -577,7 +580,6 @@ void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name); void reset_ino_dev_hashtable(void); char *xasprintf(const char *format, ...) __attribute__ ((format (printf, 1, 2))); -void xprint_and_close_file(FILE *file); #define FAIL_DELAY 3 extern void bb_do_delay(int seconds); diff --git a/libbb/Kbuild b/libbb/Kbuild index 0dd8c2be4..87c09bdfd 100644 --- a/libbb/Kbuild +++ b/libbb/Kbuild @@ -9,7 +9,7 @@ lib-y:= \ compare_string_array.o concat_path_file.o copy_file.o copyfd.o \ crc32.o create_icmp_socket.o create_icmp6_socket.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 \ + find_pid_by_name.o find_root_device.o xmalloc_fgets_str.o \ full_write.o get_last_path_component.o get_line_from_file.o \ herror_msg.o herror_msg_and_die.o \ human_readable.o inet_common.o inode_hash.o isdirectory.o \ diff --git a/libbb/fclose_nonstdin.c b/libbb/fclose_nonstdin.c index be986d1b1..951ab30d6 100644 --- a/libbb/fclose_nonstdin.c +++ b/libbb/fclose_nonstdin.c @@ -15,7 +15,7 @@ #include #include -int bb_fclose_nonstdin(FILE *f) +int fclose_if_not_stdin(FILE *f) { if (f != stdin) { return fclose(f); diff --git a/libbb/fgets_str.c b/libbb/fgets_str.c index 41370d176..1bc6c3b1c 100644 --- a/libbb/fgets_str.c +++ b/libbb/fgets_str.c @@ -8,17 +8,12 @@ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */ - -#include -#include -#include - #include "libbb.h" /* Read up to (and including) TERMINATING_STRING from FILE and return it. * Return NULL on EOF. */ -char *fgets_str(FILE *file, const char *terminating_string) +char *xmalloc_fgets_str(FILE *file, const char *terminating_string) { char *linebuf = NULL; const int term_length = strlen(terminating_string); @@ -36,7 +31,8 @@ char *fgets_str(FILE *file, const char *terminating_string) /* grow the line buffer as necessary */ while (idx > linebufsz - 2) { - linebuf = xrealloc(linebuf, linebufsz += 1000); + linebufsz += 200; + linebuf = xrealloc(linebuf, linebufsz); } linebuf[idx] = ch; diff --git a/libbb/wfopen.c b/libbb/wfopen.c index 9d663281e..26e6a13e2 100644 --- a/libbb/wfopen.c +++ b/libbb/wfopen.c @@ -7,14 +7,12 @@ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */ -#include -#include #include "libbb.h" -FILE *bb_wfopen(const char *path, const char *mode) +FILE *fopen_or_warn(const char *path, const char *mode) { - FILE *fp; - if ((fp = fopen(path, mode)) == NULL) { + FILE *fp = fopen(path, mode); + if (!fp) { bb_perror_msg("%s", path); errno = 0; } diff --git a/libbb/wfopen_input.c b/libbb/wfopen_input.c index d764f1d06..3da855fe6 100644 --- a/libbb/wfopen_input.c +++ b/libbb/wfopen_input.c @@ -14,18 +14,17 @@ * Note: We also consider "" to main stdin (for 'cmp' at least). */ -#include -#include -#include +#include "libbb.h" -FILE *bb_wfopen_input(const char *filename) +FILE *fopen_or_warn_stdin(const char *filename) { FILE *fp = stdin; - if ((filename != bb_msg_standard_input) - && filename[0] && ((filename[0] != '-') || filename[1]) + if (filename != bb_msg_standard_input + && filename[0] + && (filename[0] != '-' || filename[1]) ) { - fp = bb_wfopen(filename, "r"); + fp = fopen_or_warn(filename, "r"); } return fp; diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index c5e18c312..1144a67c6 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c @@ -341,6 +341,7 @@ char *xasprintf(const char *format, ...) // close that file. void xprint_and_close_file(FILE *file) { + fflush(stdout); // copyfd outputs error messages for us. if (bb_copyfd_eof(fileno(file), 1) == -1) exit(xfunc_error_retval); diff --git a/loginutils/deluser.c b/loginutils/deluser.c index bbbd77dbd..795dae49f 100644 --- a/loginutils/deluser.c +++ b/loginutils/deluser.c @@ -54,7 +54,7 @@ static void del_line_matching(const char *login, const char *filename) struct stat statbuf; - if ((passwd = bb_wfopen(filename, "r"))) { + if ((passwd = fopen_or_warn(filename, "r"))) { // Remove pointless const. xstat((char *)filename, &statbuf); buffer = (char *) xmalloc(statbuf.st_size * sizeof(char)); @@ -64,7 +64,7 @@ static void del_line_matching(const char *login, const char *filename) b = boundary(buffer, login); if (b.stop != 0) { /* write the file w/o the user */ - if ((passwd = bb_wfopen(filename, "w"))) { + if ((passwd = fopen_or_warn(filename, "w"))) { fwrite(buffer, (b.start - 1), sizeof(char), passwd); fwrite(&buffer[b.stop], (statbuf.st_size - b.stop), sizeof(char), passwd); fclose(passwd); diff --git a/miscutils/readahead.c b/miscutils/readahead.c index 49cd7fd09..356c40451 100644 --- a/miscutils/readahead.c +++ b/miscutils/readahead.c @@ -20,7 +20,7 @@ int readahead_main(int argc, char **argv) if (argc == 1) bb_show_usage(); while (*++argv) { - if ((f = bb_wfopen(*argv, "r")) != NULL) { + if ((f = fopen_or_warn(*argv, "r")) != NULL) { int r, fd=fileno(f); r = readahead(fd, 0, fdlength(fd)); diff --git a/miscutils/strings.c b/miscutils/strings.c index dae17315e..0d5576e9b 100644 --- a/miscutils/strings.c +++ b/miscutils/strings.c @@ -45,7 +45,7 @@ int strings_main(int argc, char **argv) } do { - file = bb_wfopen(*argv, "r"); + file = fopen_or_warn(*argv, "r"); if (file) { PIPE: count = 0; @@ -75,7 +75,7 @@ PIPE: } count++; } while (c != EOF); - bb_fclose_nonstdin(file); + fclose_if_not_stdin(file); } else { status = EXIT_FAILURE; } diff --git a/shell/lash.c b/shell/lash.c index 96c0b3007..4067bc6bc 100644 --- a/shell/lash.c +++ b/shell/lash.c @@ -426,7 +426,7 @@ static int builtin_source(struct child_prog *child) FILE *input; int status; - input = bb_wfopen(child->argv[1], "r"); + input = fopen_or_warn(child->argv[1], "r"); if (!input) { return EXIT_FAILURE; } @@ -1522,7 +1522,7 @@ int lash_main(int argc_l, char **argv_l) llist_add_to(&close_me_list, (void *)(long)fileno(prof_input)); /* Now run the file */ busy_loop(prof_input); - bb_fclose_nonstdin(prof_input); + fclose_if_not_stdin(prof_input); llist_pop(&close_me_list); } } diff --git a/util-linux/fdisk.c b/util-linux/fdisk.c index 40a8d1bae..6e4bf6418 100644 --- a/util-linux/fdisk.c +++ b/util-linux/fdisk.c @@ -2771,7 +2771,7 @@ tryprocpt(void) char line[100], ptname[100], devname[120], *s; int ma, mi, sz; - procpt = bb_wfopen("/proc/partitions", "r"); + procpt = fopen_or_warn("/proc/partitions", "r"); while (fgets(line, sizeof(line), procpt)) { if (sscanf(line, " %d %d %d %[^\n ]", diff --git a/util-linux/more.c b/util-linux/more.c index f68292e80..85209ce61 100644 --- a/util-linux/more.c +++ b/util-linux/more.c @@ -81,7 +81,7 @@ int more_main(int argc, char **argv) if (argc == 0) { file = stdin; } else - file = bb_wfopen(*argv, "r"); + file = fopen_or_warn(*argv, "r"); if(file==0) goto loop; -- 2.25.1