diff options
author | Erik Andersen <andersen@codepoet.org> | 2000-02-08 19:58:47 +0000 |
---|---|---|
committer | Erik Andersen <andersen@codepoet.org> | 2000-02-08 19:58:47 +0000 |
commit | e49d5ecbbe51718fa925b6890a735e5937cc2aa2 (patch) | |
tree | c90bda10731ad9333ce3b404f993354c9fc104b8 /wc.c | |
parent | c0bf817bbc5c7867fbe8fb76d5c39f8ee802692f (diff) | |
download | busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.gz busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.bz2 busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.zip |
Some formatting updates (ran the code through indent)
-Erik
Diffstat (limited to 'wc.c')
-rw-r--r-- | wc.c | 127 |
1 files changed, 66 insertions, 61 deletions
@@ -1,3 +1,4 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
1 | /* | 2 | /* |
2 | * Mini wc implementation for busybox | 3 | * Mini wc implementation for busybox |
3 | * | 4 | * |
@@ -23,78 +24,82 @@ | |||
23 | #include <stdio.h> | 24 | #include <stdio.h> |
24 | 25 | ||
25 | static const char wc_usage[] = "wc [OPTION]... [FILE]...\n\n" | 26 | static const char wc_usage[] = "wc [OPTION]... [FILE]...\n\n" |
26 | "Print line, word, and byte counts for each FILE, and a total line if\n" | 27 | "Print line, word, and byte counts for each FILE, and a total line if\n" |
27 | "more than one FILE is specified. With no FILE, read standard input.\n" | 28 | "more than one FILE is specified. With no FILE, read standard input.\n" |
28 | "\t-c\tprint the byte counts\n" | 29 | "\t-c\tprint the byte counts\n" |
29 | "\t-l\tprint the newline counts\n" | 30 | "\t-l\tprint the newline counts\n" |
30 | "\t-L\tprint the length of the longest line\n" | 31 | |
31 | "\t-w\tprint the word counts\n"; | 32 | "\t-L\tprint the length of the longest line\n" |
33 | "\t-w\tprint the word counts\n"; | ||
32 | 34 | ||
33 | static int total_lines, total_words, total_chars, max_length; | 35 | static int total_lines, total_words, total_chars, max_length; |
34 | static int print_lines, print_words, print_chars, print_length; | 36 | static int print_lines, print_words, print_chars, print_length; |
35 | 37 | ||
36 | void print_counts (int lines, int words, int chars, int length, | 38 | void print_counts(int lines, int words, int chars, int length, |
37 | const char *name) { | 39 | const char *name) |
40 | { | ||
38 | char const *space = ""; | 41 | char const *space = ""; |
42 | |||
39 | if (print_lines) { | 43 | if (print_lines) { |
40 | printf ("%7d", lines); | 44 | printf("%7d", lines); |
41 | space = " "; | 45 | space = " "; |
42 | } | 46 | } |
43 | if (print_words) { | 47 | if (print_words) { |
44 | printf ("%s%7d", space, words); | 48 | printf("%s%7d", space, words); |
45 | space = " "; | 49 | space = " "; |
46 | } | 50 | } |
47 | if (print_chars) { | 51 | if (print_chars) { |
48 | printf ("%s%7d", space, chars); | 52 | printf("%s%7d", space, chars); |
49 | space = " "; | 53 | space = " "; |
50 | } | 54 | } |
51 | if (print_length) | 55 | if (print_length) |
52 | printf ("%s%7d", space, length); | 56 | printf("%s%7d", space, length); |
53 | if (*name) | 57 | if (*name) |
54 | printf (" %s", name); | 58 | printf(" %s", name); |
55 | putchar ('\n'); | 59 | putchar('\n'); |
56 | } | 60 | } |
57 | 61 | ||
58 | static void wc_file(FILE *file, const char *name) | 62 | static void wc_file(FILE * file, const char *name) |
59 | { | 63 | { |
60 | int lines, words, chars, length; | 64 | int lines, words, chars, length; |
61 | int in_word = 0, linepos = 0; | 65 | int in_word = 0, linepos = 0; |
62 | int c; | 66 | int c; |
67 | |||
63 | lines = words = chars = length = 0; | 68 | lines = words = chars = length = 0; |
64 | while ((c = getc(file)) != EOF) { | 69 | while ((c = getc(file)) != EOF) { |
65 | chars++; | 70 | chars++; |
66 | switch (c) { | 71 | switch (c) { |
67 | case '\n': | 72 | case '\n': |
68 | lines++; | 73 | lines++; |
69 | case '\r': | 74 | case '\r': |
70 | case '\f': | 75 | case '\f': |
71 | if (linepos > length) | 76 | if (linepos > length) |
72 | length = linepos; | 77 | length = linepos; |
73 | linepos = 0; | 78 | linepos = 0; |
74 | goto word_separator; | 79 | goto word_separator; |
75 | case '\t': | 80 | case '\t': |
76 | linepos += 8 - (linepos % 8); | 81 | linepos += 8 - (linepos % 8); |
77 | goto word_separator; | 82 | goto word_separator; |
78 | case ' ': | 83 | case ' ': |
79 | linepos++; | 84 | linepos++; |
80 | case '\v': | 85 | case '\v': |
81 | word_separator: | 86 | word_separator: |
82 | if (in_word) { | 87 | if (in_word) { |
83 | in_word = 0; | 88 | in_word = 0; |
84 | words++; | 89 | words++; |
85 | } | 90 | } |
86 | break; | 91 | break; |
87 | default: | 92 | default: |
88 | linepos++; | 93 | linepos++; |
89 | in_word = 1; | 94 | in_word = 1; |
90 | break; | 95 | break; |
91 | } | 96 | } |
92 | } | 97 | } |
93 | if (linepos > length) | 98 | if (linepos > length) |
94 | length = linepos; | 99 | length = linepos; |
95 | if (in_word) | 100 | if (in_word) |
96 | words++; | 101 | words++; |
97 | print_counts (lines, words, chars, length, name); | 102 | print_counts(lines, words, chars, length, name); |
98 | total_lines += lines; | 103 | total_lines += lines; |
99 | total_words += words; | 104 | total_words += words; |
100 | total_chars += chars; | 105 | total_chars += chars; |
@@ -104,28 +109,30 @@ static void wc_file(FILE *file, const char *name) | |||
104 | fflush(stdout); | 109 | fflush(stdout); |
105 | } | 110 | } |
106 | 111 | ||
107 | int wc_main(int argc, char **argv) { | 112 | int wc_main(int argc, char **argv) |
113 | { | ||
108 | FILE *file; | 114 | FILE *file; |
115 | |||
109 | total_lines = total_words = total_chars = max_length = 0; | 116 | total_lines = total_words = total_chars = max_length = 0; |
110 | print_lines = print_words = print_chars = print_length = 0; | 117 | print_lines = print_words = print_chars = print_length = 0; |
111 | 118 | ||
112 | while (--argc && **(++argv) == '-') { | 119 | while (--argc && **(++argv) == '-') { |
113 | while (*++(*argv)) | 120 | while (*++(*argv)) |
114 | switch (**argv) { | 121 | switch (**argv) { |
115 | case 'c': | 122 | case 'c': |
116 | print_chars = 1; | 123 | print_chars = 1; |
117 | break; | 124 | break; |
118 | case 'l': | 125 | case 'l': |
119 | print_lines = 1; | 126 | print_lines = 1; |
120 | break; | 127 | break; |
121 | case 'L': | 128 | case 'L': |
122 | print_length = 1; | 129 | print_length = 1; |
123 | break; | 130 | break; |
124 | case 'w': | 131 | case 'w': |
125 | print_words = 1; | 132 | print_words = 1; |
126 | break; | 133 | break; |
127 | default: | 134 | default: |
128 | usage (wc_usage); | 135 | usage(wc_usage); |
129 | } | 136 | } |
130 | } | 137 | } |
131 | 138 | ||
@@ -135,16 +142,14 @@ int wc_main(int argc, char **argv) { | |||
135 | if (argc == 0) { | 142 | if (argc == 0) { |
136 | wc_file(stdin, ""); | 143 | wc_file(stdin, ""); |
137 | exit(TRUE); | 144 | exit(TRUE); |
138 | } | 145 | } else if (argc == 1) { |
139 | else if (argc == 1) { | ||
140 | file = fopen(*argv, "r"); | 146 | file = fopen(*argv, "r"); |
141 | if (file == NULL) { | 147 | if (file == NULL) { |
142 | perror(*argv); | 148 | perror(*argv); |
143 | exit(FALSE); | 149 | exit(FALSE); |
144 | } | 150 | } |
145 | wc_file(file, *argv); | 151 | wc_file(file, *argv); |
146 | } | 152 | } else { |
147 | else { | ||
148 | while (argc-- > 0 && *argv != '\0' && strlen(*argv)) { | 153 | while (argc-- > 0 && *argv != '\0' && strlen(*argv)) { |
149 | file = fopen(*argv, "r"); | 154 | file = fopen(*argv, "r"); |
150 | if (file == NULL) { | 155 | if (file == NULL) { |
@@ -154,8 +159,8 @@ int wc_main(int argc, char **argv) { | |||
154 | wc_file(file, *argv); | 159 | wc_file(file, *argv); |
155 | argv++; | 160 | argv++; |
156 | } | 161 | } |
157 | print_counts (total_lines, total_words, total_chars, | 162 | print_counts(total_lines, total_words, total_chars, |
158 | max_length, "total"); | 163 | max_length, "total"); |
159 | } | 164 | } |
160 | exit(TRUE); | 165 | exit(TRUE); |
161 | } | 166 | } |