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