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