2 * Copyright (c) 1980, 1987, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1980, 1987, 1991, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)wc.c 8.1 (Berkeley) 6/6/93
31 * $FreeBSD: head/usr.bin/wc/wc.c 281617 2015-04-16 21:44:35Z bdrewery $
34 #include <sys/param.h>
50 static uintmax_t tlinect
, twordct
, tcharct
, tlongline
;
51 static int doline
, doword
, dochar
, domulti
, dolongline
;
52 static volatile sig_atomic_t siginfo
;
54 static void show_cnt(const char *file
, uintmax_t linect
, uintmax_t wordct
,
55 uintmax_t charct
, uintmax_t llct
);
56 static int cnt(const char *);
57 static void usage(void);
61 siginfo_handler(int sig __unused
)
71 signal(SIGINFO
, SIG_DFL
);
77 main(int argc
, char *argv
[])
79 int ch
, errors
, total
;
81 setlocale(LC_CTYPE
, "");
83 while ((ch
= getopt(argc
, argv
, "clmwL")) != -1) {
111 signal(SIGINFO
, siginfo_handler
);
114 /* Wc's flags are on by default. */
115 if (doline
+ doword
+ dochar
+ domulti
+ dolongline
== 0)
116 doline
= doword
= dochar
= 1;
132 show_cnt("total", tlinect
, twordct
, tcharct
, tlongline
);
133 exit(errors
== 0 ? 0 : 1);
137 show_cnt(const char *file
, uintmax_t linect
, uintmax_t wordct
,
138 uintmax_t charct
, uintmax_t llct
)
150 fprintf(out
, " %7ju", linect
);
152 fprintf(out
, " %7ju", wordct
);
153 if (dochar
|| domulti
)
154 fprintf(out
, " %7ju", charct
);
156 fprintf(out
, " %7ju", llct
);
158 fprintf(out
, " %s\n", file
);
164 cnt(const char *file
)
167 uintmax_t linect
, wordct
, charct
, llct
, tmpll
;
172 u_char buf
[MAXBSIZE
];
176 linect
= wordct
= charct
= llct
= tmpll
= 0;
180 if ((fd
= open(file
, O_RDONLY
)) < 0) {
181 warn("%s: open", file
);
184 if (doword
|| (domulti
&& MB_CUR_MAX
!= 1))
187 * Line counting is split out because it's a lot faster to get
188 * lines than to get words, since the word count requires some
192 while ((len
= read(fd
, buf
, MAXBSIZE
))) {
194 warn("%s: read", file
);
199 show_cnt(file
, linect
, wordct
, charct
,
203 for (p
= buf
; len
--; ++p
) {
218 if (llct
> tlongline
)
221 show_cnt(file
, linect
, wordct
, charct
, llct
);
226 * If all we need is the number of characters and it's a
227 * regular file, just stat the puppy.
229 if (dochar
|| domulti
) {
230 if (fstat(fd
, &sb
)) {
231 warn("%s: fstat", file
);
235 /* Pseudo-filesystems advertize a zero size. */
236 if (S_ISREG(sb
.st_mode
) && sb
.st_size
> 0) {
239 show_cnt(file
, linect
, wordct
, charct
, llct
);
247 /* Do it the hard way... */
250 memset(&mbs
, 0, sizeof(mbs
));
251 while ((len
= read(fd
, buf
, MAXBSIZE
)) != 0) {
253 warn("%s: read", file
!= NULL
? file
: "stdin");
260 show_cnt(file
, linect
, wordct
, charct
, llct
);
261 if (!domulti
|| MB_CUR_MAX
== 1) {
263 wch
= (unsigned char)*p
;
264 } else if ((clen
= mbrtowc(&wch
, p
, len
, &mbs
)) ==
269 file
!= NULL
? file
: "stdin");
272 memset(&mbs
, 0, sizeof(mbs
));
274 wch
= (unsigned char)*p
;
275 } else if (clen
== (size_t)-2)
299 if (domulti
&& MB_CUR_MAX
> 1)
300 if (mbrtowc(NULL
, NULL
, 0, &mbs
) == (size_t)-1 && !warned
)
301 warn("%s", file
!= NULL
? file
: "stdin");
306 if (dochar
|| domulti
)
309 if (llct
> tlongline
)
312 show_cnt(file
, linect
, wordct
, charct
, llct
);
320 fprintf(stderr
, "usage: wc [-Lclmw] [file ...]\n");