Consolidate handling of some fopen failures.
authorMatt Kraai <kraai@debian.org>
Wed, 27 Sep 2000 02:43:35 +0000 (02:43 -0000)
committerMatt Kraai <kraai@debian.org>
Wed, 27 Sep 2000 02:43:35 +0000 (02:43 -0000)
14 files changed:
console-tools/loadacm.c
coreutils/wc.c
editors/sed.c
hostname.c
lash.c
loadacm.c
more.c
networking/hostname.c
sed.c
sh.c
shell/lash.c
util-linux/more.c
utility.c
wc.c

index 826c2a33fc714bc8d878059fadf4b8036264f785..f57737925291dd4345891c26c72434b73751f80b 100644 (file)
@@ -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
index b1c9a51cdfec21d2b57f44ee848faf4a7a215223..9d569459de63bc5cb9bda2af312b494835a962b6 100644 (file)
@@ -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 ;
+       return EXIT_SUCCESS;
 }
index cb3f1bbf15558d849e66bc4130c6dd8a4ad40233..75435f67add5bf451cf908efe9b54bd1e62261ad 100644 (file)
@@ -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 */
index 4320a968c364e1dfd9f838b2449f6289c71188b6..16a28ca43dacaa135aa74d42e9f5ce5da81a3430 100644 (file)
@@ -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 <tausq@debian.org>
@@ -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 77efc6aaccc649e647da4a3a216ca1100fb037f9..b1061425992be911094aa5b119d45da9914c43ed 100644 (file)
--- 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...*/
index 826c2a33fc714bc8d878059fadf4b8036264f785..f57737925291dd4345891c26c72434b73751f80b 100644 (file)
--- 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 caabc44e21aa4aa592b226b0192c5b32cf35a276..f95cb48ad07ca444a8b20938b3c474c8188d93d0 100644 (file)
--- 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
index 4320a968c364e1dfd9f838b2449f6289c71188b6..16a28ca43dacaa135aa74d42e9f5ce5da81a3430 100644 (file)
@@ -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 <tausq@debian.org>
@@ -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 cb3f1bbf15558d849e66bc4130c6dd8a4ad40233..75435f67add5bf451cf908efe9b54bd1e62261ad 100644 (file)
--- 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 77efc6aaccc649e647da4a3a216ca1100fb037f9..b1061425992be911094aa5b119d45da9914c43ed 100644 (file)
--- 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...*/
index 77efc6aaccc649e647da4a3a216ca1100fb037f9..b1061425992be911094aa5b119d45da9914c43ed 100644 (file)
@@ -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...*/
index caabc44e21aa4aa592b226b0192c5b32cf35a276..f95cb48ad07ca444a8b20938b3c474c8188d93d0 100644 (file)
@@ -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
index d376a04fb5382bcdd36d96d6798708bd64e9ff84..ae69baf4b489f05246ebf2392d3cbf65642cc2bd 100644 (file)
--- 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 b1c9a51cdfec21d2b57f44ee848faf4a7a215223..9d569459de63bc5cb9bda2af312b494835a962b6 100644 (file)
--- 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 ;
+       return EXIT_SUCCESS;
 }