-
Notifications
You must be signed in to change notification settings - Fork 46
/
wc.c
185 lines (161 loc) · 3.72 KB
/
wc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* wc - count lines, words and characters Author: david Messer */
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#define isdigit(c) (c >= '0' && c <= '9')
#define isspace(c) (c == ' ' || c == '\t' || c == '\n')
/*
*
* Usage: wc [-lwc] [names]
*
* Flags:
* l - count lines.
* w - count words.
* c - count characters.
*
* Flags l, w, and c are default.
* Words are delimited by any non-alphabetic character.
*
* Released into the PUBLIC-dOMAIN 02/10/86
*
* If you find this program to be of use to you, a donation of
* whatever you think it is worth will be cheerfully accepted.
*
* Written by: david L. Messer
* P.O. Box 19130, Mpls, MN, 55119
* Program (heavily) modified by Andy Tanenbaum
*/
int lflag = 0; /* Count lines */
int wflag = 0; /* Count words */
int cflag = 0; /* Count characters */
int lcount; /* Count of lines */
int wcount; /* Count of words */
int ccount; /* Count of characters */
int ltotal; /* Total count of lines */
int wtotal; /* Total count of words */
int ctotal; /* Total count of characters */
void count()
{
register int c;
register int word = 0;
lcount = 0;
wcount = 0;
ccount = 0L;
while ((c = getc(stdin)) > 0)
{
ccount++;
if (isspace(c))
{
if (word)
wcount++;
word = 0;
}
else
{
word = 1;
}
if (c == '\n' || c == '\f')
lcount++;
}
ltotal += lcount;
wtotal += wcount;
ctotal += ccount;
}
void usage()
{
fprintf(stderr, "Usage: wc [-lwc] [name ...]\n");
exit(1);
}
int main(int argc, char *argv[])
{
int k;
char *cp;
int tflag, files;
int fd;
/* Get flags. */
files = argc - 1;
k = 1;
cp = argv[1];
if (cp && *cp++ == '-')
{
files--;
k++; /* points to first file */
while (*cp != 0)
{
switch (*cp)
{
case 'l':
lflag++;
break;
case 'w':
wflag++;
break;
case 'c':
cflag++;
break;
default:
usage();
}
cp++;
}
}
/* If no flags are set, treat as wc -lwc. */
if (!lflag && !wflag && !cflag)
{
lflag = 1;
wflag = 1;
cflag = 1;
}
// printf("argc %6d flags %6d %6d %6d\n", argc, lflag, wflag, cflag);
/* Process files. */
tflag = files >= 2; /* set if # files > 1 */
/* Check to see if input comes from std input. */
if (k >= argc)
{
count();
if (lflag)
printf(" %6d", lcount);
if (wflag)
printf(" %6d", wcount);
if (cflag)
printf(" %6d", ccount);
printf(" \n");
exit(0);
}
/* There is an explicit list of files. Loop on files. */
while (k < argc)
{
// fclose(stdin);
close(STDIN_FILENO);
if ((fd = open(argv[k], O_RDONLY)) == -1)
{
fprintf(stderr, "wc: cannot open %s\n", argv[k]);
k++;
continue;
}
else
{
/* Next file has been opened as std input. */
count();
if (lflag)
printf(" %6d", lcount);
if (wflag)
printf(" %6d", wcount);
if (cflag)
printf(" %6d", ccount);
printf(" %s\n", argv[k]);
}
k++;
}
if (tflag)
{
if (lflag)
printf(" %6d", ltotal);
if (wflag)
printf(" %6d", wtotal);
if (cflag)
printf(" %6d", ctotal);
printf(" total\n");
}
return 0;
}