source: trunk/minix/commands/simple/wc.c@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 2.9 KB
RevLine 
[9]1/* wc - count lines, words and characters Author: David Messer */
2
3#include <ctype.h>
4#include <stdlib.h>
5#include <stdio.h>
6
7/*
8 *
9 * Usage: wc [-lwc] [names]
10 *
11 * Flags:
12 * l - count lines.
13 * w - count words.
14 * c - count characters.
15 *
16 * Flags l, w, and c are default.
17 * Words are delimited by any non-alphabetic character.
18 *
19 * Released into the PUBLIC-DOMAIN 02/10/86
20 *
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.
23 *
24 * Written by: David L. Messer
25 * P.O. Box 19130, Mpls, MN, 55119
26 * Program (heavily) modified by Andy Tanenbaum
27 */
28
29
30int lflag; /* Count lines */
31int wflag; /* Count words */
32int cflag; /* Count characters */
33
34long lcount; /* Count of lines */
35long wcount; /* Count of words */
36long ccount; /* Count of characters */
37
38long ltotal; /* Total count of lines */
39long wtotal; /* Total count of words */
40long ctotal; /* Total count of characters */
41
42_PROTOTYPE(int main, (int argc, char **argv));
43_PROTOTYPE(void count, (FILE *f));
44_PROTOTYPE(void usage, (void));
45
46int main(argc, argv)
47int argc;
48char *argv[];
49{
50 int k;
51 char *cp;
52 int tflag, files;
53
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();
67 }
68 cp++;
69 }
70 }
71
72 /* If no flags are set, treat as wc -lwc. */
73 if (!lflag && !wflag && !cflag) {
74 lflag = 1;
75 wflag = 1;
76 cflag = 1;
77 }
78
79 /* Process files. */
80 tflag = files >= 2; /* set if # files > 1 */
81
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);
91 }
92
93 /* There is an explicit list of files. Loop on files. */
94 while (k < argc) {
95 FILE *f;
96
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);
106 }
107 k++;
108 }
109
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");
115 }
116 fflush(stdout);
117 return(0);
118}
119
120void count(f)
121FILE *f;
122{
123 register int c;
124 register int word = 0;
125
126 lcount = 0;
127 wcount = 0;
128 ccount = 0L;
129
130 while ((c = getc(f)) != EOF) {
131 ccount++;
132
133 if (isspace(c)) {
134 if (word) wcount++;
135 word = 0;
136 } else {
137 word = 1;
138 }
139
140 if (c == '\n' || c == '\f') lcount++;
141 }
142 ltotal += lcount;
143 wtotal += wcount;
144 ctotal += ccount;
145}
146
147void usage()
148{
149 fprintf(stderr, "Usage: wc [-lwc] [name ...]\n");
150 exit(1);
151}
Note: See TracBrowser for help on using the repository browser.