home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / OS2MNX1.ZIP / WC.C < prev    next >
C/C++ Source or Header  |  1989-12-27  |  5KB  |  217 lines

  1. /*
  2.  * wc - count lines, words and characters    Author: David
  3.  * Messer 
  4.  */
  5. /* $Header: d:/rcs/D:/RCS/RCS/wc.c 1.1 89/12/27 03:07:23 RCA Exp $
  6.  * $Log:    wc.c $
  7.  * Revision 1.1  89/12/27  03:07:23  RCA
  8.  * Initial revision
  9.  * 
  10.  */
  11.  
  12. #include "stdio.h"
  13. #define isdigit(c) (c >= '0' && c <= '9')
  14. #define isspace(c) (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r')
  15.  
  16. /* 
  17.  * Usage:  wc [-lwc] [names] 
  18.  *
  19.  * Flags: l - count lines. w - count words. c - count characters. 
  20.  *
  21.  * Flags l, w, and c are default. Words are delimited by any
  22.  * non-alphabetic character. 
  23.  *
  24.  * Released into the PUBLIC-DOMAIN 02/10/86 
  25.  *
  26.  * If you find this program to be of use to you, a donation of
  27.  * whatever you think it is worth will be cheerfully accepted. 
  28.  *
  29.  * Written by: David L. Messer P.O. Box 19130, Mpls, MN,  55119
  30.  * Program (heavily) modified by Andy Tanenbaum */
  31.  
  32.  
  33. int     lflag;        /* Count lines */
  34. int     wflag;        /* Count words */
  35. int     cflag;        /* Count characters */
  36.  
  37. long    lcount;        /* Count of lines */
  38. long    wcount;        /* Count of words */
  39. long    ccount;        /* Count of characters */
  40.  
  41. long    ltotal;        /* Total count of lines */
  42. long    wtotal;        /* Total count of words */
  43. long    ctotal;        /* Total count of characters */
  44. main(argc, argv)
  45.    int     argc;
  46.    char   *argv[];
  47. {
  48. int     k;
  49. char   *cp;
  50. int     tflag, files;
  51. int     i;
  52.  
  53.     if (argc == 1)
  54.       {
  55.       printf ("\n █▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█");
  56.       printf ("\n █ WC       (Count words, lines, chars)    $Author: RCA $   █");
  57.       printf ("\n █          $Date: 89/12/27 03:07:23 $     $Revision: 1.1 $ █");
  58.       printf ("\n █ Usage:   WC [-lwc] filename(s)                           █");
  59.       printf ("\n █ Note:    Words are delimited by any non-alphanumeric     █");
  60.       printf ("\n █          character.                                      █");
  61.       printf ("\n █ Options: -l: Print number of lines.                      █");
  62.       printf ("\n █          -w: Print number of words.                      █");
  63.       printf ("\n █          -c: Print number of characters.                 █");
  64.       printf ("\n █          [ -lwc is the default ]                         █");
  65.       printf ("\n █ OS:      Works under OS/2 or DOS.                        █");
  66.       printf ("\n █ Credits: David Messer, Andy Tannenbaum, K&R.             █");
  67.       printf ("\n █▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄█\n");
  68.       exit(1);
  69.       }
  70.  
  71. /* Get flags. */
  72.    files = argc - 1;
  73.    k = 1;
  74.    cp = argv[1];
  75.    if (argc > 1 && *cp++ == '-')
  76.    {
  77.       files--;
  78.       k++;            /* points to first file */
  79.       while (*cp != 0)
  80.       {
  81.      switch (*cp)
  82.      {
  83.      case 'l':
  84.         lflag++;
  85.         break;
  86.      case 'w':
  87.         wflag++;
  88.         break;
  89.      case 'c':
  90.         cflag++;
  91.         break;
  92.      default:
  93.         usage();
  94.      }
  95.      cp++;
  96.       }
  97.    }
  98. /* If no flags are set, treat as wc -lwc. */
  99.    if (!lflag && !wflag && !cflag)
  100.    {
  101.       lflag = 1;
  102.       wflag = 1;
  103.       cflag = 1;
  104.    }
  105. /* Process files. */
  106.    tflag = files >= 2;        /* set if # files > 1 */
  107.  
  108. /* Check to see if input comes from std input. */
  109.    if (k >= argc)
  110.    {
  111.       count();
  112.    /*
  113.     * if(lflag) printf(" %6D", lcount); if(wflag) printf(" %6D",
  114.     * wcount); if(cflag) printf(" %6D", ccount); zxzxzx (edit
  115.     * tag) 
  116.     */
  117.       if (lflag)
  118.      printf(" %6d", lcount);
  119.       if (wflag)
  120.      printf(" %6d", wcount);
  121.       if (cflag)
  122.      printf(" %6d", ccount);
  123.       printf(" \n");
  124.       fflush(stdout);
  125.       exit(0);
  126.    }
  127. /* There is an explicit list of files.  Loop on files. */
  128.    while (k < argc)
  129.    {
  130.       fclose(stdin);
  131.       if (fopen(argv[k], "r") == NULL)
  132.       {
  133.       /*
  134.        * std_err("wc: cannot open "); std_err(argv[k]);
  135.        * std_err("\n"); 
  136.        */
  137.      printf("wc: cannot open ");
  138.      printf(argv[k]);
  139.      printf("\n");
  140.      k++;
  141.      continue;
  142.       } else
  143.       {
  144.       /* Next file has been opened as std input. */
  145.      count();
  146.       /*
  147.        * if(lflag) printf(" %6D", lcount); if(wflag) printf("
  148.        * %6D", wcount); if(cflag) printf(" %6D", ccount);
  149.        * printf(" %s\n", argv[k]); 
  150.        */
  151.      if (lflag)
  152.         printf(" %6d", lcount);
  153.      if (wflag)
  154.         printf(" %6d", wcount);
  155.      if (cflag)
  156.         printf(" %6d", ccount);
  157.      printf(" %s\n", argv[k]);
  158.       }
  159.       k++;
  160.    }
  161.  
  162.    if (tflag)
  163.    {
  164.    /*
  165.     * if(lflag) printf(" %6D", ltotal); if(wflag) printf(" %6D",
  166.     * wtotal); if(cflag) printf(" %6D", ctotal); printf("
  167.     * total\n"); 
  168.     */
  169.       if (lflag)
  170.      printf(" %6d", ltotal);
  171.       if (wflag)
  172.      printf(" %6d", wtotal);
  173.       if (cflag)
  174.      printf(" %6d", ctotal);
  175.       printf(" total\n");
  176.    }
  177.    fflush(stdout);
  178.    exit(0);
  179. }
  180. count()
  181. {
  182. register int c;
  183. register int word = 0;
  184.    lcount = 0;
  185.    wcount = 0;
  186.    ccount = 0L;
  187.  
  188.    while ((c = getc(stdin)) > 0)
  189.    {
  190.       ccount++;
  191.  
  192.       if (isspace(c))
  193.       {
  194.      if (word)
  195.         wcount++;
  196.      word = 0;
  197.       } else
  198.       {
  199.      word = 1;
  200.       }
  201.  
  202.       if (c == '\n' || c == '\f')
  203.      lcount++;
  204.    }
  205.    ltotal += lcount;
  206.    wtotal += wcount;
  207.    ctotal += ccount;
  208. }
  209. usage()
  210. {
  211. /*
  212.  * std_err("Usage: wc [-lwc] [name ...]\n"); 
  213.  */
  214.    printf("Usage: wc [-lwc] [name ...]\n");
  215.    exit(1);
  216. }
  217.