wc/historical/unix-v7-wc.c

87 lines
1.3 KiB
C
Raw Normal View History

2023-09-11 14:34:53 +00:00
/* wc line and word count */
#include <stdio.h>
main(argc, argv)
char **argv;
{
int i, token;
register FILE *fp;
long linect, wordct, charct;
long tlinect=0, twordct=0, tcharct=0;
char *wd;
register int c;
wd = "lwc";
if(argc > 1 && *argv[1] == '-') {
wd = ++argv[1];
argc--;
argv++;
}
i = 1;
fp = stdin;
do {
if(argc>1 && (fp=fopen(argv[i], "r")) == NULL) {
fprintf(stderr, "wc: can't open %s\n", argv[i]);
continue;
}
linect = 0;
wordct = 0;
charct = 0;
token = 0;
for(;;) {
c = getc(fp);
if (c == EOF)
break;
charct++;
if(' '<c&&c<0177) {
if(!token) {
wordct++;
token++;
}
continue;
}
if(c=='\n')
linect++;
else if(c!=' '&&c!='\t')
continue;
token = 0;
}
/* print lines, words, chars */
wcp(wd, charct, wordct, linect);
if(argc>1) {
printf(" %s\n", argv[i]);
} else
printf("\n");
fclose(fp);
tlinect += linect;
twordct += wordct;
tcharct += charct;
} while(++i<argc);
if(argc > 2) {
wcp(wd, tcharct, twordct, tlinect);
printf(" total\n");
}
exit(0);
}
wcp(wd, charct, wordct, linect)
register char *wd;
long charct; long wordct; long linect;
{
while (*wd) switch (*wd++) {
case 'l':
printf("%7ld", linect);
break;
case 'w':
printf("%7ld ", wordct);
break;
case 'c':
printf("%7ld", charct);
break;
}
}