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/capsicum.h>
48 #include <sys/param.h>
51 #include <capsicum_helpers.h>
66 #include <libcasper.h>
67 #include <casper/cap_fileargs.h>
69 static fileargs_t *fa;
70 static uintmax_t tlinect, twordct, tcharct, tlongline;
71 static int doline, doword, dochar, domulti, dolongline;
72 static volatile sig_atomic_t siginfo;
73 static xo_handle_t *stderr_handle;
75 static void show_cnt(const char *file, uintmax_t linect, uintmax_t wordct,
76 uintmax_t charct, uintmax_t llct);
77 static int cnt(const char *);
78 static void usage(void);
81 siginfo_handler(int sig __unused)
91 signal(SIGINFO, SIG_DFL);
96 main(int argc, char *argv[])
98 int ch, errors, total;
101 (void) setlocale(LC_CTYPE, "");
103 argc = xo_parse_args(argc, argv);
107 while ((ch = getopt(argc, argv, "clmwL")) != -1)
133 (void)signal(SIGINFO, siginfo_handler);
135 fa = fileargs_init(argc, argv, O_RDONLY, 0,
136 cap_rights_init(&rights, CAP_READ, CAP_FSTAT), FA_OPEN);
138 xo_warn("Unable to init casper");
142 caph_cache_catpages();
143 if (caph_limit_stdio() < 0) {
144 xo_warn("Unable to limit stdio");
149 if (caph_enter_casper() < 0) {
150 xo_warn("Unable to enter capability mode");
155 /* Wc's flags are on by default. */
156 if (doline + doword + dochar + domulti + dolongline == 0)
157 doline = doword = dochar = 1;
159 stderr_handle = xo_create_to_file(stderr, XO_STYLE_TEXT, 0);
160 xo_open_container("wc");
161 xo_open_list("file");
166 xo_open_instance("file");
167 if (cnt((char *)NULL) != 0)
169 xo_close_instance("file");
172 xo_open_instance("file");
175 xo_close_instance("file");
180 xo_close_list("file");
183 xo_open_container("total");
184 show_cnt("total", tlinect, twordct, tcharct, tlongline);
185 xo_close_container("total");
189 xo_close_container("wc");
191 exit(errors == 0 ? 0 : 1);
195 show_cnt(const char *file, uintmax_t linect, uintmax_t wordct,
196 uintmax_t charct, uintmax_t llct)
208 xo_emit_h(xop, " {:lines/%7ju/%ju}", linect);
210 xo_emit_h(xop, " {:words/%7ju/%ju}", wordct);
211 if (dochar || domulti)
212 xo_emit_h(xop, " {:characters/%7ju/%ju}", charct);
214 xo_emit_h(xop, " {:long-lines/%7ju/%ju}", llct);
216 xo_emit_h(xop, " {:filename/%s}\n", file);
218 xo_emit_h(xop, "\n");
222 cnt(const char *file)
225 uintmax_t linect, wordct, charct, llct, tmpll;
230 u_char buf[MAXBSIZE];
234 linect = wordct = charct = llct = tmpll = 0;
237 else if ((fd = fileargs_open(fa, file)) < 0) {
238 xo_warn("%s: open", file);
241 if (doword || (domulti && MB_CUR_MAX != 1))
244 * If all we need is the number of characters and it's a regular file,
247 if (doline == 0 && dolongline == 0) {
248 if (fstat(fd, &sb)) {
249 xo_warn("%s: fstat", file != NULL ? file : "stdin");
253 if (S_ISREG(sb.st_mode)) {
256 show_cnt(file, linect, wordct, charct, llct);
263 * For files we can't stat, or if we need line counting, slurp the
264 * file. Line counting is split out because it's a lot faster to get
265 * lines than to get words, since the word count requires locale
268 while ((len = read(fd, buf, MAXBSIZE))) {
270 xo_warn("%s: read", file != NULL ? file : "stdin");
275 show_cnt(file, linect, wordct, charct, llct);
277 if (doline || dolongline) {
278 for (p = buf; len--; ++p)
293 if (dolongline && llct > tlongline)
295 show_cnt(file, linect, wordct, charct, llct);
299 /* Do it the hard way... */
302 memset(&mbs, 0, sizeof(mbs));
303 while ((len = read(fd, buf, MAXBSIZE)) != 0) {
305 xo_warn("%s: read", file != NULL ? file : "stdin");
312 show_cnt(file, linect, wordct, charct, llct);
313 if (!domulti || MB_CUR_MAX == 1) {
315 wch = (unsigned char)*p;
316 } else if ((clen = mbrtowc(&wch, p, len, &mbs)) ==
321 file != NULL ? file : "stdin");
324 memset(&mbs, 0, sizeof(mbs));
326 wch = (unsigned char)*p;
327 } else if (clen == (size_t)-2)
351 if (domulti && MB_CUR_MAX > 1)
352 if (mbrtowc(NULL, NULL, 0, &mbs) == (size_t)-1 && !warned)
353 xo_warn("%s", file != NULL ? file : "stdin");
358 if (dochar || domulti)
360 if (dolongline && llct > tlongline)
362 show_cnt(file, linect, wordct, charct, llct);
370 xo_error("usage: wc [-Lclmw] [file ...]\n");