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