Home | History | Annotate | Line # | Download | only in wc
wc.c revision 1.1.1.2
      1      1.1  cgd /*
      2  1.1.1.2  mrg  * Copyright (c) 1980, 1987, 1991, 1993
      3  1.1.1.2  mrg  *	The Regents of the University of California.  All rights reserved.
      4      1.1  cgd  *
      5      1.1  cgd  * Redistribution and use in source and binary forms, with or without
      6      1.1  cgd  * modification, are permitted provided that the following conditions
      7      1.1  cgd  * are met:
      8      1.1  cgd  * 1. Redistributions of source code must retain the above copyright
      9      1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     10      1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11      1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     12      1.1  cgd  *    documentation and/or other materials provided with the distribution.
     13      1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     14      1.1  cgd  *    must display the following acknowledgement:
     15      1.1  cgd  *	This product includes software developed by the University of
     16      1.1  cgd  *	California, Berkeley and its contributors.
     17      1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     18      1.1  cgd  *    may be used to endorse or promote products derived from this software
     19      1.1  cgd  *    without specific prior written permission.
     20      1.1  cgd  *
     21      1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22      1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23      1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24      1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25      1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26      1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27      1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28      1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29      1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30      1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31      1.1  cgd  * SUCH DAMAGE.
     32      1.1  cgd  */
     33      1.1  cgd 
     34      1.1  cgd #ifndef lint
     35  1.1.1.2  mrg static char copyright[] =
     36  1.1.1.2  mrg "@(#) Copyright (c) 1980, 1987, 1991, 1993\n\
     37  1.1.1.2  mrg 	The Regents of the University of California.  All rights reserved.\n";
     38      1.1  cgd #endif /* not lint */
     39      1.1  cgd 
     40      1.1  cgd #ifndef lint
     41  1.1.1.2  mrg static char sccsid[] = "@(#)wc.c	8.2 (Berkeley) 5/2/95";
     42      1.1  cgd #endif /* not lint */
     43      1.1  cgd 
     44      1.1  cgd #include <sys/param.h>
     45      1.1  cgd #include <sys/stat.h>
     46  1.1.1.2  mrg #include <fcntl.h>
     47  1.1.1.2  mrg #include <unistd.h>
     48  1.1.1.2  mrg #include <errno.h>
     49      1.1  cgd #include <stdio.h>
     50  1.1.1.2  mrg #include <stdlib.h>
     51  1.1.1.2  mrg #include <string.h>
     52  1.1.1.2  mrg #include <ctype.h>
     53  1.1.1.2  mrg 
     54  1.1.1.2  mrg u_long tlinect, twordct, tcharct;
     55  1.1.1.2  mrg int doline, doword, dochar;
     56  1.1.1.2  mrg 
     57  1.1.1.2  mrg void cnt __P((char *));
     58  1.1.1.2  mrg void err __P((const char *, ...));
     59  1.1.1.2  mrg void usage __P((void));
     60      1.1  cgd 
     61  1.1.1.2  mrg int
     62      1.1  cgd main(argc, argv)
     63      1.1  cgd 	int argc;
     64  1.1.1.2  mrg 	char *argv[];
     65      1.1  cgd {
     66      1.1  cgd 	register int ch;
     67      1.1  cgd 	int total;
     68      1.1  cgd 
     69  1.1.1.2  mrg 	while ((ch = getopt(argc, argv, "lwc")) != EOF)
     70  1.1.1.2  mrg 		switch((char)ch) {
     71  1.1.1.2  mrg 		case 'l':
     72  1.1.1.2  mrg 			doline = 1;
     73  1.1.1.2  mrg 			break;
     74  1.1.1.2  mrg 		case 'w':
     75  1.1.1.2  mrg 			doword = 1;
     76  1.1.1.2  mrg 			break;
     77  1.1.1.2  mrg 		case 'c':
     78  1.1.1.2  mrg 			dochar = 1;
     79  1.1.1.2  mrg 			break;
     80  1.1.1.2  mrg 		case '?':
     81  1.1.1.2  mrg 		default:
     82  1.1.1.2  mrg 			usage();
     83  1.1.1.2  mrg 		}
     84  1.1.1.2  mrg 	argv += optind;
     85  1.1.1.2  mrg 	argc -= optind;
     86  1.1.1.2  mrg 
     87  1.1.1.2  mrg 	/* Wc's flags are on by default. */
     88  1.1.1.2  mrg 	if (doline + doword + dochar == 0)
     89      1.1  cgd 		doline = doword = dochar = 1;
     90      1.1  cgd 
     91      1.1  cgd 	total = 0;
     92      1.1  cgd 	if (!*argv) {
     93  1.1.1.2  mrg 		cnt(NULL);
     94  1.1.1.2  mrg 		(void)printf("\n");
     95      1.1  cgd 	}
     96      1.1  cgd 	else do {
     97      1.1  cgd 		cnt(*argv);
     98  1.1.1.2  mrg 		(void)printf(" %s\n", *argv);
     99      1.1  cgd 		++total;
    100      1.1  cgd 	} while(*++argv);
    101      1.1  cgd 
    102      1.1  cgd 	if (total > 1) {
    103      1.1  cgd 		if (doline)
    104  1.1.1.2  mrg 			(void)printf(" %7ld", tlinect);
    105      1.1  cgd 		if (doword)
    106  1.1.1.2  mrg 			(void)printf(" %7ld", twordct);
    107      1.1  cgd 		if (dochar)
    108  1.1.1.2  mrg 			(void)printf(" %7ld", tcharct);
    109  1.1.1.2  mrg 		(void)printf(" total\n");
    110      1.1  cgd 	}
    111      1.1  cgd 	exit(0);
    112      1.1  cgd }
    113      1.1  cgd 
    114  1.1.1.2  mrg void
    115      1.1  cgd cnt(file)
    116      1.1  cgd 	char *file;
    117      1.1  cgd {
    118  1.1.1.2  mrg 	register u_char *p;
    119      1.1  cgd 	register short gotsp;
    120  1.1.1.2  mrg 	register int ch, len;
    121  1.1.1.2  mrg 	register u_long linect, wordct, charct;
    122  1.1.1.2  mrg 	struct stat sb;
    123      1.1  cgd 	int fd;
    124      1.1  cgd 	u_char buf[MAXBSIZE];
    125      1.1  cgd 
    126  1.1.1.2  mrg 	fd = STDIN_FILENO;
    127      1.1  cgd 	linect = wordct = charct = 0;
    128      1.1  cgd 	if (file) {
    129  1.1.1.2  mrg 		if ((fd = open(file, O_RDONLY, 0)) < 0)
    130  1.1.1.2  mrg 			err("%s: %s", file, strerror(errno));
    131  1.1.1.2  mrg 		if (doword)
    132  1.1.1.2  mrg 			goto word;
    133  1.1.1.2  mrg 		/*
    134  1.1.1.2  mrg 		 * Line counting is split out because it's a lot faster to get
    135  1.1.1.2  mrg 		 * lines than to get words, since the word count requires some
    136  1.1.1.2  mrg 		 * logic.
    137  1.1.1.2  mrg 		 */
    138  1.1.1.2  mrg 		if (doline) {
    139  1.1.1.2  mrg 			while (len = read(fd, buf, MAXBSIZE)) {
    140  1.1.1.2  mrg 				if (len == -1)
    141  1.1.1.2  mrg 					err("%s: %s", file, strerror(errno));
    142  1.1.1.2  mrg 				charct += len;
    143  1.1.1.2  mrg 				for (p = buf; len--; ++p)
    144  1.1.1.2  mrg 					if (*p == '\n')
    145  1.1.1.2  mrg 						++linect;
    146      1.1  cgd 			}
    147  1.1.1.2  mrg 			tlinect += linect;
    148  1.1.1.2  mrg 			(void)printf(" %7lu", linect);
    149      1.1  cgd 			if (dochar) {
    150  1.1.1.2  mrg 				tcharct += charct;
    151  1.1.1.2  mrg 				(void)printf(" %7lu", charct);
    152      1.1  cgd 			}
    153  1.1.1.2  mrg 			(void)close(fd);
    154  1.1.1.2  mrg 			return;
    155      1.1  cgd 		}
    156  1.1.1.2  mrg 		/*
    157  1.1.1.2  mrg 		 * If all we need is the number of characters and it's a
    158  1.1.1.2  mrg 		 * regular or linked file, just stat the puppy.
    159  1.1.1.2  mrg 		 */
    160  1.1.1.2  mrg 		if (dochar) {
    161  1.1.1.2  mrg 			if (fstat(fd, &sb))
    162  1.1.1.2  mrg 				err("%s: %s", file, strerror(errno));
    163  1.1.1.2  mrg 			if (S_ISREG(sb.st_mode)) {
    164  1.1.1.2  mrg 				(void)printf(" %7qu", sb.st_size);
    165  1.1.1.2  mrg 				tcharct += sb.st_size;
    166  1.1.1.2  mrg 				(void)close(fd);
    167  1.1.1.2  mrg 				return;
    168  1.1.1.2  mrg 			}
    169      1.1  cgd 		}
    170  1.1.1.2  mrg 	}
    171  1.1.1.2  mrg 
    172  1.1.1.2  mrg 	/* Do it the hard way... */
    173  1.1.1.2  mrg word:	for (gotsp = 1; len = read(fd, buf, MAXBSIZE);) {
    174  1.1.1.2  mrg 		if (len == -1)
    175  1.1.1.2  mrg 			err("%s: %s", file, strerror(errno));
    176  1.1.1.2  mrg 		/*
    177  1.1.1.2  mrg 		 * This loses in the presence of multi-byte characters.
    178  1.1.1.2  mrg 		 * To do it right would require a function to return a
    179  1.1.1.2  mrg 		 * character while knowing how many bytes it consumed.
    180  1.1.1.2  mrg 		 */
    181      1.1  cgd 		charct += len;
    182  1.1.1.2  mrg 		for (p = buf; len--;) {
    183  1.1.1.2  mrg 			ch = *p++;
    184  1.1.1.2  mrg 			if (ch == '\n')
    185  1.1.1.2  mrg 				++linect;
    186  1.1.1.2  mrg 			if (isspace(ch))
    187  1.1.1.2  mrg 				gotsp = 1;
    188  1.1.1.2  mrg 			else if (gotsp) {
    189  1.1.1.2  mrg 				gotsp = 0;
    190  1.1.1.2  mrg 				++wordct;
    191      1.1  cgd 			}
    192  1.1.1.2  mrg 		}
    193      1.1  cgd 	}
    194      1.1  cgd 	if (doline) {
    195      1.1  cgd 		tlinect += linect;
    196  1.1.1.2  mrg 		(void)printf(" %7lu", linect);
    197      1.1  cgd 	}
    198      1.1  cgd 	if (doword) {
    199      1.1  cgd 		twordct += wordct;
    200  1.1.1.2  mrg 		(void)printf(" %7lu", wordct);
    201      1.1  cgd 	}
    202      1.1  cgd 	if (dochar) {
    203      1.1  cgd 		tcharct += charct;
    204  1.1.1.2  mrg 		(void)printf(" %7lu", charct);
    205      1.1  cgd 	}
    206  1.1.1.2  mrg 	(void)close(fd);
    207  1.1.1.2  mrg }
    208  1.1.1.2  mrg 
    209  1.1.1.2  mrg void
    210  1.1.1.2  mrg usage()
    211  1.1.1.2  mrg {
    212  1.1.1.2  mrg 	(void)fprintf(stderr, "usage: wc [-clw] [files]\n");
    213  1.1.1.2  mrg 	exit(1);
    214  1.1.1.2  mrg }
    215  1.1.1.2  mrg 
    216  1.1.1.2  mrg #if __STDC__
    217  1.1.1.2  mrg #include <stdarg.h>
    218  1.1.1.2  mrg #else
    219  1.1.1.2  mrg #include <varargs.h>
    220  1.1.1.2  mrg #endif
    221  1.1.1.2  mrg 
    222  1.1.1.2  mrg void
    223  1.1.1.2  mrg #if __STDC__
    224  1.1.1.2  mrg err(const char *fmt, ...)
    225  1.1.1.2  mrg #else
    226  1.1.1.2  mrg err(fmt, va_alist)
    227  1.1.1.2  mrg 	char *fmt;
    228  1.1.1.2  mrg         va_dcl
    229  1.1.1.2  mrg #endif
    230  1.1.1.2  mrg {
    231  1.1.1.2  mrg 	va_list ap;
    232  1.1.1.2  mrg #if __STDC__
    233  1.1.1.2  mrg 	va_start(ap, fmt);
    234  1.1.1.2  mrg #else
    235  1.1.1.2  mrg 	va_start(ap);
    236  1.1.1.2  mrg #endif
    237  1.1.1.2  mrg 	(void)fprintf(stderr, "wc: ");
    238  1.1.1.2  mrg 	(void)vfprintf(stderr, fmt, ap);
    239  1.1.1.2  mrg 	va_end(ap);
    240  1.1.1.2  mrg 	(void)fprintf(stderr, "\n");
    241  1.1.1.2  mrg 	exit(1);
    242  1.1.1.2  mrg 	/* NOTREACHED */
    243      1.1  cgd }
    244