Patch from Matt Kraai so wc will return a proper error code
authorEric Andersen <andersen@codepoet.org>
Mon, 9 Oct 2000 18:21:44 +0000 (18:21 -0000)
committerEric Andersen <andersen@codepoet.org>
Mon, 9 Oct 2000 18:21:44 +0000 (18:21 -0000)
when failing to open a file, and will not use file when it
didn't open the file.
 -Erik

coreutils/wc.c
utility.c
wc.c

index 9d569459de63bc5cb9bda2af312b494835a962b6..e6f753435610fa68bcd92a622c82bd8fbab83f32 100644 (file)
@@ -105,7 +105,7 @@ int wc_main(int argc, char **argv)
 {
        FILE *file;
        unsigned int num_files_counted = 0;
-       int opt;
+       int opt, status = EXIT_SUCCESS;
 
        total_lines = total_words = total_chars = max_length = 0;
        print_lines = print_words = print_chars = print_length = 0;
@@ -137,8 +137,10 @@ int wc_main(int argc, char **argv)
                return EXIT_SUCCESS;
        } else {
                while (optind < argc) {
-                       file = xfopen(argv[optind], "r");
-                       wc_file(file, argv[optind]);
+                       if ((file = wfopen(argv[optind], "r")) != NULL)
+                               wc_file(file, argv[optind]);
+                       else
+                               status = EXIT_FAILURE;
                        num_files_counted++;
                        optind++;
                }
@@ -148,5 +150,5 @@ int wc_main(int argc, char **argv)
                print_counts(total_lines, total_words, total_chars,
                                         max_length, "total");
 
-       return EXIT_SUCCESS;
+       return status;
 }
index 9a303751d5af1e5356393799f644272bb5b63cd4..11ade982b54803d999e8bb977d1a461f2eacfae2 100644 (file)
--- a/utility.c
+++ b/utility.c
@@ -1721,7 +1721,7 @@ void xregcomp(regex_t *preg, const char *regex, int cflags)
 }
 #endif
 
-#if defined BB_CAT || defined BB_HEAD
+#if defined BB_CAT || defined BB_HEAD || defined BB_WC
 FILE *wfopen(const char *path, const char *mode)
 {
        FILE *fp;
diff --git a/wc.c b/wc.c
index 9d569459de63bc5cb9bda2af312b494835a962b6..e6f753435610fa68bcd92a622c82bd8fbab83f32 100644 (file)
--- a/wc.c
+++ b/wc.c
@@ -105,7 +105,7 @@ int wc_main(int argc, char **argv)
 {
        FILE *file;
        unsigned int num_files_counted = 0;
-       int opt;
+       int opt, status = EXIT_SUCCESS;
 
        total_lines = total_words = total_chars = max_length = 0;
        print_lines = print_words = print_chars = print_length = 0;
@@ -137,8 +137,10 @@ int wc_main(int argc, char **argv)
                return EXIT_SUCCESS;
        } else {
                while (optind < argc) {
-                       file = xfopen(argv[optind], "r");
-                       wc_file(file, argv[optind]);
+                       if ((file = wfopen(argv[optind], "r")) != NULL)
+                               wc_file(file, argv[optind]);
+                       else
+                               status = EXIT_FAILURE;
                        num_files_counted++;
                        optind++;
                }
@@ -148,5 +150,5 @@ int wc_main(int argc, char **argv)
                print_counts(total_lines, total_words, total_chars,
                                         max_length, "total");
 
-       return EXIT_SUCCESS;
+       return status;
 }