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
32 #include <sys/capsicum.h>
33 #include <sys/param.h>
36 #include <capsicum_helpers.h>
51 #include <libcasper.h>
52 #include <casper/cap_fileargs.h>
54 static const char *stdin_filename = "stdin";
56 static fileargs_t *fa;
57 static uintmax_t tlinect, twordct, tcharct, tlongline;
58 static bool doline, doword, dochar, domulti, dolongline;
59 static volatile sig_atomic_t siginfo;
60 static xo_handle_t *stderr_handle;
62 static void show_cnt(const char *file, uintmax_t linect, uintmax_t wordct,
63 uintmax_t charct, uintmax_t llct);
64 static int cnt(const char *);
65 static void usage(void);
68 siginfo_handler(int sig __unused)
78 signal(SIGINFO, SIG_DFL);
83 main(int argc, char *argv[])
85 int ch, errors, total;
88 (void) setlocale(LC_CTYPE, "");
90 argc = xo_parse_args(argc, argv);
94 while ((ch = getopt(argc, argv, "clmwL")) != -1)
120 fa = fileargs_init(argc, argv, O_RDONLY, 0,
121 cap_rights_init(&rights, CAP_READ, CAP_FSTAT), FA_OPEN);
123 xo_err(EXIT_FAILURE, "Unable to initialize casper");
124 caph_cache_catpages();
125 if (caph_limit_stdio() < 0)
126 xo_err(EXIT_FAILURE, "Unable to limit stdio");
127 if (caph_enter_casper() < 0)
128 xo_err(EXIT_FAILURE, "Unable to enter capability mode");
130 /* Wc's flags are on by default. */
131 if (!(doline || doword || dochar || domulti || dolongline))
132 doline = doword = dochar = true;
134 stderr_handle = xo_create_to_file(stderr, XO_STYLE_TEXT, 0);
135 xo_open_container("wc");
136 xo_open_list("file");
138 (void)signal(SIGINFO, siginfo_handler);
142 xo_open_instance("file");
145 xo_close_instance("file");
148 xo_open_instance("file");
149 if (cnt(*argv++) != 0)
151 xo_close_instance("file");
156 xo_close_list("file");
159 xo_open_container("total");
160 show_cnt("total", tlinect, twordct, tcharct, tlongline);
161 xo_close_container("total");
165 xo_close_container("wc");
167 xo_err(EXIT_FAILURE, "stdout");
168 exit(errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
172 show_cnt(const char *file, uintmax_t linect, uintmax_t wordct,
173 uintmax_t charct, uintmax_t llct)
185 xo_emit_h(xop, " {:lines/%7ju/%ju}", linect);
187 xo_emit_h(xop, " {:words/%7ju/%ju}", wordct);
188 if (dochar || domulti)
189 xo_emit_h(xop, " {:characters/%7ju/%ju}", charct);
191 xo_emit_h(xop, " {:long-lines/%7ju/%ju}", llct);
192 if (file != stdin_filename)
193 xo_emit_h(xop, " {:filename/%s}\n", file);
195 xo_emit_h(xop, "\n");
199 cnt(const char *file)
201 static char buf[MAXBSIZE];
205 uintmax_t linect, wordct, charct, llct, tmpll;
212 linect = wordct = charct = llct = tmpll = 0;
215 file = stdin_filename;
216 } else if ((fd = fileargs_open(fa, file)) < 0) {
217 xo_warn("%s: open", file);
220 if (doword || (domulti && MB_CUR_MAX != 1))
223 * If all we need is the number of characters and it's a regular file,
226 if (doline == 0 && dolongline == 0) {
227 if (fstat(fd, &sb)) {
228 xo_warn("%s: fstat", file);
232 /* pseudo-filesystems advertize a zero size */
233 if (S_ISREG(sb.st_mode) && sb.st_size > 0) {
236 show_cnt(file, linect, wordct, charct, llct);
243 * For files we can't stat, or if we need line counting, slurp the
244 * file. Line counting is split out because it's a lot faster to get
245 * lines than to get words, since the word count requires locale
248 while ((len = read(fd, buf, sizeof(buf))) != 0) {
250 xo_warn("%s: read", file);
255 show_cnt(file, linect, wordct, charct, llct);
257 if (doline || dolongline) {
258 for (p = buf; len > 0; --len, ++p) {
275 if (dolongline && llct > tlongline)
277 show_cnt(file, linect, wordct, charct, llct);
281 /* Do it the hard way... */
284 memset(&mbs, 0, sizeof(mbs));
285 while ((len = read(fd, buf, sizeof(buf))) != 0) {
287 xo_warn("%s: read", file);
294 show_cnt(file, linect, wordct, charct, llct);
295 if (!domulti || MB_CUR_MAX == 1) {
297 wch = (unsigned char)*p;
298 } else if ((clen = mbrtowc(&wch, p, len, &mbs)) == 0) {
300 } else if (clen == (size_t)-1) {
306 memset(&mbs, 0, sizeof(mbs));
308 wch = (unsigned char)*p;
309 } else if (clen == (size_t)-2) {
332 if (domulti && MB_CUR_MAX > 1) {
333 if (mbrtowc(NULL, NULL, 0, &mbs) == (size_t)-1 && !warned)
340 if (dochar || domulti)
342 if (dolongline && llct > tlongline)
344 show_cnt(file, linect, wordct, charct, llct);
352 xo_error("usage: wc [-Lclmw] [file ...]\n");