home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / compiler / small_c / cb / sources / cnt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1985-08-11  |  1.3 KB  |  59 lines

  1.  
  2. /*
  3. ** cnt.c -- count characters, words, and/or lines
  4. **
  5. ** Copyright 1982 J. E. Hendrix.  All rights reserved.
  6. */
  7. #include <stdio.h>
  8. #include "tools.h"
  9. #define NOCCARGC
  10. char strc[6], strw[6], strl[6];
  11. main(argc, argv) int argc, *argv; {
  12.   char arg[MAXFN], *nc, *nl, *nw;
  13.   int c, f, i, fd, inword;
  14.   fd=stdin;
  15.   i=f=0;
  16.   while(getarg(++i, arg, MAXFN, argc, argv) != EOF) {
  17.     if(arg[0] != '-') {
  18.       if((fd = fopen(arg, "r")) == 0) cant(arg);
  19.       continue;
  20.       }
  21.     switch(f = tolower(arg[1])) {
  22.       case 'c': case 'w': case 'l': continue;
  23.       default:
  24.         fputs("usage: CNT [file] [-C|-W|-L]\n", stderr);
  25.         abort(7);
  26.       }
  27.     }
  28.   nc=nl=nw=0;
  29.   inword=NO;
  30.   while((c=fgetc(fd))!=EOF) {
  31.     poll(YES);
  32.     if(c=='\n') {
  33.       ++nl;
  34.       }
  35.     else ++nc;
  36.     if(isspace(c)) inword=NO;
  37.     else if(inword==NO) {
  38.       inword=YES;
  39.       ++nw;
  40.       }
  41.     }
  42.   itou(nc, strc, 6);
  43.   itou(nw, strw, 6);
  44.   itou(nl, strl, 6);
  45.   switch(f) {
  46.     case 'c': lout(strc, stdout); break;
  47.     case 'w': lout(strw, stdout); break;
  48.     case 'l': lout(strl, stdout); break;
  49.     default:
  50.       sout(strc, stdout); lout(" characters", stdout);
  51.       sout(strw, stdout); lout(" words", stdout);
  52.       sout(strl, stdout); lout(" lines", stdout);
  53.     }
  54.   fclose(stdout);
  55.   }
  56. #include "cant.c"
  57. #include "out.c"
  58.  
  59.