/* ** WC.C ** Written by Leor Zolman, 3/16/82 ** ** Modified for Small-C by F.A.Scacchitti ** 9/23/84 ** ** ** Text analysis utility. Given a list of text files, WC prints ** out total number of characters, words and lines in each file ** and in all files together. "Words", here, are simply delimited ** by blanks, tabs or newlines. ** ** Maximum number of words and lines are each 32767, but the char ** count is virtually unlimited. ** */ #include extern printf(),exit(),fopen(),fclose(),strlen(),fgetc(); extern putchar(),isspace(); int ibuf, lototchars, hitotchars, totwords, totlines; main(argc,argv) int argc, argv[]; { if(argc == 1) { printf("\n\nUsage: wc . . . \n\n"); exit(); } else printf("\n\t\tchars\twords\tlines\n\n"); while (--argc) dofile(*++argv); /* process the files */ printf("\nTotals:"); /* print the results */ if (hitotchars) printf("\t\t%d%04d",hitotchars,lototchars); else printf("\t\t%d",lototchars); printf("\t%d\t%d\n",totwords,totlines); } dofile(name) char *name; { char inword; int c; int lotch, hitch, twords, tlines; ibuf = (fopen(name,"r")); if (ibuf == NULL) { printf("Can't open %s\n",name); return; } printf("%s:\t",name); if (strlen(name) < 7) putchar('\t'); inword = lotch = hitch = twords = tlines = 0; while ((c =fgetc(ibuf)) != EOF ) { if (++lotch == 10000) { lotch = 0; hitch++; } if (isspace(c)) { if (inword) { inword = 0; twords++; } } else if (!inword) inword = 1; if (c == '\n') tlines++; } if (hitch) printf("%d%04d",hitch,lotch); else printf("%d",lotch); printf("\t%d\t%d\n",twords,tlines); if ((lototchars += lotch) >= 10000) { lototchars -= 10000; hitotchars++; } hitotchars += hitch; totwords += twords; totlines += tlines; fclose(ibuf); }