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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* wc -- word and line count
*/
#include <stdio.h>
#include <limits.h>
#include <locale.h>
#include <wctype.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <euc.h>
#undef BUFSIZ
#define BUFSIZ 4096
unsigned char b[BUFSIZ];
FILE *fptr = stdin;
unsigned long long wordct;
unsigned long long twordct;
unsigned long long linect;
unsigned long long tlinect;
unsigned long long charct;
unsigned long long tcharct;
unsigned long long real_charct;
unsigned long long real_tcharct;
int cflag = 0, mflag = 0, lflag = 0, wflag = 0;
static void wcp(unsigned long long, unsigned long long,
unsigned long long, unsigned long long);
static void usage(void);
int
main(int argc, char **argv)
{
unsigned char *p1, *p2;
unsigned int c;
int flag;
int i, token;
int status = 0;
wchar_t wc;
int len, n, errflag;
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
#endif
(void) textdomain(TEXT_DOMAIN);
while ((flag = getopt(argc, argv, "cCmlw")) != EOF) {
switch (flag) {
case 'c':
if (mflag)
usage();
cflag++;
break;
case 'C':
case 'm': /* POSIX.2 */
if (cflag)
usage();
mflag++;
break;
case 'l':
lflag++;
break;
case 'w':
wflag++;
break;
default:
usage();
break;
}
}
argc -= optind;
argv = &argv[optind];
/*
* If no flags set, use defaults
*/
if (cflag == 0 && mflag == 0 && lflag == 0 && wflag == 0) {
cflag = 1;
lflag = 1;
wflag = 1;
}
i = 0;
do {
if (argc > 0 && (fptr = fopen(argv[i], "r")) == NULL) {
(void) fprintf(stderr, "wc: %s: %s\n",
argv[i], strerror(errno));
status = 2;
continue;
}
p1 = p2 = b;
linect = 0;
wordct = 0;
charct = 0;
real_charct = 0;
token = 0;
errflag = 0;
for (;;) {
if (p1 >= p2) {
p1 = b;
c = fread(p1, 1, BUFSIZ, fptr);
if (c == 0) {
if (feof(fptr))
break;
/*
* skip the file and generate error
* message when failed to read the
* file.
*/
if (ferror(fptr)) {
(void) fprintf(stderr, gettext(
"wc: cannot read %s: %s\n"),
argv[i], strerror(errno));
status = 2;
errflag = 1;
break;
}
}
charct += c;
p2 = p1+c;
}
c = *p1++;
real_charct++;
if (ISASCII(c)) {
if (isspace(c)) {
if (c == '\n')
linect++;
token = 0;
continue;
}
if (!token) {
wordct++;
token++;
}
} else {
p1--;
if ((len = (p2 - p1)) <
(unsigned int)MB_CUR_MAX) {
for (n = 0; n < len; n++)
b[n] = *p1++;
p1 = b;
p2 = p1 + n;
c = fread(p2, 1, BUFSIZ - n, fptr);
if ((int)c > 0) {
charct += c;
p2 += c;
}
}
if ((len = (p2 - p1)) >
(unsigned int)MB_CUR_MAX)
len = (unsigned int)MB_CUR_MAX;
if ((len = mbtowc(&wc, (char *)p1, len)) > 0) {
p1 += len;
if (iswspace(wc)) {
token = 0;
continue;
}
} else
p1++;
if (!token) {
wordct++;
token++;
}
}
}
/* print lines, words, chars */
printwc:
(void) fclose(fptr);
if (errflag)
continue;
wcp(charct, wordct, linect, real_charct);
if (argc > 0) {
(void) printf(" %s\n", argv[i]);
}
else
(void) printf("\n");
tlinect += linect;
twordct += wordct;
tcharct += charct;
real_tcharct += real_charct;
} while (++i < argc);
if (argc > 1) {
wcp(tcharct, twordct, tlinect, real_tcharct);
(void) printf(" total\n");
}
return (status);
}
static void
wcp(
unsigned long long charct,
unsigned long long wordct,
unsigned long long linect,
unsigned long long real_charct)
{
if (lflag)
(void) printf((linect < 10000000) ? " %7llu" :
" %llu", linect);
if (wflag)
(void) printf((wordct < 10000000) ? " %7llu" :
" %llu", wordct);
if (cflag)
(void) printf((charct < 10000000) ? " %7llu" :
" %llu", charct);
else if (mflag)
(void) printf((real_charct < 10000000) ? " %7llu" :
" %llu", real_charct);
}
static void
usage()
{
(void) fprintf(stderr, gettext(
"usage: wc [-c | -m | -C] [-lw] [file ...]\n"));
exit(2);
}
|