Reorganise, make it just one function, remove -v option it didnt work properly anyway...
[oweals/busybox.git] / coreutils / wc.c
index bad03f79152fafb2be918ee223ccbf3eed4d67c5..8e3b5bbf438070cfab3b7d892b723c2363fc63c8 100644 (file)
@@ -2,7 +2,7 @@
 /*
  * 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>
+#include <getopt.h>
+#include <string.h>
+#include <stdlib.h>
+#include "busybox.h"
 
-static const char wc_usage[] = "wc [OPTION]... [FILE]...\n"
-#ifndef BB_FEATURE_TRIVIAL_HELP
-       "\nPrint 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\n"
-       "Options:\n"
-       "\t-c\tprint the byte counts\n"
-       "\t-l\tprint the newline counts\n"
+enum print_e {
+       print_lines = 1,
+       print_words = 2,
+       print_chars = 4,
+       print_length = 8
+};
 
-       "\t-L\tprint the length of the longest line\n"
-       "\t-w\tprint the word counts\n"
-#endif
-       ;
+static unsigned int total_lines = 0;
+static unsigned int total_words = 0;
+static unsigned int total_chars = 0;
+static unsigned int max_length = 0;
+static char print_type = 0;
 
-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(const unsigned int lines, const unsigned int words,
+       const unsigned int chars, const unsigned int length, const char *name)
 {
-       char const *space = "";
+       int output = 0;
 
-       if (print_lines) {
+       if (print_type & print_lines) {
                printf("%7d", lines);
-               space = " ";
+               output++;
+       }
+       if (print_type & print_words) {
+               if (output++)
+                       putchar(' ');
+               printf("%7d", words);
        }
-       if (print_words) {
-               printf("%s%7d", space, words);
-               space = " ";
+       if (print_type & print_chars) {
+               if (output++)
+                       putchar(' ');
+               printf("%7d", chars);
        }
-       if (print_chars) {
-               printf("%s%7d", space, chars);
-               space = " ";
+       if (print_type & print_length) {
+               if (output++)
+                       putchar(' ');
+               printf("%7d", length);
        }
-       if (print_length)
-               printf("%s%7d", space, length);
-       if (*name)
+       if (*name) {
                printf(" %s", name);
+       }
        putchar('\n');
 }
 
 static void wc_file(FILE * file, const char *name)
 {
-       int lines, words, chars, length;
-       int in_word = 0, linepos = 0;
+       unsigned int lines = 0;
+       unsigned int words = 0;
+       unsigned int chars = 0;
+       unsigned int length = 0;
+       unsigned int linepos = 0;
+       char in_word = 0;
        int c;
 
-       lines = words = chars = length = 0;
        while ((c = getc(file)) != EOF) {
                chars++;
                switch (c) {
@@ -87,7 +96,7 @@ static void wc_file(FILE * file, const char *name)
                case ' ':
                        linepos++;
                case '\v':
-                 word_separator:
+word_separator:
                        if (in_word) {
                                in_word = 0;
                                words++;
@@ -99,72 +108,71 @@ static void wc_file(FILE * file, const char *name)
                        break;
                }
        }
-       if (linepos > length)
+       if (linepos > length) {
                length = linepos;
-       if (in_word)
+       }
+       if (in_word) {
                words++;
+       }
        print_counts(lines, words, chars, length, name);
        total_lines += lines;
        total_words += words;
        total_chars += chars;
-       if (length > max_length)
+       if (length > max_length) {
                max_length = length;
-       fclose(file);
-       fflush(stdout);
+       }
 }
 
 int wc_main(int argc, char **argv)
 {
-       FILE *file;
-
-       total_lines = total_words = total_chars = max_length = 0;
-       print_lines = print_words = print_chars = print_length = 0;
+       int opt;
 
-       while (--argc && **(++argv) == '-') {
-               while (*++(*argv))
-                       switch (**argv) {
+       while ((opt = getopt(argc, argv, "clLw")) > 0) {
+               switch (opt) {
                        case 'c':
-                               print_chars = 1;
+                               print_type |= print_chars;
                                break;
                        case 'l':
-                               print_lines = 1;
+                               print_type |= print_lines;
                                break;
                        case 'L':
-                               print_length = 1;
+                               print_type |= print_length;
                                break;
                        case 'w':
-                               print_words = 1;
+                               print_type |= print_words;
                                break;
                        default:
-                               usage(wc_usage);
-                       }
+                               show_usage();
+               }
        }
 
-       if (!print_lines && !print_words && !print_chars && !print_length)
-               print_lines = print_words = print_chars = 1;
+       if (print_type == 0) {
+               print_type = print_lines | print_words | print_chars;
+       }
 
-       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);
        } else {
-               while (argc-- > 0 && *argv != '\0' && strlen(*argv)) {
-                       file = fopen(*argv, "r");
-                       if (file == NULL) {
-                               perror(*argv);
-                               exit(FALSE);
+               unsigned short num_files_counted = 0;
+               while (optind < argc) {
+                       if (print_type == print_chars) {
+                               struct stat statbuf;
+                               stat(argv[optind], &statbuf);
+                               print_counts(0, 0, statbuf.st_size, 0, argv[optind]);
+                               total_chars += statbuf.st_size;
+                       } else {
+                               FILE *file;
+                               file = xfopen(argv[optind], "r");
+                               wc_file(file, argv[optind]);
+                               fclose(file);
                        }
-                       wc_file(file, *argv);
-                       argv++;
+                       optind++;
+                       num_files_counted++;
+               }
+               if (num_files_counted > 1) {
+                       print_counts(total_lines, total_words, total_chars, max_length, "total");
                }
-               print_counts(total_lines, total_words, total_chars,
-                                        max_length, "total");
        }
-       return(TRUE);
+
+       return(EXIT_SUCCESS);
 }