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 const char *stdin_filename = "stdin";
71 static fileargs_t *fa;
72 static uintmax_t tlinect, twordct, tcharct, tlongline;
73 static bool doline, doword, dochar, domulti, dolongline;
74 static volatile sig_atomic_t siginfo;
75 static xo_handle_t *stderr_handle;
77 static void show_cnt(const char *file, uintmax_t linect, uintmax_t wordct,
78 uintmax_t charct, uintmax_t llct);
79 static int cnt(const char *);
80 static void usage(void);
83 siginfo_handler(int sig __unused)
93 signal(SIGINFO, SIG_DFL);
98 main(int argc, char *argv[])
100 int ch, errors, total;
103 (void) setlocale(LC_CTYPE, "");
105 argc = xo_parse_args(argc, argv);
109 while ((ch = getopt(argc, argv, "clmwL")) != -1)
135 (void)signal(SIGINFO, siginfo_handler);
137 fa = fileargs_init(argc, argv, O_RDONLY, 0,
138 cap_rights_init(&rights, CAP_READ, CAP_FSTAT), FA_OPEN);
140 xo_err(EXIT_FAILURE, "Unable to initialize casper");
141 caph_cache_catpages();
142 if (caph_limit_stdio() < 0)
143 xo_err(EXIT_FAILURE, "Unable to limit stdio");
144 if (caph_enter_casper() < 0)
145 xo_err(EXIT_FAILURE, "Unable to enter capability mode");
147 /* Wc's flags are on by default. */
148 if (!(doline || doword || dochar || domulti || dolongline))
149 doline = doword = dochar = true;
151 stderr_handle = xo_create_to_file(stderr, XO_STYLE_TEXT, 0);
152 xo_open_container("wc");
153 xo_open_list("file");
158 xo_open_instance("file");
161 xo_close_instance("file");
164 xo_open_instance("file");
165 if (cnt(*argv++) != 0)
167 xo_close_instance("file");
172 xo_close_list("file");
175 xo_open_container("total");
176 show_cnt("total", tlinect, twordct, tcharct, tlongline);
177 xo_close_container("total");
181 xo_close_container("wc");
183 xo_err(EXIT_FAILURE, "stdout");
184 exit(errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
188 show_cnt(const char *file, uintmax_t linect, uintmax_t wordct,
189 uintmax_t charct, uintmax_t llct)
201 xo_emit_h(xop, " {:lines/%7ju/%ju}", linect);
203 xo_emit_h(xop, " {:words/%7ju/%ju}", wordct);
204 if (dochar || domulti)
205 xo_emit_h(xop, " {:characters/%7ju/%ju}", charct);
207 xo_emit_h(xop, " {:long-lines/%7ju/%ju}", llct);
208 if (file != stdin_filename)
209 xo_emit_h(xop, " {:filename/%s}\n", file);
211 xo_emit_h(xop, "\n");
215 cnt(const char *file)
217 static char buf[MAXBSIZE];
221 uintmax_t linect, wordct, charct, llct, tmpll;
228 linect = wordct = charct = llct = tmpll = 0;
231 file = stdin_filename;
232 } else if ((fd = fileargs_open(fa, file)) < 0) {
233 xo_warn("%s: open", file);
236 if (doword || (domulti && MB_CUR_MAX != 1))
239 * If all we need is the number of characters and it's a regular file,
242 if (doline == 0 && dolongline == 0) {
243 if (fstat(fd, &sb)) {
244 xo_warn("%s: fstat", file);
248 if (S_ISREG(sb.st_mode)) {
251 show_cnt(file, linect, wordct, charct, llct);
258 * For files we can't stat, or if we need line counting, slurp the
259 * file. Line counting is split out because it's a lot faster to get
260 * lines than to get words, since the word count requires locale
263 while ((len = read(fd, buf, sizeof(buf))) != 0) {
265 xo_warn("%s: read", file);
270 show_cnt(file, linect, wordct, charct, llct);
272 if (doline || dolongline) {
273 for (p = buf; len > 0; --len, ++p) {
290 if (dolongline && llct > tlongline)
292 show_cnt(file, linect, wordct, charct, llct);
296 /* Do it the hard way... */
299 memset(&mbs, 0, sizeof(mbs));
300 while ((len = read(fd, buf, sizeof(buf))) != 0) {
302 xo_warn("%s: read", file);
309 show_cnt(file, linect, wordct, charct, llct);
310 if (!domulti || MB_CUR_MAX == 1) {
312 wch = (unsigned char)*p;
313 } else if ((clen = mbrtowc(&wch, p, len, &mbs)) == 0) {
315 } else if (clen == (size_t)-1) {
321 memset(&mbs, 0, sizeof(mbs));
323 wch = (unsigned char)*p;
324 } else if (clen == (size_t)-2) {
347 if (domulti && MB_CUR_MAX > 1) {
348 if (mbrtowc(NULL, NULL, 0, &mbs) == (size_t)-1 && !warned)
355 if (dochar || domulti)
357 if (dolongline && llct > tlongline)
359 show_cnt(file, linect, wordct, charct, llct);
367 xo_error("usage: wc [-Lclmw] [file ...]\n");