/* * wordcount: count line, words, and characters in input */ #include #include #include "glob.h" #define BBSZ 20480 char bufbig[BBSZ]; char words=0; /* are we counting words? */ char chars=0; /* characters? */ char lines=0; /* and/or lines? */ long totwc=0; /* total # words across all the files */ long totlc=0; /* total # lines across all the files */ long totcc=0; /* total # characters across all the files */ long filec=0; /* total # files */ extern int getopt(); extern int opterr, optind; extern char *optarg; /* * wordcount() counts the interesting things in a single stream */ wordcount(fille, name) register FILE *fille; char *name; { register c; /* lastest character read */ register long cc=0; /* total # of characters */ register isw=0; /* are we inside a word? */ register long wc=0; /* total # of words */ register long lc=0; /* total # of lines */ /* big buffer for speed */ setvbuf(fille, bufbig, _IOFBF, BBSZ); while ((c=getc(fille)) != EOF) { cc++; if (c == '\n') lc++; if (isalnum(c) != isw) if (isw = isalnum(c)) wc++; } if (lines) printf("%9ld", lc); if (words) printf("%9ld", wc); if (chars) printf("%9ld", cc); if (name) printf(" %s", name); putchar('\n'); totlc += lc; totwc += wc; totcc += cc; filec ++; } /* wordcount */ /* * wordcount main: sets up options and loops through the filelist, calling * the actual wc procedure */ main(argc, argv) char **argv; { FILE *fd; int i,opt; opterr = 1; /* getopt, please complain for me */ while ((opt=getopt(argc, argv, "wcl")) != EOF) switch (opt) { case 'w': words=1; break; case 'c': chars=1; break; case 'l': lines=1; break; default: fprintf(stderr, "usage: wc [-wcl] [-] [file ...]\n"); exit(255); } /* if no options given, count everything */ if ((words|lines|chars) == 0) words = lines = chars = 1; optind--; expand_arglist(argc-optind, argv+optind, (struct args_t*)0); if (myargc == 0) wordcount(stdin, (char*)0); else for (i=0; i 1) { if (lines) printf("%9ld", totlc); if (words) printf("%9ld", totwc); if (chars) printf("%9ld", totcc); printf(" total\n"); } exit(0); } /* main */