From bbaef66b3f99213f06adf04df6b3e5e61278d75b Mon Sep 17 00:00:00 2001 From: Matt Kraai Date: Wed, 27 Sep 2000 02:43:35 +0000 Subject: [PATCH] Consolidate handling of some fopen failures. --- console-tools/loadacm.c | 5 +---- coreutils/wc.c | 9 +++------ editors/sed.c | 4 +--- hostname.c | 20 ++++++++------------ lash.c | 5 +---- loadacm.c | 5 +---- more.c | 6 +----- networking/hostname.c | 20 ++++++++------------ sed.c | 4 +--- sh.c | 5 +---- shell/lash.c | 5 +---- util-linux/more.c | 6 +----- utility.c | 2 +- wc.c | 9 +++------ 14 files changed, 32 insertions(+), 73 deletions(-) diff --git a/console-tools/loadacm.c b/console-tools/loadacm.c index 826c2a33f..f57737925 100644 --- a/console-tools/loadacm.c +++ b/console-tools/loadacm.c @@ -330,10 +330,7 @@ void saveoldmap(int fd, char *omfil) } #endif - if ((fp = fopen(omfil, "w")) == NULL) { - perror(omfil); - exit(1); - } + fp = xfopen(omfil, "w"); #ifdef GIO_UNISCRNMAP if (is_old_map) { #endif diff --git a/coreutils/wc.c b/coreutils/wc.c index b1c9a51cd..9d569459d 100644 --- a/coreutils/wc.c +++ b/coreutils/wc.c @@ -134,13 +134,10 @@ int wc_main(int argc, char **argv) if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) { wc_file(stdin, ""); - exit(TRUE); + return EXIT_SUCCESS; } else { while (optind < argc) { - file = fopen(argv[optind], "r"); - if (file == NULL) { - fatalError(argv[optind]); - } + file = xfopen(argv[optind], "r"); wc_file(file, argv[optind]); num_files_counted++; optind++; @@ -151,5 +148,5 @@ int wc_main(int argc, char **argv) print_counts(total_lines, total_words, total_chars, max_length, "total"); - return 0 ; + return EXIT_SUCCESS; } diff --git a/editors/sed.c b/editors/sed.c index cb3f1bbf1..75435f67a 100644 --- a/editors/sed.c +++ b/editors/sed.c @@ -472,9 +472,7 @@ static void load_cmd_file(char *filename) char *line; char *nextline; - cmdfile = fopen(filename, "r"); - if (cmdfile == NULL) - fatalError(strerror(errno)); + cmdfile = xfopen(filename, "r"); while ((line = get_line_from_file(cmdfile)) != NULL) { /* if a line ends with '\' it needs the next line appended to it */ diff --git a/hostname.c b/hostname.c index 4320a968c..16a28ca43 100644 --- a/hostname.c +++ b/hostname.c @@ -1,6 +1,6 @@ /* vi: set sw=4 ts=4: */ /* - * $Id: hostname.c,v 1.13 2000/09/25 21:45:57 andersen Exp $ + * $Id: hostname.c,v 1.14 2000/09/27 02:43:35 kraai Exp $ * Mini hostname implementation for busybox * * Copyright (C) 1999 by Randolph Chung @@ -46,18 +46,14 @@ void do_sethostname(char *s, int isfile) exit(1); } } else { - if ((f = fopen(s, "r")) == NULL) { - perror(s); + f = xfopen(s, "r"); + fgets(buf, 255, f); + fclose(f); + if (buf[strlen(buf) - 1] == '\n') + buf[strlen(buf) - 1] = 0; + if (sethostname(buf, strlen(buf)) < 0) { + perror("sethostname"); exit(1); - } else { - fgets(buf, 255, f); - fclose(f); - if (buf[strlen(buf) - 1] == '\n') - buf[strlen(buf) - 1] = 0; - if (sethostname(buf, strlen(buf)) < 0) { - perror("sethostname"); - exit(1); - } } } } diff --git a/lash.c b/lash.c index 77efc6aac..b10614259 100644 --- a/lash.c +++ b/lash.c @@ -1447,10 +1447,7 @@ int shell_main(int argc_l, char **argv_l) fprintf(stdout, "Enter 'help' for a list of built-in commands.\n\n"); } else if (local_pending_command==NULL) { //fprintf(stdout, "optind=%d argv[optind]='%s'\n", optind, argv[optind]); - input = fopen(argv[optind], "r"); - if (!input) { - fatalError("%s: %s\n", argv[optind], strerror(errno)); - } + input = xfopen(argv[optind], "r"); } /* initialize the cwd -- this is never freed...*/ diff --git a/loadacm.c b/loadacm.c index 826c2a33f..f57737925 100644 --- a/loadacm.c +++ b/loadacm.c @@ -330,10 +330,7 @@ void saveoldmap(int fd, char *omfil) } #endif - if ((fp = fopen(omfil, "w")) == NULL) { - perror(omfil); - exit(1); - } + fp = xfopen(omfil, "w"); #ifdef GIO_UNISCRNMAP if (is_old_map) { #endif diff --git a/more.c b/more.c index caabc44e2..f95cb48ad 100644 --- a/more.c +++ b/more.c @@ -82,12 +82,8 @@ extern int more_main(int argc, char **argv) if (argc == 0) { file = stdin; } else - file = fopen(*argv, "r"); + file = xfopen(*argv, "r"); - if (file == NULL) { - perror(*argv); - exit(FALSE); - } fstat(fileno(file), &st); #ifdef BB_FEATURE_USE_TERMIOS diff --git a/networking/hostname.c b/networking/hostname.c index 4320a968c..16a28ca43 100644 --- a/networking/hostname.c +++ b/networking/hostname.c @@ -1,6 +1,6 @@ /* vi: set sw=4 ts=4: */ /* - * $Id: hostname.c,v 1.13 2000/09/25 21:45:57 andersen Exp $ + * $Id: hostname.c,v 1.14 2000/09/27 02:43:35 kraai Exp $ * Mini hostname implementation for busybox * * Copyright (C) 1999 by Randolph Chung @@ -46,18 +46,14 @@ void do_sethostname(char *s, int isfile) exit(1); } } else { - if ((f = fopen(s, "r")) == NULL) { - perror(s); + f = xfopen(s, "r"); + fgets(buf, 255, f); + fclose(f); + if (buf[strlen(buf) - 1] == '\n') + buf[strlen(buf) - 1] = 0; + if (sethostname(buf, strlen(buf)) < 0) { + perror("sethostname"); exit(1); - } else { - fgets(buf, 255, f); - fclose(f); - if (buf[strlen(buf) - 1] == '\n') - buf[strlen(buf) - 1] = 0; - if (sethostname(buf, strlen(buf)) < 0) { - perror("sethostname"); - exit(1); - } } } } diff --git a/sed.c b/sed.c index cb3f1bbf1..75435f67a 100644 --- a/sed.c +++ b/sed.c @@ -472,9 +472,7 @@ static void load_cmd_file(char *filename) char *line; char *nextline; - cmdfile = fopen(filename, "r"); - if (cmdfile == NULL) - fatalError(strerror(errno)); + cmdfile = xfopen(filename, "r"); while ((line = get_line_from_file(cmdfile)) != NULL) { /* if a line ends with '\' it needs the next line appended to it */ diff --git a/sh.c b/sh.c index 77efc6aac..b10614259 100644 --- a/sh.c +++ b/sh.c @@ -1447,10 +1447,7 @@ int shell_main(int argc_l, char **argv_l) fprintf(stdout, "Enter 'help' for a list of built-in commands.\n\n"); } else if (local_pending_command==NULL) { //fprintf(stdout, "optind=%d argv[optind]='%s'\n", optind, argv[optind]); - input = fopen(argv[optind], "r"); - if (!input) { - fatalError("%s: %s\n", argv[optind], strerror(errno)); - } + input = xfopen(argv[optind], "r"); } /* initialize the cwd -- this is never freed...*/ diff --git a/shell/lash.c b/shell/lash.c index 77efc6aac..b10614259 100644 --- a/shell/lash.c +++ b/shell/lash.c @@ -1447,10 +1447,7 @@ int shell_main(int argc_l, char **argv_l) fprintf(stdout, "Enter 'help' for a list of built-in commands.\n\n"); } else if (local_pending_command==NULL) { //fprintf(stdout, "optind=%d argv[optind]='%s'\n", optind, argv[optind]); - input = fopen(argv[optind], "r"); - if (!input) { - fatalError("%s: %s\n", argv[optind], strerror(errno)); - } + input = xfopen(argv[optind], "r"); } /* initialize the cwd -- this is never freed...*/ diff --git a/util-linux/more.c b/util-linux/more.c index caabc44e2..f95cb48ad 100644 --- a/util-linux/more.c +++ b/util-linux/more.c @@ -82,12 +82,8 @@ extern int more_main(int argc, char **argv) if (argc == 0) { file = stdin; } else - file = fopen(*argv, "r"); + file = xfopen(*argv, "r"); - if (file == NULL) { - perror(*argv); - exit(FALSE); - } fstat(fileno(file), &st); #ifdef BB_FEATURE_USE_TERMIOS diff --git a/utility.c b/utility.c index d376a04fb..ae69baf4b 100644 --- a/utility.c +++ b/utility.c @@ -1718,7 +1718,7 @@ void xregcomp(regex_t *preg, const char *regex, int cflags) } #endif -#if defined BB_UNIQ +#if defined BB_HOSTNAME || defined BB_LOADACM || defined BB_MORE || defined BB_SED || defined BB_SH || defined BB_UNIQ || defined BB_WC FILE *xfopen(const char *path, const char *mode) { FILE *fp; diff --git a/wc.c b/wc.c index b1c9a51cd..9d569459d 100644 --- a/wc.c +++ b/wc.c @@ -134,13 +134,10 @@ int wc_main(int argc, char **argv) if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) { wc_file(stdin, ""); - exit(TRUE); + return EXIT_SUCCESS; } else { while (optind < argc) { - file = fopen(argv[optind], "r"); - if (file == NULL) { - fatalError(argv[optind]); - } + file = xfopen(argv[optind], "r"); wc_file(file, argv[optind]); num_files_counted++; optind++; @@ -151,5 +148,5 @@ int wc_main(int argc, char **argv) print_counts(total_lines, total_words, total_chars, max_length, "total"); - return 0 ; + return EXIT_SUCCESS; } -- 2.25.1