/*
* Mini wc implementation for busybox
*
- * by Edward Betts <edward@debian.org>
+ * Copyright (C) 2000 Edward Betts <edward@debian.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
*
*/
-#include "internal.h"
+#include "busybox.h"
#include <stdio.h>
+#include <getopt.h>
+#include <stdlib.h>
static int total_lines, total_words, total_chars, max_length;
static int print_lines, print_words, print_chars, print_length;
int wc_main(int argc, char **argv)
{
FILE *file;
+ unsigned int num_files_counted = 0;
+ int opt, status = EXIT_SUCCESS;
total_lines = total_words = total_chars = max_length = 0;
print_lines = print_words = print_chars = print_length = 0;
- while (--argc && **(++argv) == '-') {
- while (*++(*argv))
- switch (**argv) {
+ while ((opt = getopt(argc, argv, "clLw")) > 0) {
+ switch (opt) {
case 'c':
print_chars = 1;
break;
print_words = 1;
break;
default:
- usage(wc_usage);
+ show_usage();
}
}
if (!print_lines && !print_words && !print_chars && !print_length)
print_lines = print_words = print_chars = 1;
- if (argc == 0) {
+ if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
wc_file(stdin, "");
- exit(TRUE);
- } else if (argc == 1) {
- file = fopen(*argv, "r");
- if (file == NULL) {
- fatalError(*argv);
- }
- wc_file(file, *argv);
+ return EXIT_SUCCESS;
} else {
- while (argc-- > 0) {
- file = fopen(*argv, "r");
- if (file == NULL) {
- fatalError(*argv);
- }
- wc_file(file, *argv);
- argv++;
+ while (optind < argc) {
+ if ((file = wfopen(argv[optind], "r")) != NULL)
+ wc_file(file, argv[optind]);
+ else
+ status = EXIT_FAILURE;
+ num_files_counted++;
+ optind++;
}
+ }
+
+ if (num_files_counted > 1)
print_counts(total_lines, total_words, total_chars,
max_length, "total");
- }
- return(TRUE);
+
+ return status;
}