wc.c revision 128049
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 128049 2004-04-09 11:17:29Z 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#include <wchar.h>
63#include <wctype.h>
64
65uintmax_t tlinect, twordct, tcharct;
66int doline, doword, dochar, domulti;
67
68static int	cnt(const char *);
69static void	usage(void);
70
71int
72main(argc, argv)
73	int argc;
74	char *argv[];
75{
76	int ch, errors, total;
77
78	(void) setlocale(LC_CTYPE, "");
79
80	while ((ch = getopt(argc, argv, "clmw")) != -1)
81		switch((char)ch) {
82		case 'l':
83			doline = 1;
84			break;
85		case 'w':
86			doword = 1;
87			break;
88		case 'c':
89			dochar = 1;
90			domulti = 0;
91			break;
92		case 'm':
93			domulti = 1;
94			dochar = 0;
95			break;
96		case '?':
97		default:
98			usage();
99		}
100	argv += optind;
101	argc -= optind;
102
103	/* Wc's flags are on by default. */
104	if (doline + doword + dochar + domulti == 0)
105		doline = doword = dochar = 1;
106
107	errors = 0;
108	total = 0;
109	if (!*argv) {
110		if (cnt((char *)NULL) != 0)
111			++errors;
112		else
113			(void)printf("\n");
114	}
115	else do {
116		if (cnt(*argv) != 0)
117			++errors;
118		else
119			(void)printf(" %s\n", *argv);
120		++total;
121	} while(*++argv);
122
123	if (total > 1) {
124		if (doline)
125			(void)printf(" %7ju", tlinect);
126		if (doword)
127			(void)printf(" %7ju", twordct);
128		if (dochar || domulti)
129			(void)printf(" %7ju", tcharct);
130		(void)printf(" total\n");
131	}
132	exit(errors == 0 ? 0 : 1);
133}
134
135static int
136cnt(file)
137	const char *file;
138{
139	struct stat sb;
140	uintmax_t linect, wordct, charct;
141	int fd, len, warned;
142	size_t clen;
143	short gotsp;
144	u_char *p;
145	u_char buf[MAXBSIZE];
146	wchar_t wch;
147	mbstate_t mbs;
148
149	linect = wordct = charct = 0;
150	if (file == NULL) {
151		file = "stdin";
152		fd = STDIN_FILENO;
153	} else {
154		if ((fd = open(file, O_RDONLY, 0)) < 0) {
155			warn("%s: open", file);
156			return (1);
157		}
158		if (doword || (domulti && MB_CUR_MAX != 1))
159			goto word;
160		/*
161		 * Line counting is split out because it's a lot faster to get
162		 * lines than to get words, since the word count requires some
163		 * logic.
164		 */
165		if (doline) {
166			while ((len = read(fd, buf, MAXBSIZE))) {
167				if (len == -1) {
168					warn("%s: read", file);
169					(void)close(fd);
170					return (1);
171				}
172				charct += len;
173				for (p = buf; len--; ++p)
174					if (*p == '\n')
175						++linect;
176			}
177			tlinect += linect;
178			(void)printf(" %7ju", linect);
179			if (dochar) {
180				tcharct += charct;
181				(void)printf(" %7ju", charct);
182			}
183			(void)close(fd);
184			return (0);
185		}
186		/*
187		 * If all we need is the number of characters and it's a
188		 * regular file, just stat the puppy.
189		 */
190		if (dochar || domulti) {
191			if (fstat(fd, &sb)) {
192				warn("%s: fstat", file);
193				(void)close(fd);
194				return (1);
195			}
196			if (S_ISREG(sb.st_mode)) {
197				(void)printf(" %7lld", (long long)sb.st_size);
198				tcharct += sb.st_size;
199				(void)close(fd);
200				return (0);
201			}
202		}
203	}
204
205	/* Do it the hard way... */
206word:	gotsp = 1;
207	warned = 0;
208	memset(&mbs, 0, sizeof(mbs));
209	while ((len = read(fd, buf, MAXBSIZE)) != 0) {
210		if (len == -1) {
211			warn("%s: read", file);
212			(void)close(fd);
213			return (1);
214		}
215		p = buf;
216		while (len > 0) {
217			if (!domulti || MB_CUR_MAX == 1) {
218				clen = 1;
219				wch = (unsigned char)*p;
220			} else if ((clen = mbrtowc(&wch, p, len, &mbs)) ==
221			    (size_t)-1) {
222				if (!warned) {
223					errno = EILSEQ;
224					warn("%s", file);
225					warned = 1;
226				}
227				memset(&mbs, 0, sizeof(mbs));
228				clen = 1;
229				wch = (unsigned char)*p;
230			} else if (clen == (size_t)-2)
231				break;
232			else if (clen == 0)
233				clen = 1;
234			charct++;
235			len -= clen;
236			p += clen;
237			if (wch == L'\n')
238				++linect;
239			if (iswspace(wch))
240				gotsp = 1;
241			else if (gotsp) {
242				gotsp = 0;
243				++wordct;
244			}
245		}
246	}
247	if (domulti && MB_CUR_MAX > 1)
248		if (mbrtowc(NULL, NULL, 0, &mbs) == (size_t)-1 && !warned)
249			warn("%s", file);
250	if (doline) {
251		tlinect += linect;
252		(void)printf(" %7ju", linect);
253	}
254	if (doword) {
255		twordct += wordct;
256		(void)printf(" %7ju", wordct);
257	}
258	if (dochar || domulti) {
259		tcharct += charct;
260		(void)printf(" %7ju", charct);
261	}
262	(void)close(fd);
263	return (0);
264}
265
266static void
267usage()
268{
269	(void)fprintf(stderr, "usage: wc [-clmw] [file ...]\n");
270	exit(1);
271}
272