2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1987, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
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
33 static const char copyright[] =
34 "@(#) Copyright (c) 1980, 1987, 1991, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)wc.c 8.1 (Berkeley) 6/6/93";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
64 static uintmax_t tlinect, twordct, tcharct, tlongline;
65 static int doline, doword, dochar, domulti, dolongline;
66 static volatile sig_atomic_t siginfo;
67 static xo_handle_t *stderr_handle;
69 static void show_cnt(const char *file, uintmax_t linect, uintmax_t wordct,
70 uintmax_t charct, uintmax_t llct);
71 static int cnt(const char *);
72 static void usage(void);
75 siginfo_handler(int sig __unused)
85 signal(SIGINFO, SIG_DFL);
90 main(int argc, char *argv[])
92 int ch, errors, total;
94 (void) setlocale(LC_CTYPE, "");
96 argc = xo_parse_args(argc, argv);
100 while ((ch = getopt(argc, argv, "clmwL")) != -1)
126 (void)signal(SIGINFO, siginfo_handler);
128 /* Wc's flags are on by default. */
129 if (doline + doword + dochar + domulti + dolongline == 0)
130 doline = doword = dochar = 1;
132 stderr_handle = xo_create_to_file(stderr, XO_STYLE_TEXT, 0);
133 xo_open_container("wc");
134 xo_open_list("file");
139 xo_open_instance("file");
140 if (cnt((char *)NULL) != 0)
142 xo_close_instance("file");
145 xo_open_instance("file");
148 xo_close_instance("file");
153 xo_close_list("file");
156 xo_open_container("total");
157 show_cnt("total", tlinect, twordct, tcharct, tlongline);
158 xo_close_container("total");
161 xo_close_container("wc");
163 exit(errors == 0 ? 0 : 1);
167 show_cnt(const char *file, uintmax_t linect, uintmax_t wordct,
168 uintmax_t charct, uintmax_t llct)
180 xo_emit_h(xop, " {:lines/%7ju/%ju}", linect);
182 xo_emit_h(xop, " {:words/%7ju/%ju}", wordct);
183 if (dochar || domulti)
184 xo_emit_h(xop, " {:characters/%7ju/%ju}", charct);
186 xo_emit_h(xop, " {:long-lines/%7ju/%ju}", llct);
188 xo_emit_h(xop, " {:filename/%s}\n", file);
190 xo_emit_h(xop, "\n");
194 cnt(const char *file)
197 uintmax_t linect, wordct, charct, llct, tmpll;
202 u_char buf[MAXBSIZE];
206 linect = wordct = charct = llct = tmpll = 0;
209 else if ((fd = open(file, O_RDONLY, 0)) < 0) {
210 xo_warn("%s: open", file);
213 if (doword || (domulti && MB_CUR_MAX != 1))
216 * If all we need is the number of characters and it's a regular file,
220 if (fstat(fd, &sb)) {
221 xo_warn("%s: fstat", file);
225 if (S_ISREG(sb.st_mode)) {
228 show_cnt(file, linect, wordct, charct, llct);
235 * For files we can't stat, or if we need line counting, slurp the
236 * file. Line counting is split out because it's a lot faster to get
237 * lines than to get words, since the word count requires locale
240 while ((len = read(fd, buf, MAXBSIZE))) {
242 xo_warn("%s: read", file);
247 show_cnt(file, linect, wordct, charct, llct);
250 for (p = buf; len--; ++p)
266 if (llct > tlongline)
269 show_cnt(file, linect, wordct, charct, llct);
273 /* Do it the hard way... */
276 memset(&mbs, 0, sizeof(mbs));
277 while ((len = read(fd, buf, MAXBSIZE)) != 0) {
279 xo_warn("%s: read", file != NULL ? file : "stdin");
286 show_cnt(file, linect, wordct, charct, llct);
287 if (!domulti || MB_CUR_MAX == 1) {
289 wch = (unsigned char)*p;
290 } else if ((clen = mbrtowc(&wch, p, len, &mbs)) ==
295 file != NULL ? file : "stdin");
298 memset(&mbs, 0, sizeof(mbs));
300 wch = (unsigned char)*p;
301 } else if (clen == (size_t)-2)
325 if (domulti && MB_CUR_MAX > 1)
326 if (mbrtowc(NULL, NULL, 0, &mbs) == (size_t)-1 && !warned)
327 xo_warn("%s", file != NULL ? file : "stdin");
332 if (dochar || domulti)
335 if (llct > tlongline)
338 show_cnt(file, linect, wordct, charct, llct);
346 xo_error("usage: wc [-Lclmw] [file ...]\n");