Home | History | Annotate | Line # | Download | only in wc
wc.c revision 1.31.6.1
      1  1.31.6.1       jym /*	$NetBSD: wc.c,v 1.31.6.1 2009/05/13 19:20:11 jym Exp $	*/
      2      1.10       tls 
      3       1.1       cgd /*
      4      1.11       mrg  * Copyright (c) 1980, 1987, 1991, 1993
      5      1.11       mrg  *	The Regents of the University of California.  All rights reserved.
      6       1.1       cgd  *
      7       1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8       1.1       cgd  * modification, are permitted provided that the following conditions
      9       1.1       cgd  * are met:
     10       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15      1.29       agc  * 3. Neither the name of the University nor the names of its contributors
     16       1.1       cgd  *    may be used to endorse or promote products derived from this software
     17       1.1       cgd  *    without specific prior written permission.
     18       1.1       cgd  *
     19       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29       1.1       cgd  * SUCH DAMAGE.
     30       1.1       cgd  */
     31       1.1       cgd 
     32      1.13       mrg #include <sys/cdefs.h>
     33       1.1       cgd #ifndef lint
     34      1.31     lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1987, 1991, 1993\
     35      1.31     lukem  The Regents of the University of California.  All rights reserved.");
     36       1.1       cgd #endif /* not lint */
     37       1.1       cgd 
     38       1.1       cgd #ifndef lint
     39      1.11       mrg #if 0
     40      1.11       mrg static char sccsid[] = "@(#)wc.c	8.2 (Berkeley) 5/2/95";
     41      1.11       mrg #else
     42  1.31.6.1       jym __RCSID("$NetBSD: wc.c,v 1.31.6.1 2009/05/13 19:20:11 jym Exp $");
     43      1.11       mrg #endif
     44       1.1       cgd #endif /* not lint */
     45       1.1       cgd 
     46       1.1       cgd /* wc line, word and char count */
     47       1.1       cgd 
     48      1.11       mrg #include <sys/param.h>
     49      1.22     enami #include <sys/file.h>
     50      1.11       mrg #include <sys/stat.h>
     51      1.11       mrg 
     52      1.22     enami #include <ctype.h>
     53      1.11       mrg #include <fcntl.h>
     54      1.22     enami #include <err.h>
     55      1.11       mrg #include <errno.h>
     56      1.22     enami #include <locale.h>
     57       1.1       cgd #include <stdio.h>
     58       1.2       jtc #include <stdlib.h>
     59       1.2       jtc #include <string.h>
     60       1.5       jtc #include <unistd.h>
     61      1.21      yamt #include <wchar.h>
     62      1.21      yamt #include <wctype.h>
     63       1.1       cgd 
     64      1.17  christos #ifdef NO_QUAD
     65      1.17  christos typedef u_long wc_count_t;
     66      1.18   mycroft # define WCFMT	" %7lu"
     67      1.17  christos # define WCCAST unsigned long
     68      1.17  christos #else
     69      1.17  christos typedef u_quad_t wc_count_t;
     70      1.18   mycroft # define WCFMT	" %7llu"
     71      1.17  christos # define WCCAST	unsigned long long
     72      1.17  christos #endif
     73      1.17  christos 
     74      1.17  christos static wc_count_t	tlinect, twordct, tcharct;
     75      1.21      yamt static int		doline, doword, dobyte, dochar;
     76      1.14  wsanchez static int 		rval = 0;
     77       1.1       cgd 
     78  1.31.6.1       jym static void	cnt(const char *);
     79  1.31.6.1       jym static void	print_counts(wc_count_t, wc_count_t, wc_count_t, const char *);
     80      1.30     perry static void	usage(void);
     81      1.30     perry static size_t	do_mb(wchar_t *, const char *, size_t, mbstate_t *,
     82      1.30     perry 		    size_t *, const char *);
     83      1.30     perry int	main(int, char *[]);
     84      1.11       mrg 
     85       1.5       jtc int
     86      1.30     perry main(int argc, char *argv[])
     87       1.1       cgd {
     88      1.13       mrg 	int ch;
     89       1.1       cgd 
     90       1.7       jtc 	setlocale(LC_ALL, "");
     91       1.7       jtc 
     92       1.6       jtc 	while ((ch = getopt(argc, argv, "lwcm")) != -1)
     93      1.24     enami 		switch (ch) {
     94       1.2       jtc 		case 'l':
     95       1.2       jtc 			doline = 1;
     96       1.2       jtc 			break;
     97       1.2       jtc 		case 'w':
     98       1.2       jtc 			doword = 1;
     99       1.2       jtc 			break;
    100       1.4       jtc 		case 'm':
    101       1.2       jtc 			dochar = 1;
    102      1.21      yamt 			dobyte = 0;
    103      1.21      yamt 			break;
    104      1.21      yamt 		case 'c':
    105      1.21      yamt 			dochar = 0;
    106      1.21      yamt 			dobyte = 1;
    107       1.2       jtc 			break;
    108       1.2       jtc 		case '?':
    109       1.2       jtc 		default:
    110      1.11       mrg 			usage();
    111       1.2       jtc 		}
    112       1.2       jtc 	argv += optind;
    113       1.2       jtc 	argc -= optind;
    114       1.2       jtc 
    115      1.11       mrg 	/* Wc's flags are on by default. */
    116      1.21      yamt 	if (doline + doword + dobyte + dochar == 0)
    117      1.21      yamt 		doline = doword = dobyte = 1;
    118       1.1       cgd 
    119       1.1       cgd 	if (!*argv) {
    120      1.11       mrg 		cnt(NULL);
    121       1.2       jtc 	} else {
    122       1.2       jtc 		int dototal = (argc > 1);
    123       1.2       jtc 
    124       1.2       jtc 		do {
    125       1.2       jtc 			cnt(*argv);
    126       1.2       jtc 		} while(*++argv);
    127       1.2       jtc 
    128      1.19   mycroft 		if (dototal)
    129      1.23     enami 			print_counts(tlinect, twordct, tcharct, "total");
    130       1.1       cgd 	}
    131       1.2       jtc 
    132       1.6       jtc 	exit(rval);
    133       1.1       cgd }
    134       1.1       cgd 
    135      1.21      yamt static size_t
    136  1.31.6.1       jym do_mb(wchar_t *wc, const char *p, size_t len, mbstate_t *st,
    137  1.31.6.1       jym     size_t *retcnt, const char *file)
    138      1.21      yamt {
    139      1.21      yamt 	size_t r;
    140      1.21      yamt 	size_t c = 0;
    141      1.21      yamt 
    142      1.21      yamt 	do {
    143  1.31.6.1       jym 		r = mbrtowc(wc, p, len, st);
    144      1.21      yamt 		if (r == (size_t)-1) {
    145      1.21      yamt 			warnx("%s: invalid byte sequence", file);
    146      1.21      yamt 			rval = 1;
    147      1.21      yamt 
    148      1.21      yamt 			/* XXX skip 1 byte */
    149  1.31.6.1       jym 			len--;
    150      1.23     enami 			p++;
    151      1.21      yamt 			memset(st, 0, sizeof(*st));
    152      1.26     enami 			continue;
    153      1.23     enami 		} else if (r == (size_t)-2)
    154      1.21      yamt 			break;
    155      1.21      yamt 		else if (r == 0)
    156      1.21      yamt 			r = 1;
    157      1.23     enami 		c++;
    158      1.21      yamt 		if (wc)
    159      1.23     enami 			wc++;
    160  1.31.6.1       jym 		len -= r;
    161      1.21      yamt 		p += r;
    162  1.31.6.1       jym 	} while (len > 0);
    163      1.21      yamt 
    164  1.31.6.1       jym 	*retcnt = c;
    165      1.21      yamt 
    166      1.24     enami 	return (r);
    167      1.21      yamt }
    168      1.23     enami 
    169       1.5       jtc static void
    170  1.31.6.1       jym cnt(const char *file)
    171       1.1       cgd {
    172      1.24     enami 	u_char buf[MAXBSIZE];
    173      1.24     enami 	wchar_t wbuf[MAXBSIZE];
    174      1.24     enami 	struct stat sb;
    175      1.24     enami 	wc_count_t charct, linect, wordct;
    176      1.24     enami 	mbstate_t st;
    177      1.13       mrg 	u_char *C;
    178      1.21      yamt 	wchar_t *WC;
    179  1.31.6.1       jym 	const char *name;			/* filename or <stdin> */
    180      1.21      yamt 	size_t r = 0;
    181      1.24     enami 	int fd, gotsp, len = 0;
    182       1.1       cgd 
    183       1.1       cgd 	linect = wordct = charct = 0;
    184       1.1       cgd 	if (file) {
    185       1.1       cgd 		if ((fd = open(file, O_RDONLY, 0)) < 0) {
    186      1.11       mrg 			warn("%s", file);
    187       1.6       jtc 			rval = 1;
    188       1.6       jtc 			return;
    189       1.1       cgd 		}
    190      1.27     enami 		name = file;
    191      1.23     enami 	} else {
    192       1.7       jtc 		fd = STDIN_FILENO;
    193      1.27     enami 		name = "<stdin>";
    194       1.7       jtc 	}
    195      1.21      yamt 
    196      1.21      yamt 	if (dochar || doword)
    197      1.21      yamt 		memset(&st, 0, sizeof(st));
    198      1.23     enami 
    199       1.7       jtc 	if (!doword) {
    200       1.7       jtc 		/*
    201       1.7       jtc 		 * line counting is split out because it's a lot
    202       1.7       jtc 		 * faster to get lines than to get words, since
    203       1.7       jtc 		 * the word count requires some logic.
    204       1.7       jtc 		 */
    205      1.21      yamt 		if (doline || dochar) {
    206      1.11       mrg 			while ((len = read(fd, buf, MAXBSIZE)) > 0) {
    207      1.21      yamt 				if (dochar) {
    208      1.21      yamt 					size_t wlen;
    209      1.21      yamt 
    210      1.23     enami 					r = do_mb(0, (char *)buf, (size_t)len,
    211      1.27     enami 					    &st, &wlen, name);
    212      1.21      yamt 					charct += wlen;
    213      1.23     enami 				} else if (dobyte)
    214      1.21      yamt 					charct += len;
    215      1.21      yamt 				if (doline)
    216      1.21      yamt 					for (C = buf; len--; ++C)
    217      1.21      yamt 						if (*C == '\n')
    218      1.21      yamt 							++linect;
    219       1.1       cgd 			}
    220       1.7       jtc 		}
    221       1.1       cgd 
    222       1.7       jtc 		/*
    223       1.7       jtc 		 * if all we need is the number of characters and
    224       1.7       jtc 		 * it's a directory or a regular or linked file, just
    225       1.7       jtc 		 * stat the puppy.  We avoid testing for it not being
    226       1.7       jtc 		 * a special device in case someone adds a new type
    227       1.7       jtc 		 * of inode.
    228       1.7       jtc 		 */
    229      1.21      yamt 		else if (dobyte) {
    230      1.11       mrg 			if (fstat(fd, &sb)) {
    231      1.27     enami 				warn("%s", name);
    232       1.7       jtc 				rval = 1;
    233       1.7       jtc 			} else {
    234      1.12   mycroft 				if (S_ISREG(sb.st_mode) ||
    235      1.12   mycroft 				    S_ISLNK(sb.st_mode) ||
    236      1.12   mycroft 				    S_ISDIR(sb.st_mode)) {
    237      1.11       mrg 					charct = sb.st_size;
    238       1.9    andrew 				} else {
    239      1.23     enami 					while ((len =
    240      1.23     enami 					    read(fd, buf, MAXBSIZE)) > 0)
    241       1.9    andrew 						charct += len;
    242       1.1       cgd 				}
    243       1.1       cgd 			}
    244       1.1       cgd 		}
    245      1.23     enami 	} else {
    246       1.7       jtc 		/* do it the hard way... */
    247       1.8       jtc 		gotsp = 1;
    248       1.8       jtc 		while ((len = read(fd, buf, MAXBSIZE)) > 0) {
    249      1.21      yamt 			size_t wlen;
    250      1.21      yamt 
    251      1.23     enami 			r = do_mb(wbuf, (char *)buf, (size_t)len, &st, &wlen,
    252      1.27     enami 			    name);
    253      1.21      yamt 			if (dochar) {
    254      1.21      yamt 				charct += wlen;
    255      1.23     enami 			} else if (dobyte)
    256      1.21      yamt 				charct += len;
    257      1.21      yamt 			for (WC = wbuf; wlen--; ++WC) {
    258      1.21      yamt 				if (iswspace(*WC)) {
    259       1.7       jtc 					gotsp = 1;
    260      1.21      yamt 					if (*WC == L'\n') {
    261       1.7       jtc 						++linect;
    262       1.7       jtc 					}
    263       1.7       jtc 				} else {
    264       1.7       jtc 					/*
    265       1.7       jtc 					 * This line implements the POSIX
    266       1.7       jtc 					 * spec, i.e. a word is a "maximal
    267       1.7       jtc 					 * string of characters delimited by
    268       1.7       jtc 					 * whitespace."  Notice nothing was
    269       1.7       jtc 					 * said about a character being
    270       1.7       jtc 					 * printing or non-printing.
    271       1.7       jtc 					 */
    272       1.7       jtc 					if (gotsp) {
    273       1.7       jtc 						gotsp = 0;
    274       1.7       jtc 						++wordct;
    275       1.7       jtc 					}
    276       1.2       jtc 				}
    277       1.1       cgd 			}
    278       1.2       jtc 		}
    279      1.21      yamt 	}
    280      1.21      yamt 
    281      1.21      yamt 	if (len == -1) {
    282      1.27     enami 		warn("%s", name);
    283      1.21      yamt 		rval = 1;
    284      1.21      yamt 	}
    285      1.21      yamt 	if (dochar && r == (size_t)-2) {
    286      1.27     enami 		warnx("%s: incomplete multibyte character", name);
    287      1.21      yamt 		rval = 1;
    288       1.1       cgd 	}
    289       1.7       jtc 
    290      1.27     enami 	print_counts(linect, wordct, charct, file);
    291       1.8       jtc 
    292      1.23     enami 	/*
    293      1.23     enami 	 * don't bother checkint doline, doword, or dobyte --- speeds
    294      1.23     enami 	 * up the common case
    295      1.23     enami 	 */
    296       1.8       jtc 	tlinect += linect;
    297       1.8       jtc 	twordct += wordct;
    298       1.8       jtc 	tcharct += charct;
    299       1.8       jtc 
    300       1.8       jtc 	if (close(fd)) {
    301      1.27     enami 		warn("%s", name);
    302       1.8       jtc 		rval = 1;
    303       1.1       cgd 	}
    304       1.8       jtc }
    305       1.8       jtc 
    306      1.11       mrg static void
    307  1.31.6.1       jym print_counts(wc_count_t lines, wc_count_t words, wc_count_t chars,
    308  1.31.6.1       jym     const char *name)
    309       1.8       jtc {
    310       1.8       jtc 
    311       1.8       jtc 	if (doline)
    312      1.17  christos 		printf(WCFMT, (WCCAST)lines);
    313       1.8       jtc 	if (doword)
    314      1.17  christos 		printf(WCFMT, (WCCAST)words);
    315      1.21      yamt 	if (dobyte || dochar)
    316      1.17  christos 		printf(WCFMT, (WCCAST)chars);
    317       1.7       jtc 
    318      1.19   mycroft 	if (name)
    319      1.19   mycroft 		printf(" %s\n", name);
    320      1.19   mycroft 	else
    321      1.19   mycroft 		printf("\n");
    322      1.11       mrg }
    323      1.11       mrg 
    324      1.11       mrg static void
    325      1.30     perry usage(void)
    326      1.11       mrg {
    327      1.23     enami 
    328      1.28       wiz 	(void)fprintf(stderr, "usage: wc [-c | -m] [-lw] [file ...]\n");
    329      1.11       mrg 	exit(1);
    330       1.1       cgd }
    331