home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 189_01 / wc.c < prev    next >
Text File  |  1985-08-21  |  3KB  |  145 lines

  1. /* wc.c:    word count */
  2.  
  3. #include <stdio.h>
  4. #include <ctype.h>
  5.  
  6. #define yes 1
  7. #define no 0
  8. #define BUFSIZE 8192
  9.  
  10. char buf[BUFSIZE];
  11.  
  12. /* ..set default options.. */
  13. int bytes = no,
  14.     words = no,
  15.     lines = no,
  16.     files = no;
  17.  
  18. long tkc, tkl, tkw;
  19.  
  20. main(argc,argv)
  21. int argc;
  22. char *argv[];
  23. {
  24.     char *s;
  25.     int fd;
  26.  
  27.     /* ..process switches.. */
  28.     while (--argc > 0 && (**++argv == '-' || **argv == '/'))
  29.     {
  30.         for (s = *argv+1; *s; ++s)
  31.             switch(tolower(*s))
  32.             {
  33.                 case 'l':
  34.                     lines = !lines;
  35.                     break;
  36.                 case 'w':
  37.                     words = !words;
  38.                     break;
  39.                 case 'c':
  40.                     bytes = !bytes;
  41.                     break;
  42.                 case 'f':
  43.                     files = !files;
  44.                     break;
  45.                 default :
  46.                     fprintf(stderr,"---> ERROR: bad option: -%c\n",*s);
  47.                     exit();
  48.             }
  49.     }
  50.  
  51.     /* ..set all three options by default.. */
  52.     if (!(bytes || words || lines))
  53.         bytes = words = lines = yes;
  54.  
  55.     /* ..initialize totals.. */
  56.     tkc = tkl = tkw = 0;
  57.  
  58.     /* ..process files.. */
  59.     if (argc == 0)
  60.         wc(0,"");
  61.     else
  62.     {
  63.         /* ..check file switch.. */
  64.         if (argc > 1)
  65.             files = !files;
  66.  
  67.         for (; *argv; ++argv)
  68.             if ((fd = open(*argv,0)) >= 0)
  69.             {
  70.                 wc(fd,*argv);
  71.                 close(fd);
  72.             }
  73.             else
  74.                 fprintf(stderr,"---> ERROR: can't open %s\n",*argv);
  75.     }
  76.  
  77.     /* ..print totals.. */
  78.     if (files)
  79.     {
  80.         if (bytes)
  81.             printf("-------- ");
  82.         if (words) 
  83.             printf("------- ");
  84.         if (lines) 
  85.             printf("------ ");
  86.         putchar('\n');
  87.         if (bytes)
  88.             printf("%8D ",tkc);
  89.         if (words) 
  90.             printf("%7D ",tkw);
  91.         if (lines) 
  92.             printf("%6D ",tkl);
  93.         putchar('\n');
  94.     }
  95. }
  96.  
  97. wc(fd,f)
  98. int fd;
  99. char *f;
  100. {
  101.     long kc, kl, kw;
  102.     int inword, count;
  103.     register i;
  104.  
  105.     kc = kw = kl = 0L;
  106.     inword = no;
  107.  
  108.     while ((count = read(fd,buf,BUFSIZE)) > 0)
  109.     {
  110.         kc += count;
  111.         inword = no;
  112.         for (i = 0; i < count; ++i)
  113.             if (isspace(buf[i]))
  114.             {
  115.                 inword = no;
  116.                 if (buf[i] == '\n')
  117.                     ++kl;
  118.             }
  119.             else if (!inword)
  120.             {
  121.                 inword = yes;
  122.                 kw++;
  123.             }
  124.     }
  125.     if (bytes)
  126.         printf("%8D ",kc);
  127.     if (words) 
  128.         printf("%7D ",kw);
  129.     if (lines) 
  130.         printf("%6D ",kl);
  131.     if (files) 
  132.         puts(f);
  133.     else
  134.         putchar('\n');
  135.  
  136.     /* ..accumulate totals.. */
  137.     tkc += kc;
  138.     tkw += kw;
  139.     tkl += kl;
  140. }
  141.       inword = no;
  142.                 if (buf[i] == '\n')
  143.                     ++kl;
  144.             }
  145.             else i