home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / misc / smc203.ark / WC.C < prev    next >
Encoding:
Text File  |  1983-07-28  |  2.0 KB  |  105 lines

  1. /*
  2. **    WC.C
  3. **    Written by Leor Zolman, 3/16/82
  4. **
  5. **    Modified for Small-C             by F.A.Scacchitti
  6. **                            9/23/84
  7. **
  8. **
  9. **    Text analysis utility. Given a list of text files, WC prints
  10. **    out total number of characters, words and lines in each file
  11. **    and in all files together. "Words", here, are simply delimited
  12. **    by blanks, tabs or newlines.
  13. **
  14. **    Maximum number of words and lines are each 32767, but the char
  15. **    count is virtually unlimited.
  16. **
  17. */
  18.  
  19. #include <stdio80.h>
  20.  
  21. extern printf(),exit(),fopen(),fclose(),strlen(),fgetc();
  22. extern putchar(),isspace();
  23. int ibuf, lototchars, hitotchars, totwords, totlines;
  24.  
  25. main(argc,argv)
  26. int argc, argv[];
  27. {
  28.     if(argc == 1)
  29.         {
  30.         printf("\n\nUsage: wc <file1> <file2> . . . <fileN> <CR>\n\n");
  31.         exit();
  32.         }
  33.     else
  34.         printf("\n\t\tchars\twords\tlines\n\n");
  35.  
  36.     while (--argc) 
  37.  
  38.         dofile(*++argv);    /* process the files */
  39.  
  40.  
  41.     printf("\nTotals:");        /* print the results */
  42.  
  43.     if (hitotchars) printf("\t\t%d%04d",hitotchars,lototchars);
  44.  
  45.     else printf("\t\t%d",lototchars);
  46.  
  47.     printf("\t%d\t%d\n",totwords,totlines);
  48. }
  49.  
  50. dofile(name)
  51. char *name;
  52. {
  53.     char inword;
  54.     int c;
  55.     int  lotch, hitch, twords, tlines;
  56.  
  57.     ibuf = (fopen(name,"r"));
  58.     if (ibuf == NULL)
  59.         {
  60.         printf("Can't open %s\n",name);
  61.         return;
  62.         }
  63.     printf("%s:\t",name);
  64.     if (strlen(name) < 7)
  65.         putchar('\t');
  66.     
  67.     inword = lotch = hitch = twords = tlines = 0;
  68.  
  69.     while ((c =fgetc(ibuf)) != EOF ) {
  70.  
  71.         if (++lotch == 10000) {
  72.             lotch = 0;
  73.             hitch++;
  74.         }
  75.  
  76.         if (isspace(c)) {
  77.             if (inword) {
  78.                 inword = 0;
  79.                 twords++;
  80.             }
  81.         } else
  82.             if (!inword)
  83.                 inword = 1;
  84.         
  85.         if (c == '\n')
  86.             tlines++;
  87.     }
  88.  
  89.     if (hitch) printf("%d%04d",hitch,lotch);
  90.     else printf("%d",lotch);
  91.     printf("\t%d\t%d\n",twords,tlines);
  92.  
  93.     if ((lototchars += lotch) >= 10000) {
  94.         lototchars -= 10000;
  95.         hitotchars++;
  96.     }
  97.     hitotchars += hitch;
  98.     totwords += twords;
  99.     totlines += tlines;
  100.  
  101. fclose(ibuf);
  102. }
  103.  
  104.  
  105. aèCC31    C   b    !CC32    C   gë