home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 September / Simtel20_Sept92.cdr / msdos / turbo_c / tc130.arc / WC.C < prev    next >
Text File  |  1987-08-12  |  4KB  |  148 lines

  1. /*
  2. **                WORD COUNT UTILITY
  3. **
  4. ** syntax:
  5. ** wc [-cwlsv] file
  6. ** -c count only characters
  7. ** -w count only words
  8. ** -l count only lines
  9. ** -s gives only a hex checksum
  10. ** -q silences reporting of filenames
  11. ** file is input file (Do NOT use stdin redirection!)
  12. ** options must be first argument only
  13. ** wildcard filenames are NOT allowed, but multiple filenames are O.K.
  14. **
  15. ** ========== COPYRIGHT 1987 BY STEVEN E. MARGISON ==============
  16. ** 8-12-86 A  Turbo-C
  17. **
  18. **   As distributed, this program requires (for compilation):
  19. **     "Steve's Turbo-C Library" version 1.30 or later
  20. **   which may be obtained without registration from many Bulletin
  21. **   Board Systems including:
  22. **      Compuserve IBMSW
  23. **      Cul-De-Sac (Holliston, MA.)
  24. **      GEnie
  25. **   and software library houses including:
  26. **      Public (Software) Library (Houston, TX.)
  27. **
  28. **   or by registration:
  29. **      $10 for Docs, Small Model Library
  30. **      $25 for Docs, C, S, M, L, H libraries, and complete library source
  31. **              in C and Assembler
  32. **     Steven E. Margison
  33. **     124 Sixth Street
  34. **     Downers Grove, IL, 60515
  35. **
  36. **
  37. */
  38.  
  39. #include <stdio.h>
  40. #include <ctype.h>
  41. #include <smdefs.h>
  42.  
  43. int binflg, copt, wopt, lopt, sopt, allopt, verbose;
  44. char path[64], fname[MAXFN], dest[64 + MAXFN + 1];
  45. FILE *fd;
  46.  
  47. main(argc, argv)
  48. int argc;
  49. char *argv[];
  50. {
  51.    char *cksum, *nc, *nl, *nw;
  52.    int index, c, inword, argci;
  53.  
  54.    if(argc < 2) usage();
  55.    verbose = YES;
  56.    sopt = lopt = wopt = copt = FALSE;
  57.    allopt=TRUE;
  58.    argci = 1;
  59.  
  60.    if(argv[argci][0] is '-') {
  61.       index = 0;
  62.       while(argv[argci][++index] isnot NULL) {
  63.          switch(tolower(argv[argci][index])) {
  64.             case 'q':
  65.                verbose = NO;
  66.                break;
  67.             case 'c': 
  68.                copt = TRUE;
  69.                allopt = FALSE;
  70.                break;
  71.             case 'w':
  72.                wopt = TRUE;
  73.                allopt = FALSE;
  74.                break;
  75.             case 'l':
  76.                lopt = TRUE;
  77.                allopt = FALSE;
  78.                break;
  79.             case 's':
  80.                sopt = TRUE;
  81.                allopt = FALSE;
  82.                break;
  83.             default:
  84.                usage();
  85.          } /* end of switch */
  86.       } /* end of while */
  87.    argci++;    /* get around options */
  88.    } /* end of option "if" */
  89.  
  90.    while(argci < argc) {
  91.       cksum = nc = nl = nw = 0;
  92.       inword = NO;
  93.       if(argv[argci][0] is '-') usage();
  94.  
  95.       do_open(argv[argci++]);
  96.  
  97.       while((c = fgetc(fd)) isnot EOF) {
  98.          cksum += c;
  99.          if(binflg) continue;
  100.          if(c is '\n') {
  101.             ++nl;
  102.             ++nc;
  103.             ++nc;     /* because a newline is actually two characters */
  104.             inword = NO;
  105.          }
  106.          else ++nc;
  107.          if(isspace(c)) inword = NO;
  108.          else if(inword is NO) {
  109.             inword = YES;
  110.             ++nw;
  111.             }
  112.          }  /* end of inner while */
  113.       if(allopt and !binflg)
  114.          printf("%6d characters  %d words  %d lines  %6x checksum\n",
  115.             nc, nw, nl, cksum);
  116.       if(binflg) printf("%x checksum\n", cksum);
  117.  
  118.       if(copt) printf("%d\n", nc);
  119.       if(wopt) printf("%d\n", nw);
  120.       if(lopt) printf("%d\n", nl);
  121.       if(sopt) printf("%x\n", cksum);
  122.       fclose(fd);
  123.    } /* end of file loop */
  124. }
  125.  
  126. usage() {
  127. fputs("WC Version 1.43 8-12-87  Copyright 1987 S.E. Margison\n", stderr);
  128. error("usage: wc [-cwlsq] <file1, file2, filen>");
  129. }
  130.  
  131. do_open(string) char *string; {
  132.    binflg = NO;
  133.    if(exttyp(string, "OBJ") is YES) { binflg = YES; goto AA; }
  134.    if(exttyp(string, "EXE") is YES) { binflg = YES; goto AA; }
  135.    if(exttyp(string, "COM") is YES) { binflg = YES; goto AA; }
  136.    if(exttyp(string, "ARC") is YES) { binflg = YES; goto AA; }
  137.    if(exttyp(string, "LIB") is YES) binflg = YES;
  138.    AA:
  139.    if(!binflg) {
  140.       if((fd = fopen(string, "r")) is NULL) cant(string);
  141.       }
  142.    else {
  143.       if((fd = fopen(string, "rb")) is NULL) cant(string);
  144.       }
  145.    if(verbose) printf("File: %s\n", string);
  146.    }
  147.  
  148.