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 / BEEHIVE / ZCAT / WORK-VMN.LBR / WC.C < prev    next >
Text File  |  2000-06-30  |  4KB  |  197 lines

  1. /* wc.c -- text analysis utility -- written by leor zolman, 03/16/82.
  2.   'wc' prints out total number of characters, words, and lines in files
  3.   declared from the command line.  'words' are defined as groups of
  4.   characters delimited by blanks, tabs, or newlines.  maximum number of
  5.   words and lines are each 65535, but character count is virtually
  6.   unlimited.
  7.  
  8.         usage: >wc <fn1.ft1><cr>
  9.                >wc <fn1.ft1> [fn2.ft2] [etc.]<cr>
  10.                >wc <*.ft><cr>
  11.                >wc <fn.*><cr>
  12.                >wc <fn.*> [fn1.ft1] [etc.]<cr>
  13.  
  14. 09/15/83  modified to compile with c/80 from software toolworks.  fg    */
  15.  
  16. #include "tprintf.c"    /* from c/80 version 3.0 environment        */
  17.  
  18. #define EOF -1
  19. #define NULL 0
  20.  
  21. static int f1;
  22. static unsigned lo_totchars, hi_totchars, totwords, totlines;
  23.  
  24. main(argc,argv)
  25. int argc;
  26. char **argv;
  27.     {
  28.     command(&argc,&argv);
  29.     if (argc < 2)
  30.         {
  31.         printf("Usage: >wc <fn1.ft1> [fn2.ft2] [etc.]\n");
  32.         printf("       >wc <*.ft> [fn.*] [etc.]\n");
  33.         exit(1);
  34.         }
  35.     lo_totchars = hi_totchars = totwords = totlines = 0;
  36.     printf("\n\t\tChars\tWords\tLines\n");
  37.     while (--argc)
  38.         dofile(*++argv);
  39.     printf("Totals:");
  40.     if (hi_totchars)
  41.         printf("\t\t%d%04d",hi_totchars,lo_totchars);
  42.     else
  43.         printf("\t\t%d",lo_totchars);
  44.     printf("\t%d\t%d\n",totwords,totlines);
  45.     }
  46.  
  47. dofile(name)
  48. char *name;
  49.     {
  50.     char inword;
  51.     register int c;
  52.     static unsigned lo_tch, hi_tch, twords, tlines;
  53.  
  54.     if ((f1 = fopen(name, "r")) == NULL)
  55.                 {
  56.         printf("Can't open %s\n",name);
  57.         return;
  58.         }
  59.     printf("%s:\t",name);
  60.     if (strlen(name) < 7)
  61.         putchar('\t');
  62.     inword = lo_tch = hi_tch = twords = tlines = 0;
  63.     while ((c = getc(f1)) != EOF)
  64.         {
  65.         if (++lo_tch == 10000)
  66.             {
  67.             lo_tch = 0;
  68.             hi_tch++;
  69.             }
  70.         if (isspace(c))
  71.             {
  72.             if (inword)
  73.                 {
  74.                 inword = 0;
  75.                 twords++;
  76.                 }
  77.             }
  78.         else if (!inword)
  79.                  inword = 1;
  80.         if (c == '\n')
  81.             tlines++;
  82.         }
  83.     fclose(f1);
  84.     if (hi_tch)
  85.         printf("%d%04d",hi_tch,lo_tch);
  86.     else
  87.         printf("%d",lo_tch);
  88.     printf("\t%d\t%d\n",twords,tlines);
  89.     if ((lo_totchars += lo_tch) >= 10000)
  90.         {
  91.         lo_totchars -= 10000;
  92.         hi_totchars++;
  93.         }
  94.     hi_totchars += hi_tch;
  95.     totwords += twords;
  96.     totlines += tlines;
  97.     }
  98.  
  99. /* command -- expand wildcard filenames in the command line.
  100.    usage: 'command(&argc, &argv)' modifies argc and argv as necessary.
  101.    uses 'sbrk' to create new arg list.  also uses 'makfcb()' and 'bdos()'
  102.    from c/80 'stdlib.c' source file.  written by dr. jim gillogly;
  103.    modified for cp/m by walt bilofsky.    (7/25/83)            */
  104.  
  105. #define MAXFILES 255    /* max number of expanded files            */
  106. #define FNSIZE 15    /* filename length is 2(d:) + 8 + 1 + 3 + null    */
  107.  
  108. int COMnf, *COMfn, COMc, *COMv;
  109. char *COMarg, *COMs;
  110. static expand();
  111.  
  112. command(argcp, argvp)
  113. int *argcp, *argvp;
  114.     {
  115.     int f_alloc[MAXFILES];
  116.  
  117.     COMfn = f_alloc;
  118.     COMc = *argcp;
  119.     COMv = *argvp;
  120.     COMfn[0] = *COMv++;             /* don't expand first one */
  121.     COMnf = 1;
  122.  
  123.     for (COMarg = *COMv; --COMc; COMarg = *++COMv)
  124.         {
  125.         for (COMs = COMarg; *COMs; COMs++)
  126.             if (*COMs == '?' || *COMs == '*')
  127.                          {
  128.                 expand();
  129.                 goto contn;  /* expand each name once    */
  130.                 }
  131.             COMfn[COMnf++] = COMarg;  /* no expansion    */
  132. contn:            ;
  133.         }
  134.     *argcp = COMnf;
  135.     COMfn[COMnf++] = -1;
  136.     COMv = *argvp = sbrk(2 * COMnf);
  137.     while (COMnf--)
  138.         COMv[COMnf] = COMfn[COMnf];
  139.     }
  140.  
  141. static expand()
  142.     {
  143.     char fcb[36];
  144.     static char *p, *q;
  145.     static int i, flg;
  146.     makfcb(COMarg, fcb);
  147.     if (fcb[0] == -1)
  148.         fcb[0] = '?';            /* check for all users    */
  149.     for (i = flg = 1; i <= 11; ++i)        /* expand *'s to ?'s    */
  150.         {
  151.         if (i == 9)
  152.             flg = 1;
  153.         if (fcb[i] == '*')
  154.             flg = 0;
  155.         if (flg == 0)
  156.             fcb[i] = '?';
  157.         }
  158.     flg = 17;
  159.     bdos(26, 0x80);               /* make sure dma address ok    */
  160.     while ((i = bdos(flg, fcb)) != -1)
  161.         {
  162.         COMfn[COMnf++] = q = sbrk(FNSIZE);
  163.         if (COMnf >= MAXFILES-1)
  164.             {
  165.             for (p = "Too many file names.\n";
  166.                             putchar(*p++); )
  167.                                 ;
  168.             exit(0);
  169.             }
  170.         p = 0x81 + i * 32;    /* where to find directory field */
  171.         if (COMarg[1] == ':' && COMarg[0] != '?')
  172.             {
  173.             *q++ = COMarg[0];
  174.             *q++ = ':';
  175.             }
  176.         for (i = 12; --i; )
  177.             {
  178.             if (i == 3)
  179.                 *q++ = '.';
  180.             if ((*q = *p++ & 0177) != ' ')
  181.                 ++q;
  182.             }
  183.         *q = 0;
  184.         flg = 18;
  185.         }
  186.     }
  187.  
  188. #include "stdlib.c"    /* required here for c/80 environment        */    
  189. 7) != ' ')
  190.                 ++q;
  191.             }
  192.         *q = 0;
  193.         flg = 18;
  194.         }
  195.     }
  196.  
  197. #include "stdlib.c"    /* requir