2 * Copyright (c) 1980, 1987, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1980, 1987, 1991, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)wc.c 8.1 (Berkeley) 6/6/93
31 * $FreeBSD: src/usr.bin/wc/wc.c,v 1.11.2.1 2002/08/25 02:47:04 tjr Exp $
32 * $DragonFly: src/usr.bin/wc/wc.c,v 1.5 2005/02/05 16:07:08 liamfoy Exp $
35 #include <sys/param.h>
48 uintmax_t tlinect
, twordct
, tcharct
;
49 int doline
, doword
, dochar
, domulti
;
51 static int cnt(const char *);
52 static void usage(void);
55 main(int argc
, char **argv
)
57 int ch
, errors
, total
;
59 setlocale(LC_CTYPE
, "");
61 while ((ch
= getopt(argc
, argv
, "clmw")) != -1) {
85 /* Wc's flags are on by default. */
86 if (doline
+ doword
+ dochar
+ domulti
== 0)
87 doline
= doword
= dochar
= 1;
101 printf(" %s\n", *argv
);
107 printf(" %7ju", tlinect
);
109 printf(" %7ju", twordct
);
110 if (dochar
|| domulti
)
111 printf(" %7ju", tcharct
);
114 exit(errors
== 0 ? 0 : 1);
118 cnt(const char *file
)
121 u_quad_t linect
, wordct
, charct
;
123 int clen
, fd
, len
, warned
;
126 u_char buf
[MAXBSIZE
];
129 linect
= wordct
= charct
= 0;
134 if ((fd
= open(file
, O_RDONLY
)) < 0) {
135 warn("%s: open", file
);
138 if (doword
|| (domulti
&& MB_CUR_MAX
!= 1))
141 * Line counting is split out because it's a lot faster to get
142 * lines than to get words, since the word count requires some
146 while ((len
= read(fd
, buf
, MAXBSIZE
))) {
148 warn("%s: read", file
);
153 for (p
= buf
; len
--; ++p
) {
159 printf(" %7ju", linect
);
162 printf(" %7ju", charct
);
168 * If all we need is the number of characters and it's a
169 * regular file, just stat the puppy.
171 if (dochar
|| domulti
) {
172 if (fstat(fd
, &sb
)) {
173 warn("%s: fstat", file
);
177 if (S_ISREG(sb
.st_mode
)) {
178 printf(" %7lld", (long long)sb
.st_size
);
179 tcharct
+= sb
.st_size
;
186 /* Do it the hard way... */
190 while ((nread
= read(fd
, buf
+ len
, MAXBSIZE
- len
)) != 0) {
192 warn("%s: read", file
);
199 if (!domulti
|| MB_CUR_MAX
== 1) {
201 wch
= (unsigned char)*p
;
202 } else if ((clen
= mbtowc(&wch
, p
, len
)) <= 0) {
203 if (len
> MB_CUR_MAX
) {
205 wch
= (unsigned char)*p
;
212 memmove(buf
, p
, len
);
231 printf(" %7ju", linect
);
235 printf(" %7ju", wordct
);
237 if (dochar
|| domulti
) {
239 printf(" %7ju", charct
);
248 fprintf(stderr
, "usage: wc [-clmw] [file ...]\n");