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