home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 143_01 / lc.c < prev    next >
Text File  |  1985-11-14  |  2KB  |  112 lines

  1.  
  2. /*
  3. %CC1 $1.C -X -E5000
  4. %CLINK $1 DIO  -S
  5. %DELETE    $1.CRL 
  6. */
  7. /*********************************************************************
  8. *                LC                     *
  9. **********************************************************************
  10. *           COPYRIGHT 1983 EUGENE H. MALLORY             *
  11. *********************************************************************/
  12. #include "BDSCIO.H"
  13. #include "DIO.H"
  14.  
  15. main(argc,argv)
  16.  
  17. char **argv;
  18. int argc;
  19.  
  20. BEGIN  
  21.     int     c,nl,wc,cc,inword;
  22.     dioinit(&argc,argv);
  23.     
  24. /*********************************************************************
  25. ***          ARGUMENT PROCESSING                   ***
  26. *********************************************************************/
  27. #define    LC 0
  28. #define    WC 1
  29. #define    CC 2
  30.  
  31. int ii,jj,optionerr,option;
  32. char *ss;
  33.     option = optionerr = 0;
  34.  
  35.     for (ii=argc-1;ii>0;ii--)
  36.         if (argv[ii][0] == '-')
  37.         BEGIN
  38.             for    (ss = &argv[ii][1]; *ss    != '\0';)
  39.             BEGIN
  40.                 switch (toupper(*ss++))
  41.                 BEGIN
  42.                     case 'L':
  43.                     option = LC;
  44.                     break;
  45.                     case 'W':
  46.                     option = WC;
  47.                     break;
  48.                     case 'C':
  49.                     option = CC;
  50.                     break;
  51.                     case 'H':
  52.                     optionerr = TRUE;
  53.                     break;
  54.                     default:
  55.                     typef("LC: Illegal option %c.\n"
  56.                         ,*--ss);
  57.                     ss++;
  58.                     optionerr = TRUE;
  59.                     break;
  60.                 END
  61.                 while (isdigit(*ss)) ss++;
  62.             END
  63.             for    (jj=ii;jj<(argc-1);jj++) argv[jj] = argv[jj+1];
  64.             argc--;
  65.         END
  66.     
  67.  
  68.     if (optionerr) 
  69.         BEGIN
  70.         typef("LC:    Legal options are:\n");
  71.         typef("-L     Line count.\n");
  72.         typef("-W     Word count.\n");
  73.         typef("-C     Character    count.\n");
  74.         exit(0);
  75.         END    
  76.  
  77. /*********************************************************************
  78. ***             END OF ARGUMENT PROCESSING               ***
  79. *********************************************************************/
  80.     
  81.     inword = wc    = cc = nl = 0;
  82.     if (option == LC)
  83.     BEGIN
  84.     while ((c=getchar()) != EOF) if (c == '\n')    nl++;
  85.     printf("%d\n",nl);
  86.     END
  87.     else if (option == WC)
  88.     BEGIN
  89.     inword = FALSE;
  90.     wc = 0;
  91.     while ((c=getchar()) != EOF) 
  92.     BEGIN
  93.         if (!isalpha(c) && inword)
  94.         BEGIN
  95.             inword = FALSE;
  96.             wc++;
  97.         END
  98.         else if (isalpha(c))
  99.         BEGIN
  100.             inword = TRUE;
  101.         END
  102.     END
  103.     printf("%d\n",wc);
  104.     END
  105.     else if (option == CC)
  106.     BEGIN
  107.     while ((c=getchar()) != EOF) cc++;
  108.     printf("%d\n",cc);
  109.     END
  110.     dioflush();
  111. END  
  112.