Home | History | Annotate | Line # | Download | only in wc
wc.c revision 1.16
      1  1.16    mjacob /*	$NetBSD: wc.c,v 1.16 1999/02/14 18:03:18 mjacob 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.16    mjacob __RCSID("$NetBSD: wc.c,v 1.16 1999/02/14 18:03:18 mjacob 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.11       mrg #include <sys/stat.h>
     54  1.15  explorer #include <sys/types.h>
     55  1.11       mrg 
     56  1.11       mrg #include <fcntl.h>
     57  1.11       mrg #include <unistd.h>
     58  1.11       mrg #include <errno.h>
     59   1.1       cgd #include <stdio.h>
     60  1.11       mrg 
     61   1.2       jtc #include <stdlib.h>
     62   1.2       jtc #include <string.h>
     63   1.7       jtc #include <locale.h>
     64   1.2       jtc #include <ctype.h>
     65   1.2       jtc #include <errno.h>
     66   1.7       jtc #include <sys/param.h>
     67   1.7       jtc #include <sys/stat.h>
     68   1.7       jtc #include <sys/file.h>
     69   1.5       jtc #include <unistd.h>
     70   1.7       jtc #include <err.h>
     71   1.1       cgd 
     72  1.15  explorer static u_int64_t	tlinect, twordct, tcharct;
     73  1.14  wsanchez static int		doline, doword, dochar;
     74  1.14  wsanchez static int 		rval = 0;
     75  1.16    mjacob static char *qfmt;
     76   1.1       cgd 
     77  1.11       mrg static void	cnt __P((char *));
     78  1.15  explorer static void	print_counts __P((u_int64_t, u_int64_t, u_int64_t, char *));
     79  1.11       mrg static void	usage __P((void));
     80  1.13       mrg int	main __P((int, char *[]));
     81  1.11       mrg 
     82   1.5       jtc int
     83   1.1       cgd main(argc, argv)
     84   1.1       cgd 	int argc;
     85  1.11       mrg 	char *argv[];
     86   1.1       cgd {
     87  1.13       mrg 	int ch;
     88   1.1       cgd 
     89   1.7       jtc 	setlocale(LC_ALL, "");
     90  1.16    mjacob 	if (sizeof (u_int64_t) > 4)
     91  1.16    mjacob 		qfmt = " %7u";
     92  1.16    mjacob 	else
     93  1.16    mjacob 		qfmt = " %7qu";
     94   1.7       jtc 
     95   1.6       jtc 	while ((ch = getopt(argc, argv, "lwcm")) != -1)
     96   1.2       jtc 		switch((char)ch) {
     97   1.2       jtc 		case 'l':
     98   1.2       jtc 			doline = 1;
     99   1.2       jtc 			break;
    100   1.2       jtc 		case 'w':
    101   1.2       jtc 			doword = 1;
    102   1.2       jtc 			break;
    103   1.2       jtc 		case 'c':
    104   1.4       jtc 		case 'm':
    105   1.2       jtc 			dochar = 1;
    106   1.2       jtc 			break;
    107   1.2       jtc 		case '?':
    108   1.2       jtc 		default:
    109  1.11       mrg 			usage();
    110   1.2       jtc 		}
    111   1.2       jtc 	argv += optind;
    112   1.2       jtc 	argc -= optind;
    113   1.2       jtc 
    114  1.11       mrg 	/* Wc's flags are on by default. */
    115  1.11       mrg 	if (doline + doword + dochar == 0)
    116   1.1       cgd 		doline = doword = dochar = 1;
    117   1.1       cgd 
    118   1.1       cgd 	if (!*argv) {
    119  1.11       mrg 		cnt(NULL);
    120   1.2       jtc 	} else {
    121   1.2       jtc 		int dototal = (argc > 1);
    122   1.2       jtc 
    123   1.2       jtc 		do {
    124   1.2       jtc 			cnt(*argv);
    125   1.2       jtc 		} while(*++argv);
    126   1.2       jtc 
    127   1.2       jtc 		if (dototal) {
    128  1.11       mrg 			print_counts(tlinect, twordct, tcharct, "total");
    129   1.2       jtc 		}
    130   1.1       cgd 	}
    131   1.2       jtc 
    132   1.6       jtc 	exit(rval);
    133   1.1       cgd }
    134   1.1       cgd 
    135   1.5       jtc static void
    136   1.1       cgd cnt(file)
    137   1.1       cgd 	char *file;
    138   1.1       cgd {
    139  1.13       mrg 	u_char *C;
    140  1.13       mrg 	short gotsp;
    141  1.13       mrg 	int len;
    142  1.15  explorer 	u_int64_t linect, wordct, charct;
    143  1.11       mrg 	struct stat sb;
    144   1.1       cgd 	int fd;
    145   1.1       cgd 	u_char buf[MAXBSIZE];
    146   1.1       cgd 
    147   1.1       cgd 	linect = wordct = charct = 0;
    148   1.1       cgd 	if (file) {
    149   1.1       cgd 		if ((fd = open(file, O_RDONLY, 0)) < 0) {
    150  1.11       mrg 			warn("%s", file);
    151   1.6       jtc 			rval = 1;
    152   1.6       jtc 			return;
    153   1.1       cgd 		}
    154   1.7       jtc 	} else  {
    155   1.7       jtc 		fd = STDIN_FILENO;
    156   1.7       jtc 	}
    157   1.7       jtc 
    158   1.7       jtc 	if (!doword) {
    159   1.7       jtc 		/*
    160   1.7       jtc 		 * line counting is split out because it's a lot
    161   1.7       jtc 		 * faster to get lines than to get words, since
    162   1.7       jtc 		 * the word count requires some logic.
    163   1.7       jtc 		 */
    164   1.7       jtc 		if (doline) {
    165  1.11       mrg 			while ((len = read(fd, buf, MAXBSIZE)) > 0) {
    166   1.7       jtc 				charct += len;
    167   1.7       jtc 				for (C = buf; len--; ++C)
    168   1.7       jtc 					if (*C == '\n')
    169   1.7       jtc 						++linect;
    170   1.7       jtc 			}
    171   1.7       jtc 			if (len == -1) {
    172   1.7       jtc 				warn ("%s", file);
    173   1.7       jtc 				rval = 1;
    174   1.1       cgd 			}
    175   1.7       jtc 		}
    176   1.1       cgd 
    177   1.7       jtc 		/*
    178   1.7       jtc 		 * if all we need is the number of characters and
    179   1.7       jtc 		 * it's a directory or a regular or linked file, just
    180   1.7       jtc 		 * stat the puppy.  We avoid testing for it not being
    181   1.7       jtc 		 * a special device in case someone adds a new type
    182   1.7       jtc 		 * of inode.
    183   1.7       jtc 		 */
    184   1.7       jtc 		else if (dochar) {
    185  1.11       mrg 			if (fstat(fd, &sb)) {
    186  1.11       mrg 				warn("%s", file);
    187   1.7       jtc 				rval = 1;
    188   1.7       jtc 			} else {
    189  1.12   mycroft 				if (S_ISREG(sb.st_mode) ||
    190  1.12   mycroft 				    S_ISLNK(sb.st_mode) ||
    191  1.12   mycroft 				    S_ISDIR(sb.st_mode)) {
    192  1.11       mrg 					charct = sb.st_size;
    193   1.9    andrew 				} else {
    194  1.11       mrg 					while ((len = read(fd, buf, MAXBSIZE)) > 0)
    195   1.9    andrew 						charct += len;
    196   1.9    andrew 					if (len == -1) {
    197   1.9    andrew 						warn ("%s", file);
    198   1.9    andrew 						rval = 1;
    199   1.9    andrew 					}
    200   1.1       cgd 				}
    201   1.1       cgd 			}
    202   1.1       cgd 		}
    203   1.1       cgd 	}
    204   1.1       cgd 	else
    205   1.7       jtc 	{
    206   1.7       jtc 		/* do it the hard way... */
    207   1.8       jtc 		gotsp = 1;
    208   1.8       jtc 		while ((len = read(fd, buf, MAXBSIZE)) > 0) {
    209   1.7       jtc 			charct += len;
    210   1.7       jtc 			for (C = buf; len--; ++C) {
    211  1.11       mrg 				if (isspace(*C)) {
    212   1.7       jtc 					gotsp = 1;
    213   1.7       jtc 					if (*C == '\n') {
    214   1.7       jtc 						++linect;
    215   1.7       jtc 					}
    216   1.7       jtc 				} else {
    217   1.7       jtc 					/*
    218   1.7       jtc 					 * This line implements the POSIX
    219   1.7       jtc 					 * spec, i.e. a word is a "maximal
    220   1.7       jtc 					 * string of characters delimited by
    221   1.7       jtc 					 * whitespace."  Notice nothing was
    222   1.7       jtc 					 * said about a character being
    223   1.7       jtc 					 * printing or non-printing.
    224   1.7       jtc 					 */
    225   1.7       jtc 					if (gotsp) {
    226   1.7       jtc 						gotsp = 0;
    227   1.7       jtc 						++wordct;
    228   1.7       jtc 					}
    229   1.2       jtc 				}
    230   1.1       cgd 			}
    231   1.2       jtc 		}
    232   1.8       jtc 		if (len == -1) {
    233  1.11       mrg 			warn("%s", file);
    234   1.8       jtc 			rval = 1;
    235   1.8       jtc 		}
    236   1.1       cgd 	}
    237   1.7       jtc 
    238  1.11       mrg 	print_counts(linect, wordct, charct, file ? file : "");
    239   1.8       jtc 
    240   1.8       jtc 	/* don't bother checkint doline, doword, or dochar --- speeds
    241   1.8       jtc            up the common case */
    242   1.8       jtc 	tlinect += linect;
    243   1.8       jtc 	twordct += wordct;
    244   1.8       jtc 	tcharct += charct;
    245   1.8       jtc 
    246   1.8       jtc 	if (close(fd)) {
    247   1.8       jtc 		warn ("%s", file);
    248   1.8       jtc 		rval = 1;
    249   1.1       cgd 	}
    250   1.8       jtc }
    251   1.8       jtc 
    252  1.11       mrg static void
    253  1.11       mrg print_counts(lines, words, chars, name)
    254  1.15  explorer 	u_int64_t lines;
    255  1.15  explorer 	u_int64_t words;
    256  1.15  explorer 	u_int64_t chars;
    257   1.8       jtc 	char *name;
    258   1.8       jtc {
    259   1.8       jtc 
    260   1.8       jtc 	if (doline)
    261  1.16    mjacob 		printf(qfmt, lines);
    262   1.8       jtc 	if (doword)
    263  1.16    mjacob 		printf(qfmt, words);
    264   1.8       jtc 	if (dochar)
    265  1.16    mjacob 		printf(qfmt, chars);
    266   1.7       jtc 
    267  1.11       mrg 	printf(" %s\n", name);
    268  1.11       mrg }
    269  1.11       mrg 
    270  1.11       mrg static void
    271  1.11       mrg usage()
    272  1.11       mrg {
    273  1.11       mrg 	(void)fprintf(stderr, "usage: wc [-clw] [files]\n");
    274  1.11       mrg 	exit(1);
    275   1.1       cgd }
    276