wc.c revision 1.21 1 1.21 yamt /* $NetBSD: wc.c,v 1.21 2001/10/19 06:09:56 yamt Exp $ */
2 1.10 tls
3 1.1 cgd /*
4 1.11 mrg * Copyright (c) 1980, 1987, 1991, 1993
5 1.11 mrg * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.13 mrg #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.13 mrg __COPYRIGHT("@(#) Copyright (c) 1980, 1987, 1991, 1993\n\
39 1.13 mrg The Regents of the University of California. All rights reserved.\n");
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #ifndef lint
43 1.11 mrg #if 0
44 1.11 mrg static char sccsid[] = "@(#)wc.c 8.2 (Berkeley) 5/2/95";
45 1.11 mrg #else
46 1.21 yamt __RCSID("$NetBSD: wc.c,v 1.21 2001/10/19 06:09:56 yamt Exp $");
47 1.11 mrg #endif
48 1.1 cgd #endif /* not lint */
49 1.1 cgd
50 1.1 cgd /* wc line, word and char count */
51 1.1 cgd
52 1.11 mrg #include <sys/param.h>
53 1.11 mrg #include <sys/stat.h>
54 1.15 explorer #include <sys/types.h>
55 1.11 mrg
56 1.11 mrg #include <fcntl.h>
57 1.11 mrg #include <unistd.h>
58 1.11 mrg #include <errno.h>
59 1.1 cgd #include <stdio.h>
60 1.11 mrg
61 1.2 jtc #include <stdlib.h>
62 1.2 jtc #include <string.h>
63 1.7 jtc #include <locale.h>
64 1.2 jtc #include <ctype.h>
65 1.2 jtc #include <errno.h>
66 1.7 jtc #include <sys/param.h>
67 1.7 jtc #include <sys/stat.h>
68 1.7 jtc #include <sys/file.h>
69 1.5 jtc #include <unistd.h>
70 1.7 jtc #include <err.h>
71 1.21 yamt #include <wchar.h>
72 1.21 yamt #include <wctype.h>
73 1.1 cgd
74 1.17 christos #ifdef NO_QUAD
75 1.17 christos typedef u_long wc_count_t;
76 1.18 mycroft # define WCFMT " %7lu"
77 1.17 christos # define WCCAST unsigned long
78 1.17 christos #else
79 1.17 christos typedef u_quad_t wc_count_t;
80 1.18 mycroft # define WCFMT " %7llu"
81 1.17 christos # define WCCAST unsigned long long
82 1.17 christos #endif
83 1.17 christos
84 1.17 christos static wc_count_t tlinect, twordct, tcharct;
85 1.21 yamt static int doline, doword, dobyte, dochar;
86 1.14 wsanchez static int rval = 0;
87 1.1 cgd
88 1.11 mrg static void cnt __P((char *));
89 1.17 christos static void print_counts __P((wc_count_t, wc_count_t, wc_count_t, char *));
90 1.11 mrg static void usage __P((void));
91 1.21 yamt static size_t do_mb __P((wchar_t *, const char *, size_t, mbstate_t *,
92 1.21 yamt size_t *, const char *));
93 1.13 mrg int main __P((int, char *[]));
94 1.11 mrg
95 1.5 jtc int
96 1.1 cgd main(argc, argv)
97 1.1 cgd int argc;
98 1.11 mrg char *argv[];
99 1.1 cgd {
100 1.13 mrg int ch;
101 1.1 cgd
102 1.7 jtc setlocale(LC_ALL, "");
103 1.7 jtc
104 1.6 jtc while ((ch = getopt(argc, argv, "lwcm")) != -1)
105 1.2 jtc switch((char)ch) {
106 1.2 jtc case 'l':
107 1.2 jtc doline = 1;
108 1.2 jtc break;
109 1.2 jtc case 'w':
110 1.2 jtc doword = 1;
111 1.2 jtc break;
112 1.4 jtc case 'm':
113 1.2 jtc dochar = 1;
114 1.21 yamt dobyte = 0;
115 1.21 yamt break;
116 1.21 yamt case 'c':
117 1.21 yamt dochar = 0;
118 1.21 yamt dobyte = 1;
119 1.2 jtc break;
120 1.2 jtc case '?':
121 1.2 jtc default:
122 1.11 mrg usage();
123 1.2 jtc }
124 1.2 jtc argv += optind;
125 1.2 jtc argc -= optind;
126 1.2 jtc
127 1.11 mrg /* Wc's flags are on by default. */
128 1.21 yamt if (doline + doword + dobyte + dochar == 0)
129 1.21 yamt doline = doword = dobyte = 1;
130 1.1 cgd
131 1.1 cgd if (!*argv) {
132 1.11 mrg cnt(NULL);
133 1.2 jtc } else {
134 1.2 jtc int dototal = (argc > 1);
135 1.2 jtc
136 1.2 jtc do {
137 1.2 jtc cnt(*argv);
138 1.2 jtc } while(*++argv);
139 1.2 jtc
140 1.19 mycroft if (dototal)
141 1.11 mrg print_counts(tlinect, twordct, tcharct, "total");
142 1.1 cgd }
143 1.2 jtc
144 1.6 jtc exit(rval);
145 1.1 cgd }
146 1.1 cgd
147 1.21 yamt static size_t
148 1.21 yamt do_mb(wc, p, mblen, st, cnt, file)
149 1.21 yamt wchar_t *wc;
150 1.21 yamt const char *p;
151 1.21 yamt size_t mblen;
152 1.21 yamt mbstate_t *st;
153 1.21 yamt size_t *cnt;
154 1.21 yamt const char *file;
155 1.21 yamt {
156 1.21 yamt size_t r;
157 1.21 yamt size_t c = 0;
158 1.21 yamt
159 1.21 yamt do {
160 1.21 yamt r = mbrtowc(wc, p, mblen, st);
161 1.21 yamt if (r == (size_t)-1) {
162 1.21 yamt warnx("%s: invalid byte sequence", file);
163 1.21 yamt rval = 1;
164 1.21 yamt
165 1.21 yamt /* XXX skip 1 byte */
166 1.21 yamt mblen --;
167 1.21 yamt p ++;
168 1.21 yamt memset(st, 0, sizeof(*st));
169 1.21 yamt }
170 1.21 yamt else if (r == (size_t)-2)
171 1.21 yamt break;
172 1.21 yamt else if (r == 0)
173 1.21 yamt r = 1;
174 1.21 yamt c ++;
175 1.21 yamt if (wc)
176 1.21 yamt wc ++;
177 1.21 yamt mblen -= r;
178 1.21 yamt p += r;
179 1.21 yamt } while (mblen > 0);
180 1.21 yamt
181 1.21 yamt *cnt = c;
182 1.21 yamt
183 1.21 yamt return r;
184 1.21 yamt }
185 1.21 yamt
186 1.5 jtc static void
187 1.1 cgd cnt(file)
188 1.1 cgd char *file;
189 1.1 cgd {
190 1.13 mrg u_char *C;
191 1.21 yamt wchar_t *WC;
192 1.13 mrg short gotsp;
193 1.21 yamt int len = 0;
194 1.17 christos wc_count_t linect, wordct, charct;
195 1.11 mrg struct stat sb;
196 1.1 cgd int fd;
197 1.1 cgd u_char buf[MAXBSIZE];
198 1.21 yamt wchar_t wbuf[MAXBSIZE];
199 1.21 yamt size_t r = 0;
200 1.21 yamt mbstate_t st;
201 1.1 cgd
202 1.1 cgd linect = wordct = charct = 0;
203 1.1 cgd if (file) {
204 1.1 cgd if ((fd = open(file, O_RDONLY, 0)) < 0) {
205 1.11 mrg warn("%s", file);
206 1.6 jtc rval = 1;
207 1.6 jtc return;
208 1.1 cgd }
209 1.7 jtc } else {
210 1.7 jtc fd = STDIN_FILENO;
211 1.7 jtc }
212 1.21 yamt
213 1.21 yamt if (dochar || doword)
214 1.21 yamt memset(&st, 0, sizeof(st));
215 1.7 jtc
216 1.7 jtc if (!doword) {
217 1.7 jtc /*
218 1.7 jtc * line counting is split out because it's a lot
219 1.7 jtc * faster to get lines than to get words, since
220 1.7 jtc * the word count requires some logic.
221 1.7 jtc */
222 1.21 yamt if (doline || dochar) {
223 1.11 mrg while ((len = read(fd, buf, MAXBSIZE)) > 0) {
224 1.21 yamt if (dochar) {
225 1.21 yamt size_t wlen;
226 1.21 yamt
227 1.21 yamt r = do_mb(0, (char *)buf, (size_t)len, &st, &wlen, file);
228 1.21 yamt charct += wlen;
229 1.21 yamt }
230 1.21 yamt else if (dobyte)
231 1.21 yamt charct += len;
232 1.21 yamt if (doline)
233 1.21 yamt for (C = buf; len--; ++C)
234 1.21 yamt if (*C == '\n')
235 1.21 yamt ++linect;
236 1.1 cgd }
237 1.7 jtc }
238 1.1 cgd
239 1.7 jtc /*
240 1.7 jtc * if all we need is the number of characters and
241 1.7 jtc * it's a directory or a regular or linked file, just
242 1.7 jtc * stat the puppy. We avoid testing for it not being
243 1.7 jtc * a special device in case someone adds a new type
244 1.7 jtc * of inode.
245 1.7 jtc */
246 1.21 yamt else if (dobyte) {
247 1.11 mrg if (fstat(fd, &sb)) {
248 1.11 mrg warn("%s", file);
249 1.7 jtc rval = 1;
250 1.7 jtc } else {
251 1.12 mycroft if (S_ISREG(sb.st_mode) ||
252 1.12 mycroft S_ISLNK(sb.st_mode) ||
253 1.12 mycroft S_ISDIR(sb.st_mode)) {
254 1.11 mrg charct = sb.st_size;
255 1.9 andrew } else {
256 1.11 mrg while ((len = read(fd, buf, MAXBSIZE)) > 0)
257 1.9 andrew charct += len;
258 1.1 cgd }
259 1.1 cgd }
260 1.1 cgd }
261 1.1 cgd }
262 1.1 cgd else
263 1.7 jtc {
264 1.7 jtc /* do it the hard way... */
265 1.8 jtc gotsp = 1;
266 1.8 jtc while ((len = read(fd, buf, MAXBSIZE)) > 0) {
267 1.21 yamt size_t wlen;
268 1.21 yamt
269 1.21 yamt r = do_mb(wbuf, (char *)buf, (size_t)len, &st, &wlen, file);
270 1.21 yamt if (dochar) {
271 1.21 yamt charct += wlen;
272 1.21 yamt }
273 1.21 yamt else if (dobyte)
274 1.21 yamt charct += len;
275 1.21 yamt for (WC = wbuf; wlen--; ++WC) {
276 1.21 yamt if (iswspace(*WC)) {
277 1.7 jtc gotsp = 1;
278 1.21 yamt if (*WC == L'\n') {
279 1.7 jtc ++linect;
280 1.7 jtc }
281 1.7 jtc } else {
282 1.7 jtc /*
283 1.7 jtc * This line implements the POSIX
284 1.7 jtc * spec, i.e. a word is a "maximal
285 1.7 jtc * string of characters delimited by
286 1.7 jtc * whitespace." Notice nothing was
287 1.7 jtc * said about a character being
288 1.7 jtc * printing or non-printing.
289 1.7 jtc */
290 1.7 jtc if (gotsp) {
291 1.7 jtc gotsp = 0;
292 1.7 jtc ++wordct;
293 1.7 jtc }
294 1.2 jtc }
295 1.1 cgd }
296 1.2 jtc }
297 1.21 yamt }
298 1.21 yamt
299 1.21 yamt if (len == -1) {
300 1.21 yamt warn ("%s", file);
301 1.21 yamt rval = 1;
302 1.21 yamt }
303 1.21 yamt if (dochar && r == (size_t)-2) {
304 1.21 yamt warnx ("%s: incomplete multibyte character", file);
305 1.21 yamt rval = 1;
306 1.1 cgd }
307 1.7 jtc
308 1.19 mycroft print_counts(linect, wordct, charct, file ? file : 0);
309 1.8 jtc
310 1.21 yamt /* don't bother checkint doline, doword, or dobyte --- speeds
311 1.8 jtc up the common case */
312 1.8 jtc tlinect += linect;
313 1.8 jtc twordct += wordct;
314 1.8 jtc tcharct += charct;
315 1.8 jtc
316 1.8 jtc if (close(fd)) {
317 1.8 jtc warn ("%s", file);
318 1.8 jtc rval = 1;
319 1.1 cgd }
320 1.8 jtc }
321 1.8 jtc
322 1.11 mrg static void
323 1.11 mrg print_counts(lines, words, chars, name)
324 1.17 christos wc_count_t lines;
325 1.17 christos wc_count_t words;
326 1.17 christos wc_count_t chars;
327 1.8 jtc char *name;
328 1.8 jtc {
329 1.8 jtc
330 1.8 jtc if (doline)
331 1.17 christos printf(WCFMT, (WCCAST)lines);
332 1.8 jtc if (doword)
333 1.17 christos printf(WCFMT, (WCCAST)words);
334 1.21 yamt if (dobyte || dochar)
335 1.17 christos printf(WCFMT, (WCCAST)chars);
336 1.7 jtc
337 1.19 mycroft if (name)
338 1.19 mycroft printf(" %s\n", name);
339 1.19 mycroft else
340 1.19 mycroft printf("\n");
341 1.11 mrg }
342 1.11 mrg
343 1.11 mrg static void
344 1.11 mrg usage()
345 1.11 mrg {
346 1.20 kleink (void)fprintf(stderr, "usage: wc [-clw] [file ...]\n");
347 1.11 mrg exit(1);
348 1.1 cgd }
349