1 /* $OpenBSD: wc.c,v 1.22 2018/04/26 12:42:51 guenther Exp $ */
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/param.h> /* MAXBSIZE */
45 int64_t tlinect
, twordct
, tcharct
;
46 int doline
, doword
, dochar
, multibyte
;
48 extern char *__progname
;
50 static void print_counts(int64_t, int64_t, int64_t, char *);
51 static void format_and_print(int64_t);
52 static void cnt(char *);
55 main(int argc
, char *argv
[])
59 setlocale(LC_CTYPE
, "");
61 if (pledge("stdio rpath", NULL
) == -1)
64 while ((ch
= getopt(argc
, argv
, "lwcm")) != -1)
82 "usage: %s [-c | -m] [-lw] [file ...]\n",
90 * wc is unusual in that its flags are on by default, so,
91 * if you don't get any arguments, you have to turn them
94 if (!doline
&& !doword
&& !dochar
)
95 doline
= doword
= dochar
= 1;
100 int dototal
= (argc
> 1);
107 print_counts(tlinect
, twordct
, tcharct
, "total");
124 int64_t linect
, wordct
, charct
;
128 linect
= wordct
= charct
= 0;
131 if ((fd
= open(file
, O_RDONLY
, 0)) < 0) {
140 if (!doword
&& !multibyte
) {
141 if (bufsz
< MAXBSIZE
&&
142 (buf
= realloc(buf
, MAXBSIZE
)) == NULL
)
145 * Line counting is split out because it's a lot
146 * faster to get lines than to get words, since
147 * the word count requires some logic.
150 while ((len
= read(fd
, buf
, MAXBSIZE
)) > 0) {
152 for (C
= buf
; len
--; ++C
)
162 * If all we need is the number of characters and
163 * it's a directory or a regular or linked file, just
164 * stat the puppy. We avoid testing for it not being
165 * a special device in case someone adds a new type
171 if (fstat(fd
, &sbuf
)) {
175 ifmt
= sbuf
.st_mode
& S_IFMT
;
176 if (ifmt
== S_IFREG
|| ifmt
== S_IFLNK
177 || ifmt
== S_IFDIR
) {
178 charct
= sbuf
.st_size
;
180 while ((len
= read(fd
, buf
, MAXBSIZE
)) > 0)
192 else if ((stream
= fdopen(fd
, "r")) == NULL
) {
200 * Do it the hard way.
201 * According to POSIX, a word is a "maximal string of
202 * characters delimited by whitespace." Nothing is said
203 * about a character being printing or non-printing.
206 while ((len
= getline(&buf
, &bufsz
, stream
)) > 0) {
208 for (C
= buf
; *C
!= '\0'; C
+= len
) {
210 len
= mbtowc(&wc
, C
, MB_CUR_MAX
);
228 for (C
= buf
; *C
!= '\0'; ++C
) {
229 if (isspace((unsigned char)*C
)) {
240 if (ferror(stream
)) {
246 print_counts(linect
, wordct
, charct
, file
);
249 * Don't bother checking doline, doword, or dochar -- speeds
256 if ((stream
== NULL
? close(fd
) : fclose(stream
)) != 0) {
263 format_and_print(int64_t v
)
269 print_counts(int64_t lines
, int64_t words
, int64_t chars
, char *name
)
272 format_and_print(lines
);
274 format_and_print(words
);
276 format_and_print(chars
);
279 printf(" %s\n", name
);