home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0000 - 0009 / ibm0000-0009 / ibm0003.tar / ibm0003 / C_DISK5.ZIP / WC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-03-31  |  1.0 KB  |  52 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. main (argc, argv)
  5. int argc;
  6. char *argv[];
  7. {
  8.   int i;
  9.   unsigned long int w_total = 0, l_total = 0, c_total = 0;
  10.  
  11.   printf ("   lines   words   bytes file\n");
  12.   for (i = 1; i < argc; ++i)
  13.   {
  14.     FILE *input;
  15.     unsigned long int w_cnt, l_cnt, c_cnt;
  16.     int c, inword;
  17.  
  18.     if ((input = fopen(argv[i], "rb")) == NULL)
  19.     {
  20.       printf ("wc: cannot open %s\n", argv[i]);
  21.       continue;
  22.     }
  23.  
  24.     w_cnt = l_cnt = c_cnt = 0;
  25.     while ((c = fgetc(input)) != EOF)
  26.     {
  27.       if (c == '\n')
  28.         ++l_cnt;
  29.       if (!isspace(c))
  30.       {
  31.         if (!inword){
  32.           inword = 1;
  33.           ++w_cnt;
  34.         }
  35.       }
  36.       else
  37.         inword = 0;
  38.       ++c_cnt;
  39.     }
  40.     fclose (input);
  41.     printf ("%8lu%8lu%8lu %s\n", l_cnt, w_cnt, c_cnt, argv[i]);
  42.     l_total += l_cnt;
  43.     w_total += w_cnt;
  44.     c_total += c_cnt;
  45.   }
  46.   if (argc > 2)
  47.   {
  48.     printf ("--------------------------------------\n%8lu%8lu%8lu total",
  49.       l_total, w_total, c_total);
  50.   }
  51. }
  52.