Home | History | Annotate | Line # | Download | only in wc
wc.c revision 1.5
      1  1.1      cgd /*
      2  1.1      cgd  * Copyright (c) 1980, 1987 Regents of the University of California.
      3  1.1      cgd  * 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      cgd char copyright[] =
     36  1.1      cgd "@(#) Copyright (c) 1980, 1987 Regents of the University of California.\n\
     37  1.1      cgd  All rights reserved.\n";
     38  1.1      cgd #endif /* not lint */
     39  1.1      cgd 
     40  1.1      cgd #ifndef lint
     41  1.3  mycroft /*static char sccsid[] = "from: @(#)wc.c	5.7 (Berkeley) 3/2/91";*/
     42  1.5      jtc static char rcsid[] = "$Id: wc.c,v 1.5 1993/08/27 22:31:06 jtc Exp $";
     43  1.1      cgd #endif /* not lint */
     44  1.1      cgd 
     45  1.1      cgd /* wc line, word and char count */
     46  1.1      cgd 
     47  1.1      cgd #include <sys/param.h>
     48  1.1      cgd #include <sys/stat.h>
     49  1.1      cgd #include <sys/file.h>
     50  1.1      cgd #include <stdio.h>
     51  1.2      jtc #include <stdlib.h>
     52  1.2      jtc #include <string.h>
     53  1.2      jtc #include <ctype.h>
     54  1.2      jtc #include <errno.h>
     55  1.5      jtc #include <unistd.h>
     56  1.1      cgd 
     57  1.1      cgd #define DEL	0177			/* del char */
     58  1.1      cgd #define NL	012			/* newline char */
     59  1.1      cgd #define SPACE	040			/* space char */
     60  1.1      cgd #define TAB	011			/* tab char */
     61  1.1      cgd 
     62  1.5      jtc static void	cnt();
     63  1.1      cgd static long	tlinect, twordct, tcharct;
     64  1.1      cgd static int	doline, doword, dochar;
     65  1.1      cgd 
     66  1.5      jtc int
     67  1.1      cgd main(argc, argv)
     68  1.1      cgd 	int argc;
     69  1.1      cgd 	char **argv;
     70  1.1      cgd {
     71  1.1      cgd 	extern int optind;
     72  1.1      cgd 	register int ch;
     73  1.1      cgd 
     74  1.4      jtc 	while ((ch = getopt(argc, argv, "lwcm")) != EOF)
     75  1.2      jtc 		switch((char)ch) {
     76  1.2      jtc 		case 'l':
     77  1.2      jtc 			doline = 1;
     78  1.2      jtc 			break;
     79  1.2      jtc 		case 'w':
     80  1.2      jtc 			doword = 1;
     81  1.2      jtc 			break;
     82  1.2      jtc 		case 'c':
     83  1.4      jtc 		case 'm':
     84  1.2      jtc 			dochar = 1;
     85  1.2      jtc 			break;
     86  1.2      jtc 		case '?':
     87  1.2      jtc 		default:
     88  1.2      jtc 			fputs("usage: wc [-lwc] [files]\n", stderr);
     89  1.2      jtc 			exit(1);
     90  1.2      jtc 		}
     91  1.2      jtc 	argv += optind;
     92  1.2      jtc 	argc -= optind;
     93  1.2      jtc 
     94  1.1      cgd 	/*
     95  1.1      cgd 	 * wc is unusual in that its flags are on by default, so,
     96  1.1      cgd 	 * if you don't get any arguments, you have to turn them
     97  1.1      cgd 	 * all on.
     98  1.1      cgd 	 */
     99  1.2      jtc 	if (!doline && !doword && !dochar) {
    100  1.1      cgd 		doline = doword = dochar = 1;
    101  1.1      cgd 	}
    102  1.1      cgd 
    103  1.1      cgd 	if (!*argv) {
    104  1.1      cgd 		cnt((char *)NULL);
    105  1.1      cgd 		putchar('\n');
    106  1.2      jtc 	} else {
    107  1.2      jtc 		int dototal = (argc > 1);
    108  1.2      jtc 
    109  1.2      jtc 		do {
    110  1.2      jtc 			cnt(*argv);
    111  1.2      jtc 			printf(" %s\n", *argv);
    112  1.2      jtc 		} while(*++argv);
    113  1.2      jtc 
    114  1.2      jtc 		if (dototal) {
    115  1.2      jtc 			if (doline)
    116  1.2      jtc 				printf(" %7ld", tlinect);
    117  1.2      jtc 			if (doword)
    118  1.2      jtc 				printf(" %7ld", twordct);
    119  1.2      jtc 			if (dochar)
    120  1.2      jtc 				printf(" %7ld", tcharct);
    121  1.2      jtc 			puts(" total");
    122  1.2      jtc 		}
    123  1.1      cgd 	}
    124  1.2      jtc 
    125  1.1      cgd 	exit(0);
    126  1.1      cgd }
    127  1.1      cgd 
    128  1.5      jtc static void
    129  1.1      cgd cnt(file)
    130  1.1      cgd 	char *file;
    131  1.1      cgd {
    132  1.1      cgd 	register u_char *C;
    133  1.1      cgd 	register short gotsp;
    134  1.1      cgd 	register int len;
    135  1.1      cgd 	register long linect, wordct, charct;
    136  1.1      cgd 	struct stat sbuf;
    137  1.1      cgd 	int fd;
    138  1.1      cgd 	u_char buf[MAXBSIZE];
    139  1.1      cgd 
    140  1.1      cgd 	linect = wordct = charct = 0;
    141  1.1      cgd 	if (file) {
    142  1.1      cgd 		if ((fd = open(file, O_RDONLY, 0)) < 0) {
    143  1.2      jtc 			fprintf (stderr, "wc: %s: %s\n", file, strerror(errno));
    144  1.1      cgd 			exit(1);
    145  1.1      cgd 		}
    146  1.1      cgd 		if (!doword) {
    147  1.1      cgd 			/*
    148  1.1      cgd 			 * line counting is split out because it's a lot
    149  1.1      cgd 			 * faster to get lines than to get words, since
    150  1.1      cgd 			 * the word count requires some logic.
    151  1.1      cgd 			 */
    152  1.1      cgd 			if (doline) {
    153  1.5      jtc 				while((len = read(fd, buf, MAXBSIZE)) != 0) {
    154  1.1      cgd 					if (len == -1) {
    155  1.2      jtc 						fprintf (stderr, "wc: %s: %s\n",
    156  1.2      jtc 							file, strerror(errno));
    157  1.1      cgd 						exit(1);
    158  1.1      cgd 					}
    159  1.1      cgd 					charct += len;
    160  1.1      cgd 					for (C = buf; len--; ++C)
    161  1.1      cgd 						if (*C == '\n')
    162  1.1      cgd 							++linect;
    163  1.1      cgd 				}
    164  1.1      cgd 				tlinect += linect;
    165  1.1      cgd 				printf(" %7ld", linect);
    166  1.1      cgd 				if (dochar) {
    167  1.1      cgd 					tcharct += charct;
    168  1.1      cgd 					printf(" %7ld", charct);
    169  1.1      cgd 				}
    170  1.1      cgd 				close(fd);
    171  1.1      cgd 				return;
    172  1.1      cgd 			}
    173  1.1      cgd 			/*
    174  1.1      cgd 			 * if all we need is the number of characters and
    175  1.1      cgd 			 * it's a directory or a regular or linked file, just
    176  1.1      cgd 			 * stat the puppy.  We avoid testing for it not being
    177  1.1      cgd 			 * a special device in case someone adds a new type
    178  1.1      cgd 			 * of inode.
    179  1.1      cgd 			 */
    180  1.1      cgd 			if (dochar) {
    181  1.1      cgd 				int ifmt;
    182  1.1      cgd 
    183  1.1      cgd 				if (fstat(fd, &sbuf)) {
    184  1.2      jtc 					fprintf (stderr, "wc: %s: %s\n",
    185  1.2      jtc 						file, strerror(errno));
    186  1.1      cgd 					exit(1);
    187  1.1      cgd 				}
    188  1.1      cgd 
    189  1.1      cgd 				ifmt = sbuf.st_mode & S_IFMT;
    190  1.1      cgd 				if (ifmt == S_IFREG || ifmt == S_IFLNK
    191  1.1      cgd 					|| ifmt == S_IFDIR) {
    192  1.1      cgd 					printf(" %7ld", sbuf.st_size);
    193  1.1      cgd 					tcharct += sbuf.st_size;
    194  1.1      cgd 					close(fd);
    195  1.1      cgd 					return;
    196  1.1      cgd 				}
    197  1.1      cgd 			}
    198  1.1      cgd 		}
    199  1.1      cgd 	}
    200  1.1      cgd 	else
    201  1.1      cgd 		fd = 0;
    202  1.1      cgd 	/* do it the hard way... */
    203  1.5      jtc 	for (gotsp = 1; (len = read(fd, buf, MAXBSIZE));) {
    204  1.1      cgd 		if (len == -1) {
    205  1.2      jtc 			fprintf (stderr, "wc: %s: %s\n", file, strerror(errno));
    206  1.1      cgd 			exit(1);
    207  1.1      cgd 		}
    208  1.1      cgd 		charct += len;
    209  1.2      jtc 		for (C = buf; len--; ++C) {
    210  1.2      jtc 			if (isspace (*C)) {
    211  1.2      jtc 				gotsp = 1;
    212  1.2      jtc 				if (*C == NL) {
    213  1.1      cgd 					++linect;
    214  1.2      jtc 				}
    215  1.2      jtc 			} else {
    216  1.2      jtc #if 0
    217  1.2      jtc 				/*
    218  1.2      jtc 				 * This line of code implements the
    219  1.2      jtc 				 * original V7 wc algorithm, i.e.
    220  1.2      jtc 				 * a non-printing character doesn't
    221  1.2      jtc 				 * toggle the "word" count, so that
    222  1.2      jtc 				 * "  ^D^F  " counts as 6 spaces,
    223  1.2      jtc 				 * while "foo^D^Fbar" counts as 8
    224  1.2      jtc 				 * characters.
    225  1.2      jtc 				 *
    226  1.2      jtc 				 * test order is important -- gotsp
    227  1.2      jtc 				 * will normally be NO, so test it
    228  1.2      jtc 				 * first
    229  1.2      jtc 				 */
    230  1.2      jtc 				if (gotsp && *C > SPACE && *C < DEL) ...
    231  1.1      cgd #endif
    232  1.2      jtc 				/*
    233  1.2      jtc 				 * This line implements the POSIX
    234  1.2      jtc 				 * spec, i.e. a word is a "maximal
    235  1.2      jtc 				 * string of characters delimited by
    236  1.2      jtc 				 * whitespace."  Notice nothing was
    237  1.2      jtc 				 * said about a character being
    238  1.2      jtc 				 * printing or non-printing.
    239  1.2      jtc 				 */
    240  1.2      jtc 				if (gotsp) {
    241  1.2      jtc 					gotsp = 0;
    242  1.2      jtc 					++wordct;
    243  1.2      jtc 				}
    244  1.1      cgd 			}
    245  1.2      jtc 		}
    246  1.1      cgd 	}
    247  1.1      cgd 	if (doline) {
    248  1.1      cgd 		tlinect += linect;
    249  1.1      cgd 		printf(" %7ld", linect);
    250  1.1      cgd 	}
    251  1.1      cgd 	if (doword) {
    252  1.1      cgd 		twordct += wordct;
    253  1.1      cgd 		printf(" %7ld", wordct);
    254  1.1      cgd 	}
    255  1.1      cgd 	if (dochar) {
    256  1.1      cgd 		tcharct += charct;
    257  1.1      cgd 		printf(" %7ld", charct);
    258  1.1      cgd 	}
    259  1.1      cgd 	close(fd);
    260  1.1      cgd }
    261