home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / TOOLS2.ZIP / WC.C < prev    next >
C/C++ Source or Header  |  1988-07-05  |  4KB  |  205 lines

  1. /**
  2.  
  3.     WC.C
  4.     
  5.     A UNIX-style Word Count utility.
  6.  
  7. **/
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <ctype.h>
  12. #include <process.h>
  13.  
  14. /**
  15.     Constants
  16. **/
  17. #define TRUE     1        /* Boolean Truth */
  18. #define FALSE    0        /* Boolean Falsehood */
  19. #define MAXLINE    2048        /* maximum line size */
  20.  
  21. /**
  22.     Statics
  23. **/
  24. static char szBuffer[MAXLINE] ;
  25. static unsigned long lChars ;    /* Number of characters */
  26. static unsigned long lWords ;    /* Number of words */
  27. static unsigned long lLines ;    /* Number of lines */
  28. static unsigned long lTChars ;    /* Total characters */
  29. static unsigned long lTWords ;    /* Total words */
  30. static unsigned long lTLines ;    /* Total lines */
  31. static int bChars ;        /* print character count */
  32. static int bWords ;        /* print word count */
  33. static int bLines ;        /* print line count */
  34. static int bTotals ;        /* print a total count */
  35.  
  36. /**
  37.     Function prototypes
  38. **/
  39. void main( int, char ** ) ;
  40. static void Count( char * ) ;
  41. static void PrintCount( long, long, long, char * ) ;
  42.  
  43. /**
  44.  
  45.     PrintCount()
  46.  
  47.     Print the count of words, characters and lines.
  48.  
  49. **/
  50.  
  51. static void PrintCount( lCCnt, lWCnt, lLCnt, szName ) 
  52. long lCCnt ;
  53. long lWCnt ;
  54. long lLCnt ;
  55. char *szName ;
  56. {
  57.     if ( bChars )        /* print character count only */
  58.         printf( "%ld %s\n", lCCnt, szName ) ;
  59.     else if ( bWords )    /* print word count only */
  60.         printf( "%ld %s\n", lWCnt, szName ) ;
  61.     else if ( bLines )    /* print line count only */
  62.         printf( "%ld %s\n", lLCnt, szName ) ;
  63.     else            /* print all three */
  64.         printf( "%ld\t%ld\t%ld %s\n", lLCnt, lWCnt, lCCnt, szName ) ;
  65. }
  66.  
  67. /**
  68.  
  69.     Count()
  70.     
  71.     Count number of words, characters and lines in a file.
  72.     Set lChars, lWords, and lLines accordingly.
  73.  
  74. **/
  75. static void Count( szName ) 
  76. char *szName ;
  77. {
  78.     FILE *pFile ;
  79.     int c ;
  80.     int bNonBlank = FALSE ;
  81.     char *szStd = "" ;
  82.     
  83.     lChars = 0L ;
  84.     lWords = 0L ;
  85.     lLines = 0L ;
  86.  
  87.     if ( szName )
  88.     {
  89.         pFile = fopen( szName, "rb" ) ;
  90.         if ( pFile == NULL )
  91.         {
  92.             printf( "ERROR: could not open file %s\n", szName ) ;
  93.             exit(1) ;
  94.         }
  95.     }
  96.     else
  97.     {
  98.         pFile = stdin ;        /* NULL name, read standard input */
  99.         szName = szStd ;
  100.     }
  101.     
  102.     while ( TRUE )
  103.     {
  104.         c = fgetc(pFile) ;
  105.  
  106.         if ( c == EOF )
  107.             break ;
  108.  
  109.         ++lChars ;
  110.             
  111.         if ( isspace(c) )
  112.         {
  113.             if ( bNonBlank )
  114.                 ++lWords ;
  115.             bNonBlank = FALSE ;
  116.         }
  117.         else
  118.             bNonBlank = TRUE ;
  119.  
  120.         if ( c == '\n' )
  121.             ++lLines ;
  122.     }
  123.  
  124.     /* close file if not stdin */
  125.     if ( pFile != stdin )
  126.         fclose( pFile ) ;
  127.  
  128.     /* print out the line count */
  129.     PrintCount( lChars, lWords, lLines, szName ) ;
  130.  
  131.     /* Add to the totals */
  132.     lTChars += lChars ;
  133.     lTWords += lWords ;
  134.     lTLines += lLines ;
  135. }
  136.  
  137. /**
  138.  
  139.     MAIN ROUTINE
  140.  
  141. **/
  142.  
  143. void main( argc, argv )
  144. int argc ;
  145. char *argv[] ;
  146. {
  147.     int nArg;
  148.  
  149.     /* Print a cute littel banner */
  150.     fprintf( stderr, "WC - A Word Count Utility\n" ) ;
  151.     fprintf( stderr, "By Ralph E. Brendler\n\n" ) ; 
  152.     
  153.     nArg = 1 ;
  154.     while ( nArg < argc )
  155.     {
  156.         if ( *argv[nArg] != '-' && *argv[nArg] != '/' )
  157.             break ;
  158.  
  159.         /* process command line switches */
  160.         switch( *(argv[nArg]+1) )
  161.         {
  162.         case 'c':    /* print character count */
  163.             bChars = TRUE ;
  164.             bWords = bLines = FALSE ;
  165.             break ;
  166.         case 'w':    /* print word count */
  167.             bWords = TRUE ;
  168.             bChars = bLines = FALSE ;
  169.             break ;
  170.         case 'l':    /* print line count */
  171.             bLines = TRUE ;
  172.             bChars = bWords = FALSE ;
  173.             break ;
  174.         case 't':    /* print totals */
  175.             bTotals = TRUE ;
  176.             break ;
  177.         case '?':    /* print usage */
  178.             printf( "Usage: wc [-cltw?] [file...]\n" ) ;
  179.             exit(0) ;
  180.             break ;
  181.         default:    /* bad switch */
  182.             printf( "ERROR: Bad switch %s\n", argv[nArg] ) ;
  183.             printf( "Type \"wc -?\" for help\n" ) ;
  184.             exit(1) ;
  185.             break ;
  186.         }
  187.  
  188.         ++nArg ;
  189.     }
  190.  
  191.     /* go through each file, counting stuff */
  192.     if ( nArg == argc )
  193.         Count( NULL ) ;        /* count stdin */
  194.     else
  195.         while ( nArg < argc )    /* count filename */
  196.             Count( argv[nArg++] ) ;
  197.  
  198.     /* Print out the totals */
  199.     if ( bTotals )
  200.         PrintCount( lTChars, lTWords, lTLines, "Total Count" ) ;
  201.  
  202.     exit(0) ;
  203. }
  204.  
  205.