1 /***********************************************************************
2 * *
3 * This software is part of the ast package *
4 * Copyright (c) 1992-2013 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Eclipse Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
8 * *
9 * A copy of the License is available at *
10 * http://www.eclipse.org/org/documents/epl-v10.html *
11 * (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12 * *
13 * Information and Software Systems Research *
14 * AT&T Research *
15 * Florham Park NJ *
16 * *
17 * Glenn Fowler <glenn.s.fowler@gmail.com> *
18 * David Korn <dgkorn@gmail.com> *
19 * *
20 ***********************************************************************/
21 #pragma prototyped
22 /*
23 * David Korn
24 * AT&T Bell Laboratories
25 *
26 * count the number of bytes, words, and lines in a file
27 */
28
29 static const char usage[] =
30 "[-?\n@(#)$Id: wc (AT&T Research) 2013-09-13 $\n]"
31 USAGE_LICENSE
32 "[+NAME?wc - print the number of bytes, words, and lines in files]"
33 "[+DESCRIPTION?\bwc\b reads one or more input files and, by default, "
34 "for each file writes a line containing the number of newlines, "
35 "\aword\as, and bytes contained in each file followed by the "
36 "file name to standard output in that order. A \aword\a is "
37 "defined to be a non-zero length string delimited by \bisspace\b(3) "
38 "characters.]"
39 "[+?If more than one file is specified, \bwc\b writes a total count "
40 "for all of the named files with \btotal\b written instead "
41 "of the file name.]"
42 "[+?By default, \bwc\b writes all three counts. The count options select "
43 "specific counts to be written. The options \b--bytes\b "
44 "and \b--multibyte-chars\b are mutually exclusive.]"
45 "[+?If no \afile\a is given, or if the \afile\a is \b-\b, \bwc\b "
46 "reads from standard input and no filename is written to standard "
47 "output. The start of the file is defined as the current offset.]"
48 "[l:lines?List the line counts.]"
49 "[w:words?List the word counts.]"
50 "[c:bytes|chars:chars?List the byte counts.]"
51 "[m|C:multibyte-chars?List the character counts.]"
52 "[X:invalid-bytes?List the counts of bytes not constituting a valid "
53 "character. Implies \b--multibyte-chars\b.]"
54 "[q:quiet?Suppress invalid multibyte character warnings.]"
55 "[L:longest-line|max-line-length?List the longest line length; the newline,"
56 "if any, is not counted in the length.]"
57 "[N!:utf8?For \bUTF-8\b locales \b--noutf8\b disables \bUTF-8\b "
58 "optimzations and relies on the native \bmbtowc\b(3).]"
59 "\n"
60 "\n[file ...]\n"
61 "\n"
62 "[+EXIT STATUS?]{"
63 "[+0?All files processed successfully.]"
64 "[+>0?One or more files failed to open or could not be read.]"
65 "}"
66 "[+SEE ALSO?\bcat\b(1), \bisspace\b(3)]"
67 ;
68
69
70 #include <cmd.h>
71 #include <wc.h>
72 #include <ls.h>
73
74 #define ERRORMAX 125
75
printout(register Wc_t * wp,register char * name,register int mode)76 static void printout(register Wc_t *wp, register char *name,register int mode)
77 {
78 if (mode&WC_LINES)
79 sfprintf(sfstdout," %7I*d",sizeof(wp->lines),wp->lines);
80 if (mode&WC_WORDS)
81 sfprintf(sfstdout," %7I*d",sizeof(wp->words),wp->words);
82 if (mode&WC_CHARS)
83 sfprintf(sfstdout," %7I*d",sizeof(wp->chars),wp->chars);
84 if (mode&WC_INVAL)
85 sfprintf(sfstdout," %7I*d",sizeof(wp->chars),wp->inval);
86 if (mode&WC_LONGEST)
87 sfprintf(sfstdout," %7I*d",sizeof(wp->chars),wp->longest);
88 if (name)
89 sfprintf(sfstdout," %s",name);
90 sfputc(sfstdout,'\n');
91 }
92
93 int
b_wc(int argc,register char ** argv,Shbltin_t * context)94 b_wc(int argc,register char **argv, Shbltin_t* context)
95 {
96 register char *cp;
97 register int mode=0, n;
98 register Wc_t *wp;
99 Sfio_t *fp;
100 Sfoff_t tlines=0, twords=0, tchars=0, tinval=0;
101 struct stat statb;
102
103 cmdinit(argc, argv, context, ERROR_CATALOG, 0);
104 for (;;)
105 {
106 switch (optget(argv, usage))
107 {
108 case 'c':
109 mode |= WC_CHARS;
110 continue;
111 case 'l':
112 mode |= WC_LINES;
113 continue;
114 case 'L':
115 mode |= WC_LONGEST;
116 continue;
117 case 'N':
118 if (!opt_info.num)
119 mode |= WC_NOUTF8;
120 continue;
121 case 'm':
122 case 'C':
123 mode |= WC_MBYTE;
124 continue;
125 case 'X':
126 mode |= WC_INVAL|WC_MBYTE;
127 continue;
128 case 'q':
129 mode |= WC_QUIET;
130 continue;
131 case 'w':
132 mode |= WC_WORDS;
133 continue;
134 case ':':
135 error(2, "%s", opt_info.arg);
136 break;
137 case '?':
138 error(ERROR_usage(2), "%s", opt_info.arg);
139 break;
140 }
141 break;
142 }
143 argv += opt_info.index;
144 if (error_info.errors)
145 error(ERROR_usage(2), "%s", optusage(NiL));
146 if (mode&WC_MBYTE)
147 {
148 if (mode&WC_CHARS)
149 error(2, "-c and -C are mutually exclusive");
150 if (!mbwide())
151 mode &= ~WC_MBYTE;
152 mode |= WC_CHARS;
153 }
154 if (!(mode&(WC_WORDS|WC_CHARS|WC_LINES|WC_MBYTE|WC_INVAL|WC_LONGEST)))
155 mode |= (WC_WORDS|WC_CHARS|WC_LINES);
156 if (!(wp = wc_init(mode)))
157 error(3,"internal error");
158 if (cp = *argv)
159 argv++;
160 n = 0;
161 do
162 {
163 if (!cp || streq(cp,"-"))
164 fp = sfstdin;
165 else if (!(fp = sfopen(NiL,cp,"r")))
166 {
167 error(ERROR_system(0),"%s: cannot open",cp);
168 continue;
169 }
170 if (cp)
171 n++;
172 if (!(mode&(WC_WORDS|WC_LINES|WC_MBYTE|WC_INVAL|WC_LONGEST)) &&
173 fstat(sffileno(fp),&statb)>=0 && S_ISREG(statb.st_mode))
174 {
175 wp->chars = statb.st_size - lseek(sffileno(fp),0L,1);
176 lseek(sffileno(fp),0L,2);
177 }
178 else
179 wc_count(wp, fp, cp);
180 if (fp!=sfstdin)
181 sfclose(fp);
182 tchars += wp->chars;
183 twords += wp->words;
184 tlines += wp->lines;
185 tlines += wp->inval;
186 printout(wp,cp,mode);
187 } while (cp= *argv++);
188 if (n > 1)
189 {
190 wp->chars = tchars;
191 wp->words = twords;
192 wp->lines = tlines;
193 wp->inval = tinval;
194 printout(wp,"total",mode);
195 }
196 return error_info.errors<ERRORMAX?error_info.errors:ERRORMAX;
197 }
198