xref: /freebsd/usr.bin/wc/wc.c (revision df7f5d4de4592a8948a25ce01e5bddfbb7ce39dc)
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 #ifndef lint
41 #if 0
42 static const char sccsid[] = "@(#)wc.c	8.1 (Berkeley) 6/6/93";
43 #else
44 static const char rcsid[] =
45 	"$Id$";
46 #endif
47 #endif /* not lint */
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 <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60 
61 u_long tlinect, twordct, tcharct;
62 int doline, doword, dochar;
63 
64 int cnt __P((char *));
65 void usage __P((void));
66 
67 int
68 main(argc, argv)
69 	int argc;
70 	char *argv[];
71 {
72 	register int ch;
73 	int errors, total;
74 
75 	(void) setlocale(LC_CTYPE, "");
76 
77 	while ((ch = getopt(argc, argv, "lwc")) != EOF)
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(" %7ld", tlinect);
118 		if (doword)
119 			(void)printf(" %7ld", twordct);
120 		if (dochar)
121 			(void)printf(" %7ld", tcharct);
122 		(void)printf(" total\n");
123 	}
124 	exit(errors == 0 ? 0 : 1);
125 }
126 
127 int
128 cnt(file)
129 	char *file;
130 {
131 	register u_char *p, ch;
132 	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... */
196 word:	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 
236 void
237 usage()
238 {
239 	(void)fprintf(stderr, "usage: wc [-clw] [files]\n");
240 	exit(1);
241 }
242