USG_PG3/usr/source/cmd6/wc.c

Compare this file to the similar file:
Show the results in this format:

/* wc line and word count */

int	buf[259];
long wordct;
long twordct;
long linect;
long tlinect;
long charct;
long tcharct;

main(argc,argv)
char **argv;
{
	int i, token, wd;
	register char *p1, *p2;
	register int c;

	wd = 0;
	if(argc > 1 && *argv[1] == '-') {
		wd = 1;
		argc--;
		argv++;
	}

	i = 1;
	do {
		if(argc<=1) buf[0] = 0;
		else if(fopen(argv[i],buf)<0) {
			diag(argv[i]);
			diag(": cannot open\n");
			continue;
		}
		p1 = 0;
		p2 = 0;
		linect = 0;
		wordct = 0;
		charct = 0;
		token = 0;
		for(;;) {
			if(p1 >= p2) {
				p1 = &buf[1];
				c = read(buf[0], p1, 512);
				if(c <= 0)
					break;
				charct =+ c;
				p2 = p1+c;
			}
			c = 0;
			c =| *p1++;
			if(' '<c&&c<0177) {
				if(!token++)
					wordct++;
			} else {
				if(c=='\n')
					linect++;
				else if(c!=' '&&c!='\t')
					continue;
				token = 0;
			}
		}
		if(!wd)
			printf("%7ld ", linect);
		printf("%7ld", wordct);
		if(!wd)
			printf(" %7ld %s\n", charct, argc<=1?"":argv[i]);
		else
			printf("\n");
		close(buf[0]);
		tlinect =+ linect;
		twordct =+ wordct;
		tcharct =+ charct;
	} while(++i<argc);
	if(argc > 2) {
		if(!wd)
			printf("%7ld ",tlinect);
		printf("%7ld ",twordct);
		if(!wd)
			printf("%7ld total\n", tcharct);
		else
			printf("\n");
	}
}

diag(s)
char *s;
{
	while(*s)
		write(2,s++,1);
}