|
|
1.1 root 1: /*
2: * wc - word count
3: * Counts characters, lines, and words in all
4: * of the input files.
5: *
6: * Usage: wc [-lwc] [name ...]
7: *
8: * Revised June '93 for POSIX.2: changed output format and uses isspace () to
9: * delimit words.
10: */
11:
12: #include <stdio.h>
13: #include <ctype.h>
14: #ifdef COHERENT
15: #include <access.h>
16: #endif
17:
18: char _cmdname[] = "wc";
19: int nfiles;
20: int lineflg;
21: int wordflg;
22: int charflg;
23: long chars, words, lines;
24: long tchars, twords, tlines;
25:
26: main(argc, argv)
27: char *argv[];
28: {
29: register int i = 0;
30: register char *ap;
31: register FILE *fp;
32:
33: while (argv[1] != NULL && *argv[1] == '-') {
34: for (ap = &argv[1][1]; *ap; ap++)
35: switch (*ap) {
36: case 'l':
37: lineflg = 1;
38: break;
39:
40: case 'w':
41: wordflg = 1;
42: break;
43:
44: case 'c':
45: charflg = 1;
46: break;
47:
48: default:
49: usage();
50: }
51: argv++;
52: argc--;
53: }
54: if (argc == 1) {
55: #ifdef MSDOS
56: _setbinary(stdin);
57: #endif
58: wc(stdin);
59: print(NULL);
60: } else {
61: argv++; /* adjust args */
62:
63: while (*argv)
64: {
65: #ifdef COHERENT
66: if (access(*argv, AREAD))
67: { fprintf(stderr, "wc: can't open %s\n", *argv);
68: *argv[0] = 0; /* flag arg as invalid */
69: }
70: #endif
71: i++; /* count the arg */
72: argv++; /* advance to next arg */
73: }
74:
75: argv -= i; /* back to start of filename args to process */
76: argc--;
77:
78: for (i=0; i<argc; i++) {
79: if (*argv[i] == 0) /* arg to skip? */
80: continue; /* yes */
81: if ((fp = fopen(argv[i], "rb")) == NULL) {
82: fprintf(stderr,
83: "wc: can't open %s\n", argv[i]);
84: continue;
85: }
86: nfiles++;
87: wc(fp);
88: print(argv[i]);
89: tchars += chars;
90: twords += words;
91: tlines += lines;
92: fclose(fp);
93: }
94: if (nfiles >= 2) {
95: chars = tchars;
96: words = twords;
97: lines = tlines;
98: print("Total");
99: }
100: }
101: }
102:
103: /*
104: * Count words, lines and characters in the input file
105: * descriptor and put into global variables.
106: */
107: wc(in)
108: register FILE *in;
109: {
110: register c;
111: int inw;
112:
113: inw = 0;
114: chars = words = lines = 0;
115: while ((c = getc(in)) != EOF) {
116: chars++;
117:
118: if (c == '\n')
119: lines++;
120:
121: if (isspace (c))
122: inw = 0;
123: else if (!inw) {
124: inw = 1;
125: words++;
126: }
127: }
128: }
129:
130: /*
131: * Print according to option flags to totals
132: */
133: print(name)
134: char *name;
135: {
136: if (!lineflg && !wordflg && !charflg)
137: printf("%7ld %7ld %7ld", lines, words, chars);
138: else {
139: if (lineflg) {
140: printf("%7ld", lines);
141: if (wordflg || charflg)
142: putchar (' ');
143: }
144: if (wordflg) {
145: printf("%7ld", words);
146: if (charflg)
147: putchar (' ');
148: }
149: if (charflg)
150: printf("%7ld", chars);
151: }
152: if (name != NULL)
153: printf(" %s", name);
154: printf("\n");
155: }
156:
157: usage()
158: {
159: fprintf(stderr, "Usage: wc [-lwc] [name ...]\n");
160: exit(1);
161: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.