wc.c revision 23012
1178476Sjb/*
2178476Sjb * Copyright (c) 1980, 1987, 1991, 1993
3178476Sjb *	The Regents of the University of California.  All rights reserved.
4178476Sjb *
5178476Sjb * Redistribution and use in source and binary forms, with or without
6178476Sjb * modification, are permitted provided that the following conditions
7178476Sjb * are met:
8178476Sjb * 1. Redistributions of source code must retain the above copyright
9178476Sjb *    notice, this list of conditions and the following disclaimer.
10178476Sjb * 2. Redistributions in binary form must reproduce the above copyright
11178476Sjb *    notice, this list of conditions and the following disclaimer in the
12178476Sjb *    documentation and/or other materials provided with the distribution.
13178476Sjb * 3. All advertising materials mentioning features or use of this software
14178476Sjb *    must display the following acknowledgement:
15178476Sjb *	This product includes software developed by the University of
16178476Sjb *	California, Berkeley and its contributors.
17178476Sjb * 4. Neither the name of the University nor the names of its contributors
18178476Sjb *    may be used to endorse or promote products derived from this software
19178476Sjb *    without specific prior written permission.
20178476Sjb *
21178476Sjb * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22178476Sjb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23178476Sjb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24178476Sjb * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25178476Sjb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26178476Sjb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27178476Sjb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28178476Sjb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29178476Sjb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30178476Sjb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31178476Sjb * SUCH DAMAGE.
32178476Sjb */
33178476Sjb
34178476Sjb#ifndef lint
35178476Sjbstatic const char copyright[] =
36178476Sjb"@(#) Copyright (c) 1980, 1987, 1991, 1993\n\
37178476Sjb	The Regents of the University of California.  All rights reserved.\n";
38178476Sjb#endif /* not lint */
39178476Sjb
40178476Sjb#ifndef lint
41178476Sjb#if 0
42178476Sjbstatic const char sccsid[] = "@(#)wc.c	8.1 (Berkeley) 6/6/93";
43178476Sjb#else
44178476Sjbstatic const char rcsid[] =
45178476Sjb	"$Id$";
46178476Sjb#endif
47178476Sjb#endif /* not lint */
48178476Sjb
49178476Sjb#include <sys/param.h>
50178476Sjb#include <sys/stat.h>
51178476Sjb
52178476Sjb#include <ctype.h>
53178476Sjb#include <err.h>
54178476Sjb#include <fcntl.h>
55178476Sjb#include <locale.h>
56178476Sjb#include <stdio.h>
57178476Sjb#include <stdlib.h>
58178476Sjb#include <string.h>
59178476Sjb#include <unistd.h>
60178476Sjb
61178476Sjbu_long tlinect, twordct, tcharct;
62178476Sjbint doline, doword, dochar;
63178476Sjb
64178476Sjbint cnt __P((char *));
65178476Sjbvoid usage __P((void));
66178476Sjb
67178476Sjbint
68178476Sjbmain(argc, argv)
69178476Sjb	int argc;
70178476Sjb	char *argv[];
71178476Sjb{
72178476Sjb	register int ch;
73178476Sjb	int errors, total;
74178476Sjb
75178476Sjb	(void) setlocale(LC_CTYPE, "");
76178476Sjb
77178476Sjb	while ((ch = getopt(argc, argv, "lwc")) != EOF)
78178476Sjb		switch((char)ch) {
79178476Sjb		case 'l':
80178476Sjb			doline = 1;
81178476Sjb			break;
82178476Sjb		case 'w':
83178476Sjb			doword = 1;
84178476Sjb			break;
85178476Sjb		case 'c':
86178476Sjb			dochar = 1;
87178476Sjb			break;
88178476Sjb		case '?':
89178476Sjb		default:
90178476Sjb			usage();
91178476Sjb		}
92178476Sjb	argv += optind;
93178476Sjb	argc -= optind;
94178476Sjb
95178476Sjb	/* Wc's flags are on by default. */
96178476Sjb	if (doline + doword + dochar == 0)
97178476Sjb		doline = doword = dochar = 1;
98178476Sjb
99178476Sjb	errors = 0;
100178476Sjb	total = 0;
101178476Sjb	if (!*argv) {
102178476Sjb		if (cnt((char *)NULL) != 0)
103178476Sjb			++errors;
104178476Sjb		else
105178476Sjb			(void)printf("\n");
106178476Sjb	}
107178476Sjb	else do {
108178476Sjb		if (cnt(*argv) != 0)
109178476Sjb			++errors;
110178476Sjb		else
111178476Sjb			(void)printf(" %s\n", *argv);
112178476Sjb		++total;
113178476Sjb	} while(*++argv);
114178476Sjb
115178476Sjb	if (total > 1) {
116178476Sjb		if (doline)
117178476Sjb			(void)printf(" %7ld", tlinect);
118178476Sjb		if (doword)
119178476Sjb			(void)printf(" %7ld", twordct);
120178476Sjb		if (dochar)
121178476Sjb			(void)printf(" %7ld", tcharct);
122178476Sjb		(void)printf(" total\n");
123178476Sjb	}
124178476Sjb	exit(errors == 0 ? 0 : 1);
125178476Sjb}
126178476Sjb
127178476Sjbint
128178476Sjbcnt(file)
129178476Sjb	char *file;
130178476Sjb{
131178476Sjb	register u_char *p, ch;
132178476Sjb	register short gotsp;
133	register int len;
134	register u_long linect, wordct, charct;
135	struct stat sb;
136	int fd;
137	u_char buf[MAXBSIZE];
138
139	linect = wordct = charct = 0;
140	if (file == NULL) {
141		file = "stdin";
142		fd = STDIN_FILENO;
143	} else {
144		if ((fd = open(file, O_RDONLY, 0)) < 0) {
145			warn("%s: open", file);
146			return (1);
147		}
148		if (doword)
149			goto word;
150		/*
151		 * Line counting is split out because it's a lot faster to get
152		 * lines than to get words, since the word count requires some
153		 * logic.
154		 */
155		if (doline) {
156			while (len = read(fd, buf, MAXBSIZE)) {
157				if (len == -1) {
158					warn("%s: read", file);
159					(void)close(fd);
160					return (1);
161				}
162				charct += len;
163				for (p = buf; len--; ++p)
164					if (*p == '\n')
165						++linect;
166			}
167			tlinect += linect;
168			(void)printf(" %7lu", linect);
169			if (dochar) {
170				tcharct += charct;
171				(void)printf(" %7lu", charct);
172			}
173			(void)close(fd);
174			return (0);
175		}
176		/*
177		 * If all we need is the number of characters and it's a
178		 * regular or linked file, just stat the puppy.
179		 */
180		if (dochar) {
181			if (fstat(fd, &sb)) {
182				warn("%s: fstat", file);
183				(void)close(fd);
184				return (1);
185			}
186			if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode)) {
187				(void)printf(" %7qu", sb.st_size);
188				tcharct += sb.st_size;
189				(void)close(fd);
190				return (0);
191			}
192		}
193	}
194
195	/* Do it the hard way... */
196word:	for (gotsp = 1; len = read(fd, buf, MAXBSIZE);) {
197		if (len == -1) {
198			warn("%s: read", file);
199			(void)close(fd);
200			return (1);
201		}
202		/*
203		 * This loses in the presence of multi-byte characters.
204		 * To do it right would require a function to return a
205		 * character while knowing how many bytes it consumed.
206		 */
207		charct += len;
208		for (p = buf; len--;) {
209			ch = *p++;
210			if (ch == '\n')
211				++linect;
212			if (isspace(ch))
213				gotsp = 1;
214			else if (gotsp) {
215				gotsp = 0;
216				++wordct;
217			}
218		}
219	}
220	if (doline) {
221		tlinect += linect;
222		(void)printf(" %7lu", linect);
223	}
224	if (doword) {
225		twordct += wordct;
226		(void)printf(" %7lu", wordct);
227	}
228	if (dochar) {
229		tcharct += charct;
230		(void)printf(" %7lu", charct);
231	}
232	(void)close(fd);
233	return (0);
234}
235
236void
237usage()
238{
239	(void)fprintf(stderr, "usage: wc [-clw] [files]\n");
240	exit(1);
241}
242