wc.c revision 49461
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#ifndef lint
41#if 0
42static const char sccsid[] = "@(#)wc.c	8.1 (Berkeley) 6/6/93";
43#else
44static const char rcsid[] =
45	"$Id: wc.c,v 1.8 1997/08/25 06:44:59 charnier Exp $";
46#endif
47#endif /* not lint */
48
49#include <sys/param.h>
50#include <sys/stat.h>
51#include <sys/types.h>
52
53#include <ctype.h>
54#include <err.h>
55#include <fcntl.h>
56#include <locale.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <unistd.h>
61
62u_quad_t tlinect, twordct, tcharct;
63int doline, doword, dochar;
64
65int cnt __P((char *));
66void usage __P((void));
67
68int
69main(argc, argv)
70	int argc;
71	char *argv[];
72{
73	int ch;
74	int errors, total;
75
76	(void) setlocale(LC_CTYPE, "");
77
78	while ((ch = getopt(argc, argv, "lwc")) != -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			break;
89		case '?':
90		default:
91			usage();
92		}
93	argv += optind;
94	argc -= optind;
95
96	/* Wc's flags are on by default. */
97	if (doline + doword + dochar == 0)
98		doline = doword = dochar = 1;
99
100	errors = 0;
101	total = 0;
102	if (!*argv) {
103		if (cnt((char *)NULL) != 0)
104			++errors;
105		else
106			(void)printf("\n");
107	}
108	else do {
109		if (cnt(*argv) != 0)
110			++errors;
111		else
112			(void)printf(" %s\n", *argv);
113		++total;
114	} while(*++argv);
115
116	if (total > 1) {
117		if (doline)
118			(void)printf(" %7qu", (u_quad_t)tlinect);
119		if (doword)
120			(void)printf(" %7qu", (u_quad_t)twordct);
121		if (dochar)
122			(void)printf(" %7qu", (u_quad_t)tcharct);
123		(void)printf(" total\n");
124	}
125	exit(errors == 0 ? 0 : 1);
126}
127
128int
129cnt(file)
130	char *file;
131{
132	u_char *p, ch;
133	short gotsp;
134	int len;
135	u_quad_t linect, wordct, charct;
136	struct stat sb;
137	int fd;
138	u_char buf[MAXBSIZE];
139
140	linect = wordct = charct = 0;
141	if (file == NULL) {
142		file = "stdin";
143		fd = STDIN_FILENO;
144	} else {
145		if ((fd = open(file, O_RDONLY, 0)) < 0) {
146			warn("%s: open", file);
147			return (1);
148		}
149		if (doword)
150			goto word;
151		/*
152		 * Line counting is split out because it's a lot faster to get
153		 * lines than to get words, since the word count requires some
154		 * logic.
155		 */
156		if (doline) {
157			while ((len = read(fd, buf, MAXBSIZE))) {
158				if (len == -1) {
159					warn("%s: read", file);
160					(void)close(fd);
161					return (1);
162				}
163				charct += len;
164				for (p = buf; len--; ++p)
165					if (*p == '\n')
166						++linect;
167			}
168			tlinect += linect;
169			(void)printf(" %7qu", (u_quad_t)linect);
170			if (dochar) {
171				tcharct += charct;
172				(void)printf(" %7qu", (u_quad_t)charct);
173			}
174			(void)close(fd);
175			return (0);
176		}
177		/*
178		 * If all we need is the number of characters and it's a
179		 * regular or linked file, just stat the puppy.
180		 */
181		if (dochar) {
182			if (fstat(fd, &sb)) {
183				warn("%s: fstat", file);
184				(void)close(fd);
185				return (1);
186			}
187			if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode)) {
188				(void)printf(" %7qu", sb.st_size);
189				tcharct += sb.st_size;
190				(void)close(fd);
191				return (0);
192			}
193		}
194	}
195
196	/* Do it the hard way... */
197word:	for (gotsp = 1; (len = read(fd, buf, MAXBSIZE));) {
198		if (len == -1) {
199			warn("%s: read", file);
200			(void)close(fd);
201			return (1);
202		}
203		/*
204		 * This loses in the presence of multi-byte characters.
205		 * To do it right would require a function to return a
206		 * character while knowing how many bytes it consumed.
207		 */
208		charct += len;
209		for (p = buf; len--;) {
210			ch = *p++;
211			if (ch == '\n')
212				++linect;
213			if (isspace(ch))
214				gotsp = 1;
215			else if (gotsp) {
216				gotsp = 0;
217				++wordct;
218			}
219		}
220	}
221	if (doline) {
222		tlinect += linect;
223		(void)printf(" %7qu", (u_quad_t)linect);
224	}
225	if (doword) {
226		twordct += wordct;
227		(void)printf(" %7qu", (u_quad_t)wordct);
228	}
229	if (dochar) {
230		tcharct += charct;
231		(void)printf(" %7qu", (u_quad_t)charct);
232	}
233	(void)close(fd);
234	return (0);
235}
236
237void
238usage()
239{
240	(void)fprintf(stderr, "usage: wc [-clw] [file ...]\n");
241	exit(1);
242}
243