unstack, sort: cleanup and improvement
[minix.git] / commands / wc / wc.c
blobb978d21c4e0ab8921015741c79eeffccdf326186
1 /* wc - count lines, words and characters Author: David Messer */
3 #include <ctype.h>
4 #include <stdlib.h>
5 #include <stdio.h>
7 /*
9 * Usage: wc [-lwc] [names]
11 * Flags:
12 * l - count lines.
13 * w - count words.
14 * c - count characters.
16 * Flags l, w, and c are default.
17 * Words are delimited by any non-alphabetic character.
19 * Released into the PUBLIC-DOMAIN 02/10/86
21 * If you find this program to be of use to you, a donation of
22 * whatever you think it is worth will be cheerfully accepted.
24 * Written by: David L. Messer
25 * P.O. Box 19130, Mpls, MN, 55119
26 * Program (heavily) modified by Andy Tanenbaum
30 int lflag; /* Count lines */
31 int wflag; /* Count words */
32 int cflag; /* Count characters */
34 long lcount; /* Count of lines */
35 long wcount; /* Count of words */
36 long ccount; /* Count of characters */
38 long ltotal; /* Total count of lines */
39 long wtotal; /* Total count of words */
40 long ctotal; /* Total count of characters */
42 int main(int argc, char **argv);
43 void count(FILE *f);
44 void usage(void);
46 int main(argc, argv)
47 int argc;
48 char *argv[];
50 int k;
51 char *cp;
52 int tflag, files;
54 /* Get flags. */
55 files = argc - 1;
56 k = 1;
57 cp = argv[1];
58 if (argc > 1 && *cp++ == '-') {
59 files--;
60 k++; /* points to first file */
61 while (*cp != 0) {
62 switch (*cp) {
63 case 'l': lflag++; break;
64 case 'w': wflag++; break;
65 case 'c': cflag++; break;
66 default: usage();
68 cp++;
72 /* If no flags are set, treat as wc -lwc. */
73 if (!lflag && !wflag && !cflag) {
74 lflag = 1;
75 wflag = 1;
76 cflag = 1;
79 /* Process files. */
80 tflag = files >= 2; /* set if # files > 1 */
82 /* Check to see if input comes from std input. */
83 if (k >= argc) {
84 count(stdin);
85 if (lflag) printf(" %6ld", lcount);
86 if (wflag) printf(" %6ld", wcount);
87 if (cflag) printf(" %6ld", ccount);
88 printf(" \n");
89 fflush(stdout);
90 exit(0);
93 /* There is an explicit list of files. Loop on files. */
94 while (k < argc) {
95 FILE *f;
97 if ((f = fopen(argv[k], "r")) == NULL) {
98 fprintf(stderr, "wc: cannot open %s\n", argv[k]);
99 } else {
100 count(f);
101 if (lflag) printf(" %6ld", lcount);
102 if (wflag) printf(" %6ld", wcount);
103 if (cflag) printf(" %6ld", ccount);
104 printf(" %s\n", argv[k]);
105 fclose(f);
107 k++;
110 if (tflag) {
111 if (lflag) printf(" %6ld", ltotal);
112 if (wflag) printf(" %6ld", wtotal);
113 if (cflag) printf(" %6ld", ctotal);
114 printf(" total\n");
116 fflush(stdout);
117 return(0);
120 void count(f)
121 FILE *f;
123 register int c;
124 register int word = 0;
126 lcount = 0;
127 wcount = 0;
128 ccount = 0L;
130 while ((c = getc(f)) != EOF) {
131 ccount++;
133 if (isspace(c)) {
134 if (word) wcount++;
135 word = 0;
136 } else {
137 word = 1;
140 if (c == '\n' || c == '\f') lcount++;
142 ltotal += lcount;
143 wtotal += wcount;
144 ctotal += ccount;
147 void usage()
149 fprintf(stderr, "Usage: wc [-lwc] [name ...]\n");
150 exit(1);