rename functions to more understandable names

This commit is contained in:
Denis Vlasenko 2006-10-26 23:25:17 +00:00
parent f0ed376eda
commit ddec5af6b0
27 changed files with 63 additions and 68 deletions

View File

@ -787,7 +787,7 @@ static void index_status_file(const char *filename)
unsigned int status_num; unsigned int status_num;
status_file = xfopen(filename, "r"); 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); const unsigned int package_num = fill_package_struct(control_buffer);
if (package_num != -1) { if (package_num != -1) {
status_node = xmalloc(sizeof(status_node_t)); status_node = xmalloc(sizeof(status_node_t));
@ -842,7 +842,7 @@ static void write_status_file(deb_file_t **deb_file)
int i = 0; int i = 0;
/* Update previously known packages */ /* 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) { if ((tmp_string = strstr(control_buffer, "Package:")) == NULL) {
continue; continue;
} }

View File

@ -26,10 +26,10 @@ int cat_main(int argc, char **argv)
} }
do { do {
f = bb_wfopen_input(*argv); f = fopen_or_warn_stdin(*argv);
if (f) { if (f) {
off_t r = bb_copyfd_eof(fileno(f), STDOUT_FILENO); off_t r = bb_copyfd_eof(fileno(f), STDOUT_FILENO);
bb_fclose_nonstdin(f); fclose_if_not_stdin(f);
if (r >= 0) { if (r >= 0) {
continue; continue;
} }

View File

@ -22,7 +22,7 @@ int cksum_main(int argc, char **argv)
int inp_stdin = (argc == optind) ? 1 : 0; int inp_stdin = (argc == optind) ? 1 : 0;
do { 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; crc = 0;
length = 0; length = 0;

View File

@ -27,10 +27,9 @@ static FILE *cmp_xfopen_input(const char * const filename)
{ {
FILE *fp; FILE *fp;
if ((fp = bb_wfopen_input(filename)) != NULL) { fp = fopen_or_warn_stdin(filename);
if (fp)
return fp; return fp;
}
exit(xfunc_error_retval); /* We already output an error message. */ exit(xfunc_error_retval); /* We already output an error message. */
} }

View File

@ -272,7 +272,7 @@ int cut_main(int argc, char **argv)
FILE *file; FILE *file;
for (; optind < argc; optind++) { for (; optind < argc; optind++) {
file = bb_wfopen(argv[optind], "r"); file = fopen_or_warn(argv[optind], "r");
if (file) { if (file) {
cut_file(file); cut_file(file);
fclose(file); fclose(file);

View File

@ -70,7 +70,7 @@ int fold_main(int argc, char **argv)
} }
do { do {
FILE *istream = bb_wfopen_input(*argv); FILE *istream = fopen_or_warn_stdin(*argv);
int c; int c;
int column = 0; /* Screen column where next char will go. */ int column = 0; /* Screen column where next char will go. */
int offset_out = 0; /* Index in `line_out' for next char. */ 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); 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. */ bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */
errs |= EXIT_FAILURE; errs |= EXIT_FAILURE;
} }

View File

@ -112,7 +112,7 @@ int head_main(int argc, char **argv)
#endif #endif
do { do {
fp = bb_wfopen_input(*argv); fp = fopen_or_warn_stdin(*argv);
if (fp) { if (fp) {
if (fp == stdin) { if (fp == stdin) {
*argv = (char *) bb_msg_standard_input; *argv = (char *) bb_msg_standard_input;
@ -127,7 +127,7 @@ int head_main(int argc, char **argv)
} }
putchar(c); putchar(c);
} }
if (bb_fclose_nonstdin(fp)) { if (fclose_if_not_stdin(fp)) {
bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */ bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */
retval = EXIT_FAILURE; retval = EXIT_FAILURE;
} }

View File

@ -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", bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
count_failed, count_total); 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); bb_perror_msg_and_die("cannot close file %s", file_ptr);
} }
} else { } else {

View File

@ -38,7 +38,7 @@ static int bsd_sum_file(const char *file, int print_name)
fp = stdin; fp = stdin;
have_read_stdin++; have_read_stdin++;
} else { } else {
fp = bb_wfopen(file, "r"); fp = fopen_or_warn(file, "r");
if (fp == NULL) if (fp == NULL)
goto out; goto out;
} }
@ -52,11 +52,11 @@ static int bsd_sum_file(const char *file, int print_name)
if (ferror(fp)) { if (ferror(fp)) {
bb_perror_msg(file); bb_perror_msg(file);
bb_fclose_nonstdin(fp); fclose_if_not_stdin(fp);
goto out; goto out;
} }
if (bb_fclose_nonstdin(fp) == EOF) { if (fclose_if_not_stdin(fp) == EOF) {
bb_perror_msg(file); bb_perror_msg(file);
goto out; goto out;
} }

View File

@ -49,7 +49,7 @@ int tee_main(int argc, char **argv)
goto GOT_NEW_FILE; goto GOT_NEW_FILE;
do { do {
if ((*p = bb_wfopen(*argv, mode)) == NULL) { if ((*p = fopen_or_warn(*argv, mode)) == NULL) {
retval = EXIT_FAILURE; retval = EXIT_FAILURE;
continue; continue;
} }

View File

@ -174,7 +174,7 @@ int uudecode_main(int argc, char **argv)
} }
free(line); free(line);
ret = decode_fn_ptr(src_stream, dst_stream); ret = decode_fn_ptr(src_stream, dst_stream);
bb_fclose_nonstdin(src_stream); fclose_if_not_stdin(src_stream);
return(ret); return(ret);
} }
bb_error_msg_and_die("No `begin' line"); bb_error_msg_and_die("No `begin' line");

View File

@ -107,7 +107,7 @@ int wc_main(int argc, char **argv)
while ((arg = *argv++) != 0) { while ((arg = *argv++) != 0) {
++num_files; ++num_files;
fp = bb_wfopen_input(arg); fp = fopen_or_warn_stdin(arg);
if (!fp) { if (!fp) {
status = EXIT_FAILURE; status = EXIT_FAILURE;
continue; continue;
@ -172,7 +172,7 @@ int wc_main(int argc, char **argv)
} }
totals[WC_LENGTH] -= counts[WC_LENGTH]; totals[WC_LENGTH] -= counts[WC_LENGTH];
bb_fclose_nonstdin(fp); fclose_if_not_stdin(fp);
OUTPUT: OUTPUT:
/* coreutils wc tries hard to print pretty columns /* coreutils wc tries hard to print pretty columns

View File

@ -1194,7 +1194,7 @@ int sed_main(int argc, char **argv)
process_files(); process_files();
continue; continue;
} }
file = bb_wfopen(argv[i], "r"); file = fopen_or_warn(argv[i], "r");
if (!file) { if (!file) {
status = EXIT_FAILURE; status = EXIT_FAILURE;
continue; continue;

View File

@ -447,7 +447,7 @@ int grep_main(int argc, char **argv)
} }
} }
matched += grep_file(file); matched += grep_file(file);
bb_fclose_nonstdin(file); fclose_if_not_stdin(file);
grep_done: grep_done:
if (matched < 0) { if (matched < 0) {
/* we found a match but were told to be quiet, stop here and /* we found a match but were told to be quiet, stop here and

View File

@ -240,20 +240,29 @@ extern void erase_mtab(const char * name);
extern long *find_pid_by_name( const char* pidName); extern long *find_pid_by_name( const char* pidName);
extern long *pidlist_reverse(long *pidList); extern long *pidlist_reverse(long *pidList);
extern char *find_block_device(char *path); 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_size(int fd1, int fd2, off_t size);
extern off_t bb_copyfd_eof(int fd1, int fd2); extern off_t bb_copyfd_eof(int fd1, int fd2);
extern char bb_process_escape_sequence(const char **ptr); extern char bb_process_escape_sequence(const char **ptr);
extern char *bb_get_last_path_component(char *path); 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 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 void xstat(char *filename, struct stat *buf);
extern int xsocket(int domain, int type, int protocol); 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 option_mask32;
extern uint32_t getopt32(int argc, char **argv, const char *applet_opts, ...); 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 bb_warn_ignoring_args(int n);
extern void chomp(char *s); 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 *concat_subpath_file(const char *path, const char *filename);
char *last_char_is(const char *s, int c); char *last_char_is(const char *s, int c);
char *fgets_str(FILE *file, const char *terminating_string);
int execable_file(const char *name); int execable_file(const char *name);
char *find_execable(const char *filename); char *find_execable(const char *filename);
int exists_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); void reset_ino_dev_hashtable(void);
char *xasprintf(const char *format, ...) __attribute__ ((format (printf, 1, 2))); char *xasprintf(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
void xprint_and_close_file(FILE *file);
#define FAIL_DELAY 3 #define FAIL_DELAY 3
extern void bb_do_delay(int seconds); extern void bb_do_delay(int seconds);

View File

@ -9,7 +9,7 @@ lib-y:= \
compare_string_array.o concat_path_file.o copy_file.o copyfd.o \ compare_string_array.o concat_path_file.o copy_file.o copyfd.o \
crc32.o create_icmp_socket.o create_icmp6_socket.o \ crc32.o create_icmp_socket.o create_icmp6_socket.o \
device_open.o dump.o error_msg.o error_msg_and_die.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 \ full_write.o get_last_path_component.o get_line_from_file.o \
herror_msg.o herror_msg_and_die.o \ herror_msg.o herror_msg_and_die.o \
human_readable.o inet_common.o inode_hash.o isdirectory.o \ human_readable.o inet_common.o inode_hash.o isdirectory.o \

View File

@ -15,7 +15,7 @@
#include <stdio.h> #include <stdio.h>
#include <libbb.h> #include <libbb.h>
int bb_fclose_nonstdin(FILE *f) int fclose_if_not_stdin(FILE *f)
{ {
if (f != stdin) { if (f != stdin) {
return fclose(f); return fclose(f);

View File

@ -8,17 +8,12 @@
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details. * 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" #include "libbb.h"
/* Read up to (and including) TERMINATING_STRING from FILE and return it. /* Read up to (and including) TERMINATING_STRING from FILE and return it.
* Return NULL on EOF. */ * 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; char *linebuf = NULL;
const int term_length = strlen(terminating_string); 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 */ /* grow the line buffer as necessary */
while (idx > linebufsz - 2) { while (idx > linebufsz - 2) {
linebuf = xrealloc(linebuf, linebufsz += 1000); linebufsz += 200;
linebuf = xrealloc(linebuf, linebufsz);
} }
linebuf[idx] = ch; linebuf[idx] = ch;

View File

@ -7,14 +7,12 @@
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*/ */
#include <stdio.h>
#include <errno.h>
#include "libbb.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; FILE *fp = fopen(path, mode);
if ((fp = fopen(path, mode)) == NULL) { if (!fp) {
bb_perror_msg("%s", path); bb_perror_msg("%s", path);
errno = 0; errno = 0;
} }

View File

@ -14,18 +14,17 @@
* Note: We also consider "" to main stdin (for 'cmp' at least). * Note: We also consider "" to main stdin (for 'cmp' at least).
*/ */
#include <stdio.h> #include "libbb.h"
#include <sys/stat.h>
#include <libbb.h>
FILE *bb_wfopen_input(const char *filename) FILE *fopen_or_warn_stdin(const char *filename)
{ {
FILE *fp = stdin; FILE *fp = stdin;
if ((filename != bb_msg_standard_input) if (filename != bb_msg_standard_input
&& filename[0] && ((filename[0] != '-') || filename[1]) && filename[0]
&& (filename[0] != '-' || filename[1])
) { ) {
fp = bb_wfopen(filename, "r"); fp = fopen_or_warn(filename, "r");
} }
return fp; return fp;

View File

@ -341,6 +341,7 @@ char *xasprintf(const char *format, ...)
// close that file. // close that file.
void xprint_and_close_file(FILE *file) void xprint_and_close_file(FILE *file)
{ {
fflush(stdout);
// copyfd outputs error messages for us. // copyfd outputs error messages for us.
if (bb_copyfd_eof(fileno(file), 1) == -1) if (bb_copyfd_eof(fileno(file), 1) == -1)
exit(xfunc_error_retval); exit(xfunc_error_retval);

View File

@ -54,7 +54,7 @@ static void del_line_matching(const char *login, const char *filename)
struct stat statbuf; struct stat statbuf;
if ((passwd = bb_wfopen(filename, "r"))) { if ((passwd = fopen_or_warn(filename, "r"))) {
// Remove pointless const. // Remove pointless const.
xstat((char *)filename, &statbuf); xstat((char *)filename, &statbuf);
buffer = (char *) xmalloc(statbuf.st_size * sizeof(char)); 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); b = boundary(buffer, login);
if (b.stop != 0) { if (b.stop != 0) {
/* write the file w/o the user */ /* 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.start - 1), sizeof(char), passwd);
fwrite(&buffer[b.stop], (statbuf.st_size - b.stop), sizeof(char), passwd); fwrite(&buffer[b.stop], (statbuf.st_size - b.stop), sizeof(char), passwd);
fclose(passwd); fclose(passwd);

View File

@ -20,7 +20,7 @@ int readahead_main(int argc, char **argv)
if (argc == 1) bb_show_usage(); if (argc == 1) bb_show_usage();
while (*++argv) { while (*++argv) {
if ((f = bb_wfopen(*argv, "r")) != NULL) { if ((f = fopen_or_warn(*argv, "r")) != NULL) {
int r, fd=fileno(f); int r, fd=fileno(f);
r = readahead(fd, 0, fdlength(fd)); r = readahead(fd, 0, fdlength(fd));

View File

@ -45,7 +45,7 @@ int strings_main(int argc, char **argv)
} }
do { do {
file = bb_wfopen(*argv, "r"); file = fopen_or_warn(*argv, "r");
if (file) { if (file) {
PIPE: PIPE:
count = 0; count = 0;
@ -75,7 +75,7 @@ PIPE:
} }
count++; count++;
} while (c != EOF); } while (c != EOF);
bb_fclose_nonstdin(file); fclose_if_not_stdin(file);
} else { } else {
status = EXIT_FAILURE; status = EXIT_FAILURE;
} }

View File

@ -426,7 +426,7 @@ static int builtin_source(struct child_prog *child)
FILE *input; FILE *input;
int status; int status;
input = bb_wfopen(child->argv[1], "r"); input = fopen_or_warn(child->argv[1], "r");
if (!input) { if (!input) {
return EXIT_FAILURE; 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)); llist_add_to(&close_me_list, (void *)(long)fileno(prof_input));
/* Now run the file */ /* Now run the file */
busy_loop(prof_input); busy_loop(prof_input);
bb_fclose_nonstdin(prof_input); fclose_if_not_stdin(prof_input);
llist_pop(&close_me_list); llist_pop(&close_me_list);
} }
} }

View File

@ -2771,7 +2771,7 @@ tryprocpt(void)
char line[100], ptname[100], devname[120], *s; char line[100], ptname[100], devname[120], *s;
int ma, mi, sz; int ma, mi, sz;
procpt = bb_wfopen("/proc/partitions", "r"); procpt = fopen_or_warn("/proc/partitions", "r");
while (fgets(line, sizeof(line), procpt)) { while (fgets(line, sizeof(line), procpt)) {
if (sscanf(line, " %d %d %d %[^\n ]", if (sscanf(line, " %d %d %d %[^\n ]",

View File

@ -81,7 +81,7 @@ int more_main(int argc, char **argv)
if (argc == 0) { if (argc == 0) {
file = stdin; file = stdin;
} else } else
file = bb_wfopen(*argv, "r"); file = fopen_or_warn(*argv, "r");
if(file==0) if(file==0)
goto loop; goto loop;