home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / BDSC / BDSC-3 / WC.C < prev    next >
Text File  |  2000-06-30  |  2KB  |  88 lines

  1. /*
  2.     WC.C
  3.     Written by Leor Zolman, 3/16/82
  4.  
  5.     Text analysis utility. Given a list of text files, WC prints
  6.     out total number of characters, words and lines in each file
  7.     and in all files together. "Words", here, are simply delimited
  8.     by blanks, tabs or newlines.
  9.  
  10.     Maximum number of words and lines are each 65535, but the char
  11.     count is virtually unlimited.
  12.  
  13.     compile & link by:
  14.         cc1 wc.c -e2000 -o
  15.         {clink | l2}  wc wildexp
  16. */
  17.  
  18. #include "bdscio.h"
  19.  
  20. unsigned lo_totchars, hi_totchars, totwords, totlines;
  21. char ibuf[BUFSIZ];
  22.  
  23. main(argc,argv)
  24. char **argv;
  25. {
  26.     wildexp(&argc,&argv);
  27.     if (argc == 1) exit(puts("Usage: wc <list of files>\n"));
  28.  
  29.     lo_totchars = hi_totchars = totwords = totlines = 0;
  30.  
  31.     puts("\n\t\tchars\twords\tlines\n");
  32.     while (--argc) dofile(*++argv);
  33.     puts("\nTotals:");
  34.     if (hi_totchars) printf("\t\t%d%04d",hi_totchars,lo_totchars);
  35.     else printf("\t\t%d",lo_totchars);
  36.     printf("\t%u\t%u\n",totwords,totlines);
  37. }
  38.  
  39. dofile(name)
  40. char *name;
  41. {
  42.     char inword;
  43.     int c;
  44.     unsigned  lo_tch, hi_tch, twords, tlines;
  45.  
  46.     if (fopen(name,ibuf) == ERROR)
  47.         return printf("Can't open %s\n",name);
  48.  
  49.     printf("%s:\t",name);
  50.     if (strlen(name) < 7)
  51.         putchar('\t');
  52.     
  53.     inword = lo_tch = hi_tch = twords = tlines = 0;
  54.  
  55.     while ((c = getc(ibuf)) != EOF && c != CPMEOF) {
  56.  
  57.         if (++lo_tch == 10000) {
  58.             lo_tch = 0;
  59.             hi_tch++;
  60.         }
  61.  
  62.         if (isspace(c)) {
  63.             if (inword) {
  64.                 inword = 0;
  65.                 twords++;
  66.             }
  67.         } else
  68.             if (!inword)
  69.                 inword = 1;
  70.         
  71.         if (c == '\n')
  72.             tlines++;
  73.     }
  74.     fabort(ibuf->_fd);
  75.  
  76.     if (hi_tch) printf("%d%04d",hi_tch,lo_tch);
  77.     else printf("%d",lo_tch);
  78.     printf("\t%u\t%u\n",twords,tlines);
  79.  
  80.     if ((lo_totchars += lo_tch) >= 10000) {
  81.         lo_totchars -= 10000;
  82.         hi_totchars++;
  83.     }
  84.     hi_totchars += hi_tch;
  85.     totwords += twords;
  86.     totlines += tlines;
  87. }
  88.