wc.c revision 98165
1/*
2 * Copyright (c) 1980, 1987, 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1980, 1987, 1991, 1993\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#if 0
41#ifndef lint
42static char sccsid[] = "@(#)wc.c	8.1 (Berkeley) 6/6/93";
43#endif /* not lint */
44#endif
45
46#include <sys/cdefs.h>
47__FBSDID("$FreeBSD: head/usr.bin/wc/wc.c 98165 2002-06-13 12:48:50Z tjr $");
48
49#include <sys/param.h>
50#include <sys/stat.h>
51
52#include <ctype.h>
53#include <err.h>
54#include <errno.h>
55#include <fcntl.h>
56#include <locale.h>
57#include <stdint.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
61#include <unistd.h>
62
63uintmax_t tlinect, twordct, tcharct;
64int doline, doword, dochar, domulti;
65
66static int	cnt(const char *);
67static void	usage(void);
68
69int
70main(argc, argv)
71	int argc;
72	char *argv[];
73{
74	int ch, errors, total;
75
76	(void) setlocale(LC_CTYPE, "");
77
78	while ((ch = getopt(argc, argv, "clmw")) != -1)
79		switch((char)ch) {
80		case 'l':
81			doline = 1;
82			break;
83		case 'w':
84			doword = 1;
85			break;
86		case 'c':
87			dochar = 1;
88			domulti = 0;
89			break;
90		case 'm':
91			domulti = 1;
92			dochar = 0;
93			break;
94		case '?':
95		default:
96			usage();
97		}
98	argv += optind;
99	argc -= optind;
100
101	/* Wc's flags are on by default. */
102	if (doline + doword + dochar + domulti == 0)
103		doline = doword = dochar = 1;
104
105	errors = 0;
106	total = 0;
107	if (!*argv) {
108		if (cnt((char *)NULL) != 0)
109			++errors;
110		else
111			(void)printf("\n");
112	}
113	else do {
114		if (cnt(*argv) != 0)
115			++errors;
116		else
117			(void)printf(" %s\n", *argv);
118		++total;
119	} while(*++argv);
120
121	if (total > 1) {
122		if (doline)
123			(void)printf(" %7ju", tlinect);
124		if (doword)
125			(void)printf(" %7ju", twordct);
126		if (dochar || domulti)
127			(void)printf(" %7ju", tcharct);
128		(void)printf(" total\n");
129	}
130	exit(errors == 0 ? 0 : 1);
131}
132
133static int
134cnt(file)
135	const char *file;
136{
137	struct stat sb;
138	uintmax_t linect, wordct, charct;
139	ssize_t nread;
140	int clen, fd, len, warned;
141	short gotsp;
142	u_char *p;
143	u_char buf[MAXBSIZE], ch;
144	wchar_t wch;
145
146	linect = wordct = charct = 0;
147	if (file == NULL) {
148		file = "stdin";
149		fd = STDIN_FILENO;
150	} else {
151		if ((fd = open(file, O_RDONLY, 0)) < 0) {
152			warn("%s: open", file);
153			return (1);
154		}
155		if (doword || (domulti && MB_CUR_MAX != 1))
156			goto word;
157		/*
158		 * Line counting is split out because it's a lot faster to get
159		 * lines than to get words, since the word count requires some
160		 * logic.
161		 */
162		if (doline) {
163			while ((len = read(fd, buf, MAXBSIZE))) {
164				if (len == -1) {
165					warn("%s: read", file);
166					(void)close(fd);
167					return (1);
168				}
169				charct += len;
170				for (p = buf; len--; ++p)
171					if (*p == '\n')
172						++linect;
173			}
174			tlinect += linect;
175			(void)printf(" %7ju", linect);
176			if (dochar) {
177				tcharct += charct;
178				(void)printf(" %7ju", charct);
179			}
180			(void)close(fd);
181			return (0);
182		}
183		/*
184		 * If all we need is the number of characters and it's a
185		 * regular or linked file, just stat the puppy.
186		 */
187		if (dochar || domulti) {
188			if (fstat(fd, &sb)) {
189				warn("%s: fstat", file);
190				(void)close(fd);
191				return (1);
192			}
193			if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode)) {
194				(void)printf(" %7lld", (long long)sb.st_size);
195				tcharct += sb.st_size;
196				(void)close(fd);
197				return (0);
198			}
199		}
200	}
201
202	/* Do it the hard way... */
203word:	gotsp = 1;
204	len = 0;
205	warned = 0;
206	while ((nread = read(fd, buf + len, MAXBSIZE - len)) != 0) {
207		if (nread == -1) {
208			warn("%s: read", file);
209			(void)close(fd);
210			return (1);
211		}
212		len += nread;
213		p = buf;
214		while (len > 0) {
215			if (!domulti || MB_CUR_MAX == 1) {
216				clen = 1;
217				wch = (unsigned char)*p;
218			} else if ((clen = mbtowc(&wch, p, len)) <= 0) {
219				if (len > MB_CUR_MAX) {
220					clen = 1;
221					wch = (unsigned char)*p;
222					if (!warned) {
223						errno = EILSEQ;
224						warn("%s", file);
225						warned = 1;
226					}
227				} else {
228					memmove(buf, p, len);
229					break;
230				}
231			}
232			charct++;
233			len -= clen;
234			p += clen;
235			if (wch == L'\n')
236				++linect;
237			/* XXX Non-portable; should use iswspace() */
238			if (isspace(ch))
239				gotsp = 1;
240			else if (gotsp) {
241				gotsp = 0;
242				++wordct;
243			}
244		}
245	}
246	if (doline) {
247		tlinect += linect;
248		(void)printf(" %7ju", linect);
249	}
250	if (doword) {
251		twordct += wordct;
252		(void)printf(" %7ju", wordct);
253	}
254	if (dochar || domulti) {
255		tcharct += charct;
256		(void)printf(" %7ju", charct);
257	}
258	(void)close(fd);
259	return (0);
260}
261
262static void
263usage()
264{
265	(void)fprintf(stderr, "usage: wc [-clmw] [file ...]\n");
266	exit(1);
267}
268