xref: /csrg-svn/usr.bin/wc/wc.c (revision 21588)
1*21588Sdist /*
2*21588Sdist  * Copyright (c) 1980 Regents of the University of California.
3*21588Sdist  * All rights reserved.  The Berkeley software License Agreement
4*21588Sdist  * specifies the terms and conditions for redistribution.
5*21588Sdist  */
6*21588Sdist 
7*21588Sdist #ifndef lint
8*21588Sdist char copyright[] =
9*21588Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
10*21588Sdist  All rights reserved.\n";
11*21588Sdist #endif not lint
12*21588Sdist 
13*21588Sdist #ifndef lint
14*21588Sdist static char sccsid[] = "@(#)wc.c	5.1 (Berkeley) 05/31/85";
15*21588Sdist #endif not lint
16*21588Sdist 
171161Sbill /* wc line and word count */
181161Sbill 
191161Sbill #include <stdio.h>
201161Sbill long	linect, wordct, charct, pagect;
211161Sbill long	tlinect, twordct, tcharct, tpagect;
221161Sbill char	*wd = "lwc";
231161Sbill 
241161Sbill main(argc, argv)
251161Sbill char **argv;
261161Sbill {
271161Sbill 	int i, token;
281161Sbill 	register FILE *fp;
291161Sbill 	register int c;
301161Sbill 	char *p;
311161Sbill 
321161Sbill 	while (argc > 1 && *argv[1] == '-') {
331161Sbill 		switch (argv[1][1]) {
3412856Smo 		case 'l': case 'w': case 'c':
351161Sbill 			wd = argv[1]+1;
361161Sbill 			break;
371750Smark 		default:
381750Smark 		usage:
3912856Smo 			fprintf(stderr, "Usage: wc [-lwc] [files]\n");
401750Smark 			exit(1);
411161Sbill 		}
421161Sbill 		argc--;
431161Sbill 		argv++;
441161Sbill 	}
451161Sbill 
461161Sbill 	i = 1;
471161Sbill 	fp = stdin;
481161Sbill 	do {
491161Sbill 		if(argc>1 && (fp=fopen(argv[i], "r")) == NULL) {
5012088Smckusick 			perror(argv[i]);
511161Sbill 			continue;
521161Sbill 		}
531161Sbill 		linect = 0;
541161Sbill 		wordct = 0;
551161Sbill 		charct = 0;
561161Sbill 		token = 0;
571161Sbill 		for(;;) {
581161Sbill 			c = getc(fp);
591161Sbill 			if (c == EOF)
601161Sbill 				break;
611161Sbill 			charct++;
621161Sbill 			if(' '<c&&c<0177) {
631161Sbill 				if(!token) {
641161Sbill 					wordct++;
651161Sbill 					token++;
661161Sbill 				}
671161Sbill 				continue;
681161Sbill 			}
691161Sbill 			if(c=='\n') {
701161Sbill 				linect++;
711161Sbill 			}
721161Sbill 			else if(c!=' '&&c!='\t')
731161Sbill 				continue;
741161Sbill 			token = 0;
751161Sbill 		}
761161Sbill 		/* print lines, words, chars */
7712856Smo 		wcp(wd, charct, wordct, linect);
781161Sbill 		if(argc>1) {
791161Sbill 			printf(" %s\n", argv[i]);
801161Sbill 		} else
811161Sbill 			printf("\n");
821161Sbill 		fclose(fp);
831161Sbill 		tlinect += linect;
841161Sbill 		twordct += wordct;
851161Sbill 		tcharct += charct;
861161Sbill 	} while(++i<argc);
871161Sbill 	if(argc > 2) {
8812856Smo 		wcp(wd, tcharct, twordct, tlinect);
891161Sbill 		printf(" total\n");
901161Sbill 	}
911161Sbill 	exit(0);
921161Sbill }
931161Sbill 
9412856Smo wcp(wd, charct, wordct, linect)
951161Sbill register char *wd;
9612856Smo long charct; long wordct; long linect;
971161Sbill {
981161Sbill 	while (*wd) switch (*wd++) {
991161Sbill 	case 'l':
1001161Sbill 		ipr(linect);
1011161Sbill 		break;
1021161Sbill 
1031161Sbill 	case 'w':
1041161Sbill 		ipr(wordct);
1051161Sbill 		break;
1061161Sbill 
1071161Sbill 	case 'c':
1081161Sbill 		ipr(charct);
1091161Sbill 		break;
1101161Sbill 
1111161Sbill 	}
1121161Sbill }
1131161Sbill 
1141161Sbill ipr(num)
1151161Sbill long num;
1161161Sbill {
11712856Smo 	printf(" %7ld", num);
1181161Sbill }
1191161Sbill 
120