Patch from Matt Kraai so wc will return a proper error code

when failing to open a file, and will not use file when it
didn't open the file.
 -Erik
This commit is contained in:
Eric Andersen 2000-10-09 18:21:44 +00:00
parent 62f987e95f
commit 7a86e61a54
3 changed files with 13 additions and 9 deletions

View File

@ -105,7 +105,7 @@ int wc_main(int argc, char **argv)
{ {
FILE *file; FILE *file;
unsigned int num_files_counted = 0; unsigned int num_files_counted = 0;
int opt; int opt, status = EXIT_SUCCESS;
total_lines = total_words = total_chars = max_length = 0; total_lines = total_words = total_chars = max_length = 0;
print_lines = print_words = print_chars = print_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; return EXIT_SUCCESS;
} else { } else {
while (optind < argc) { while (optind < argc) {
file = xfopen(argv[optind], "r"); if ((file = wfopen(argv[optind], "r")) != NULL)
wc_file(file, argv[optind]); wc_file(file, argv[optind]);
else
status = EXIT_FAILURE;
num_files_counted++; num_files_counted++;
optind++; optind++;
} }
@ -148,5 +150,5 @@ int wc_main(int argc, char **argv)
print_counts(total_lines, total_words, total_chars, print_counts(total_lines, total_words, total_chars,
max_length, "total"); max_length, "total");
return EXIT_SUCCESS; return status;
} }

View File

@ -1721,7 +1721,7 @@ void xregcomp(regex_t *preg, const char *regex, int cflags)
} }
#endif #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 *wfopen(const char *path, const char *mode)
{ {
FILE *fp; FILE *fp;

8
wc.c
View File

@ -105,7 +105,7 @@ int wc_main(int argc, char **argv)
{ {
FILE *file; FILE *file;
unsigned int num_files_counted = 0; unsigned int num_files_counted = 0;
int opt; int opt, status = EXIT_SUCCESS;
total_lines = total_words = total_chars = max_length = 0; total_lines = total_words = total_chars = max_length = 0;
print_lines = print_words = print_chars = print_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; return EXIT_SUCCESS;
} else { } else {
while (optind < argc) { while (optind < argc) {
file = xfopen(argv[optind], "r"); if ((file = wfopen(argv[optind], "r")) != NULL)
wc_file(file, argv[optind]); wc_file(file, argv[optind]);
else
status = EXIT_FAILURE;
num_files_counted++; num_files_counted++;
optind++; optind++;
} }
@ -148,5 +150,5 @@ int wc_main(int argc, char **argv)
print_counts(total_lines, total_words, total_chars, print_counts(total_lines, total_words, total_chars,
max_length, "total"); max_length, "total");
return EXIT_SUCCESS; return status;
} }