summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen2000-10-09 18:21:44 +0000
committerEric Andersen2000-10-09 18:21:44 +0000
commit7a86e61a54f75c6c457be177fb20f6dde27c06d3 (patch)
treee307b6cec6e7143fd03604aa03384001751ddb33
parent62f987e95f497a21bc1455153dd156aa12718574 (diff)
downloadbusybox-7a86e61a54f75c6c457be177fb20f6dde27c06d3.zip
busybox-7a86e61a54f75c6c457be177fb20f6dde27c06d3.tar.gz
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
-rw-r--r--coreutils/wc.c10
-rw-r--r--utility.c2
-rw-r--r--wc.c10
3 files changed, 13 insertions, 9 deletions
diff --git a/coreutils/wc.c b/coreutils/wc.c
index 9d56945..e6f7534 100644
--- a/coreutils/wc.c
+++ b/coreutils/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;
}
diff --git a/utility.c b/utility.c
index 9a30375..11ade98 100644
--- 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 9d56945..e6f7534 100644
--- 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;
}