* Copyright (c) 1980, 1987 Regents of the University of California.
* %sccs.include.redist.c%
"@(#) Copyright (c) 1987, 1991 Regents of the University of California.\n\
static char sccsid
[] = "@(#)wc.c 5.8 (Berkeley) %G%";
u_long tlinect
, twordct
, tcharct
;
int doline
, doword
, dochar
;
void err
__P((const char *, ...));
while ((ch
= getopt(argc
, argv
, "lwc")) != EOF
)
/* Wc's flags are on by default. */
if (doline
+ doword
+ dochar
== 0)
doline
= doword
= dochar
= 1;
(void)printf(" %s\n", *argv
);
(void)printf(" %7ld", tlinect
);
(void)printf(" %7ld", twordct
);
(void)printf(" %7ld", tcharct
);
(void)printf(" total\n");
register u_long linect
, wordct
, charct
;
linect
= wordct
= charct
= 0;
if ((fd
= open(file
, O_RDONLY
, 0)) < 0)
err("%s: %s", file
, strerror(errno
));
* Line counting is split out because it's a lot faster to get
* lines than to get words, since the word count requires some
while (len
= read(fd
, buf
, MAXBSIZE
)) {
err("%s: %s", file
, strerror(errno
));
for (p
= buf
; len
--; ++p
)
(void)printf(" %7lu", linect
);
(void)printf(" %7lu", charct
);
* If all we need is the number of characters and it's a
* regular or linked file, just stat the puppy.
err("%s: %s", file
, strerror(errno
));
if (S_ISREG(sb
.st_mode
) || S_ISLNK(sb
.st_mode
)) {
(void)printf(" %7lu", sb
.st_size
);
/* Do it the hard way... */
word
: for (gotsp
= 1; len
= read(fd
, buf
, MAXBSIZE
);) {
err("%s: %s", file
, strerror(errno
));
* This loses in the presence of multi-byte characters.
* To do it right would require a function to return a
* character while knowing how many bytes it consumed.
(void)printf(" %7lu", linect
);
(void)printf(" %7lu", wordct
);
(void)printf(" %7lu", charct
);
(void)fprintf(stderr
, "usage: wc [-clw] [files]\n");
err(const char *fmt
, ...)
(void)fprintf(stderr
, "wc: ");
(void)vfprintf(stderr
, fmt
, ap
);
(void)fprintf(stderr
, "\n");