Annotation of src/usr.bin/wc/wc.c, Revision 1.14
1.14 ! deraadt 1: /* $OpenBSD: wc.c,v 1.13 2009/10/27 23:59:49 deraadt Exp $ */
1.2 deraadt 2:
1.1 deraadt 3: /*
1.3 millert 4: * Copyright (c) 1980, 1987, 1991, 1993
5: * The Regents of the University of California. All rights reserved.
1.1 deraadt 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.
1.9 millert 15: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 16: * may be used to endorse or promote products derived from this software
17: * without specific prior written permission.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29: * SUCH DAMAGE.
30: */
31:
32: #include <stdio.h>
33: #include <stdlib.h>
34: #include <string.h>
35: #include <locale.h>
36: #include <ctype.h>
1.3 millert 37: #include <err.h>
1.1 deraadt 38: #include <sys/param.h>
39: #include <sys/stat.h>
40: #include <sys/file.h>
41: #include <unistd.h>
1.11 espie 42: #include <util.h>
1.1 deraadt 43:
1.3 millert 44: int64_t tlinect, twordct, tcharct;
1.11 espie 45: int doline, doword, dochar, humanchar;
1.3 millert 46: int rval;
47: extern char *__progname;
48:
1.7 millert 49: void print_counts(int64_t, int64_t, int64_t, char *);
1.14 ! deraadt 50: void format_and_print(long long);
1.7 millert 51: void cnt(char *);
1.1 deraadt 52:
53: int
1.8 deraadt 54: main(int argc, char *argv[])
1.1 deraadt 55: {
1.6 mpech 56: int ch;
1.1 deraadt 57:
58: setlocale(LC_ALL, "");
59:
1.11 espie 60: while ((ch = getopt(argc, argv, "lwchm")) != -1)
1.1 deraadt 61: switch((char)ch) {
62: case 'l':
63: doline = 1;
64: break;
65: case 'w':
66: doword = 1;
67: break;
68: case 'c':
69: case 'm':
70: dochar = 1;
71: break;
1.11 espie 72: case 'h':
73: humanchar = 1;
74: break;
1.1 deraadt 75: case '?':
76: default:
1.3 millert 77: (void)fprintf(stderr,
1.11 espie 78: "usage: %s [-c | -m] [-hlw] [file ...]\n",
1.3 millert 79: __progname);
1.1 deraadt 80: exit(1);
81: }
82: argv += optind;
83: argc -= optind;
84:
85: /*
86: * wc is unusual in that its flags are on by default, so,
87: * if you don't get any arguments, you have to turn them
88: * all on.
89: */
1.3 millert 90: if (!doline && !doword && !dochar)
1.1 deraadt 91: doline = doword = dochar = 1;
92:
93: if (!*argv) {
94: cnt((char *)NULL);
95: } else {
96: int dototal = (argc > 1);
97:
98: do {
99: cnt(*argv);
100: } while(*++argv);
101:
1.3 millert 102: if (dototal)
1.10 deraadt 103: print_counts(tlinect, twordct, tcharct, "total");
1.1 deraadt 104: }
105:
106: exit(rval);
107: }
108:
1.3 millert 109: void
1.8 deraadt 110: cnt(char *file)
1.1 deraadt 111: {
1.6 mpech 112: u_char *C;
113: short gotsp;
114: int len;
115: int64_t linect, wordct, charct;
1.1 deraadt 116: struct stat sbuf;
117: int fd;
118: u_char buf[MAXBSIZE];
119:
120: linect = wordct = charct = 0;
121: if (file) {
122: if ((fd = open(file, O_RDONLY, 0)) < 0) {
1.3 millert 123: warn("%s", file);
1.1 deraadt 124: rval = 1;
125: return;
126: }
127: } else {
128: fd = STDIN_FILENO;
129: }
1.10 deraadt 130:
1.1 deraadt 131: if (!doword) {
132: /*
1.3 millert 133: * Line counting is split out because it's a lot
1.1 deraadt 134: * faster to get lines than to get words, since
135: * the word count requires some logic.
136: */
137: if (doline) {
1.3 millert 138: while ((len = read(fd, buf, MAXBSIZE)) > 0) {
1.1 deraadt 139: charct += len;
140: for (C = buf; len--; ++C)
141: if (*C == '\n')
142: ++linect;
143: }
144: if (len == -1) {
1.3 millert 145: warn("%s", file);
1.1 deraadt 146: rval = 1;
147: }
148: }
149: /*
1.3 millert 150: * If all we need is the number of characters and
1.1 deraadt 151: * it's a directory or a regular or linked file, just
152: * stat the puppy. We avoid testing for it not being
153: * a special device in case someone adds a new type
154: * of inode.
155: */
156: else if (dochar) {
1.3 millert 157: mode_t ifmt;
1.1 deraadt 158:
159: if (fstat(fd, &sbuf)) {
1.3 millert 160: warn("%s", file);
1.1 deraadt 161: rval = 1;
162: } else {
163: ifmt = sbuf.st_mode & S_IFMT;
164: if (ifmt == S_IFREG || ifmt == S_IFLNK
1.3 millert 165: || ifmt == S_IFDIR) {
1.1 deraadt 166: charct = sbuf.st_size;
167: } else {
1.3 millert 168: while ((len = read(fd, buf, MAXBSIZE)) > 0)
1.1 deraadt 169: charct += len;
170: if (len == -1) {
1.3 millert 171: warn("%s", file);
1.1 deraadt 172: rval = 1;
173: }
174: }
175: }
176: }
1.3 millert 177: } else {
178: /* Do it the hard way... */
1.1 deraadt 179: gotsp = 1;
180: while ((len = read(fd, buf, MAXBSIZE)) > 0) {
1.3 millert 181: /*
182: * This loses in the presence of multi-byte characters.
183: * To do it right would require a function to return a
184: * character while knowing how many bytes it consumed.
185: */
1.1 deraadt 186: charct += len;
187: for (C = buf; len--; ++C) {
188: if (isspace (*C)) {
189: gotsp = 1;
1.3 millert 190: if (*C == '\n')
1.1 deraadt 191: ++linect;
192: } else {
193: /*
194: * This line implements the POSIX
195: * spec, i.e. a word is a "maximal
196: * string of characters delimited by
197: * whitespace." Notice nothing was
198: * said about a character being
199: * printing or non-printing.
200: */
201: if (gotsp) {
202: gotsp = 0;
203: ++wordct;
204: }
205: }
206: }
207: }
208: if (len == -1) {
1.3 millert 209: warn("%s", file);
1.1 deraadt 210: rval = 1;
211: }
212: }
213:
1.12 otto 214: print_counts(linect, wordct, charct, file);
1.1 deraadt 215:
1.3 millert 216: /*
217: * Don't bother checking doline, doword, or dochar -- speeds
1.10 deraadt 218: * up the common case
1.3 millert 219: */
1.1 deraadt 220: tlinect += linect;
221: twordct += wordct;
222: tcharct += charct;
223:
1.3 millert 224: if (close(fd) != 0) {
225: warn("%s", file);
1.1 deraadt 226: rval = 1;
227: }
228: }
229:
1.11 espie 230: void
231: format_and_print(long long v)
232: {
233: if (humanchar) {
234: char result[FMT_SCALED_STRSIZE];
235:
236: (void)fmt_scaled(v, result);
237: (void)printf("%7s", result);
238: } else {
239: (void)printf(" %7lld", v);
240: }
241: }
242:
1.1 deraadt 243: void
1.8 deraadt 244: print_counts(int64_t lines, int64_t words, int64_t chars, char *name)
1.1 deraadt 245: {
246: if (doline)
1.11 espie 247: format_and_print((long long)lines);
1.1 deraadt 248: if (doword)
1.11 espie 249: format_and_print((long long)words);
1.1 deraadt 250: if (dochar)
1.11 espie 251: format_and_print((long long)chars);
1.1 deraadt 252:
1.12 otto 253: if (name)
254: (void)printf(" %s\n", name);
255: else
256: (void)printf("\n");
1.1 deraadt 257: }