Use enums for selected functionality, Reduce the size by nearly 100 Bytes
authorGlenn L McGrath <bug1@ihug.co.nz>
Wed, 21 Nov 2001 09:17:00 +0000 (09:17 -0000)
committerGlenn L McGrath <bug1@ihug.co.nz>
Wed, 21 Nov 2001 09:17:00 +0000 (09:17 -0000)
coreutils/wc.c

index 695e7e7d4e4b765b1954d5ecb5d9ea11f1128a10..03dd3c3af7a8087a68757359c1f026e75e72c15d 100644 (file)
 #include "busybox.h"
 
 static int total_lines, total_words, total_chars, max_length;
-static int print_lines, print_words, print_chars, print_length;
+//static int print_lines, print_words, print_chars, print_length;
+static char print_type = 0;
+enum print_e {
+       print_lines = 1,
+       print_words = 2,
+       print_chars = 4,
+       print_length = 8
+};
 
 static void print_counts(int lines, int words, int chars, int length,
                                                 const char *name)
 {
        char const *space = "";
 
-       if (print_lines) {
+       if (print_type & print_lines) {
                printf("%7d", lines);
                space = " ";
        }
-       if (print_words) {
+       if (print_type & print_words) {
                printf("%s%7d", space, words);
                space = " ";
        }
-       if (print_chars) {
+       if (print_type & print_chars) {
                printf("%s%7d", space, chars);
                space = " ";
        }
-       if (print_length)
+       if (print_type & print_length)
                printf("%s%7d", space, length);
        if (*name)
                printf(" %s", name);
@@ -110,36 +117,37 @@ int wc_main(int argc, char **argv)
        int opt, status = EXIT_SUCCESS;
 
        total_lines = total_words = total_chars = max_length = 0;
-       print_lines = print_words = print_chars = print_length = 0;
 
        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:
                                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 (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
                wc_file(stdin, "");
                return EXIT_SUCCESS;
        } else {
                while (optind < argc) {
-                       if ((file = wfopen(argv[optind], "r")) != NULL)
+                       file = wfopen(argv[optind], "r");
+                       if (file != NULL)
                                wc_file(file, argv[optind]);
                        else
                                status = EXIT_FAILURE;