/*
* 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 <stdio.h>
-
-static const char wc_usage[] = "wc [OPTION]... [FILE]...\n\n"
- "Print line, word, and byte counts for each FILE, and a total line if\n"
- "more than one FILE is specified. With no FILE, read standard input.\n"
- "\t-c\tprint the byte counts\n"
- "\t-l\tprint the newline counts\n"
-
- "\t-L\tprint the length of the longest line\n"
- "\t-w\tprint the word counts\n";
+#include <getopt.h>
+#include <string.h>
+#include <stdlib.h>
+#include "busybox.h"
static int total_lines, total_words, total_chars, max_length;
static int print_lines, print_words, print_chars, print_length;
-void print_counts(int lines, int words, int chars, int length,
- const char *name)
+static void print_counts(int lines, int words, int chars, int length,
+ const char *name)
{
char const *space = "";
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) {
- perror(*argv);
- exit(FALSE);
- }
- wc_file(file, *argv);
+ return EXIT_SUCCESS;
} else {
- while (argc-- > 0 && *argv != '\0' && strlen(*argv)) {
- file = fopen(*argv, "r");
- if (file == NULL) {
- perror(*argv);
- exit(FALSE);
- }
- 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");
- }
- exit(TRUE);
+
+ return status;
}