rename functions to more understandable names
No binary (build failed)
—
(log yok)
| @@ -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; | |
| } | |
| @@ -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; | |
| } | |
| @@ -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; | |
| @@ -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. */ | |
| } | |
| @@ -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); | |
| @@ -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; | |
| } | |
| @@ -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; | |
| } | |
| @@ -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 { | |
| @@ -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; | |
| } | |
| @@ -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; | |
| } | |
| @@ -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"); | |
| @@ -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 | |
| @@ -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; | |
| @@ -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 | |
| @@ -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); | |
| @@ -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 \ | |
| @@ -15,7 +15,7 @@ | |
| #include <stdio.h> | |
| #include <libbb.h> | |
| - | int bb_fclose_nonstdin(FILE *f) |
| + | int fclose_if_not_stdin(FILE *f) |
| { | |
| if (f != stdin) { | |
| return fclose(f); | |
| @@ -8,17 +8,12 @@ | |
| * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | |
| */ | |
| - | |
| - | #include <stdio.h> |
| - | #include <stdlib.h> |
| - | #include <string.h> |
| - | |
| #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; | |
| @@ -7,14 +7,12 @@ | |
| * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | |
| */ | |
| - | #include <stdio.h> |
| - | #include <errno.h> |
| #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; | |
| } | |
| @@ -14,18 +14,17 @@ | |
| * Note: We also consider "" to main stdin (for 'cmp' at least). | |
| */ | |
| - | #include <stdio.h> |
| - | #include <sys/stat.h> |
| - | #include <libbb.h> |
| + | #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; | |
| @@ -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); | |
| @@ -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); | |
| @@ -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)); | |
| @@ -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; | |
| } | |
| @@ -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); | |
| } | |
| } | |
| @@ -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 ]", | |
| @@ -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; | |