home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / unix / du_wc.arc / WORDCNT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-10  |  3.1 KB  |  114 lines

  1. /**************************************************************************
  2.  *   WORDCNT.C  -- a very dumb word and line counter by
  3.  *        James W. Birdsall
  4.  *   version 1.0    3/13/89
  5.  *
  6.  *   usage: wordcnt filename
  7.  *
  8.  *   This program is a memory hog. In order to avoid thrashing the drive,
  9.  *   especially on large files, the program reads the entire file into
  10.  *   memory. The entire file must fit into memory -- there is no provision
  11.  *   made for partial reads. Maybe in the next version...
  12.  */
  13.  
  14.  
  15. #include <stdio.h>
  16. #include <alloc.h>
  17. #include <ctype.h>
  18. #include <dos.h>
  19.  
  20. #define INWORD 1
  21. #define INSPACE 0
  22. #define HOLDINGSIZ 32000
  23.  
  24.  
  25. main(int argc, char *argv[])
  26. {
  27.    FILE *infile;
  28.    char huge *temp, huge *temp3;
  29.    char holding[HOLDINGSIZ];
  30.    long words = 0;
  31.    long lines = 0;
  32.    long length, temp2;
  33.    int readtemp;
  34.    long readchar = 0;
  35.    short state;
  36.    short oldstate;
  37.  
  38.  
  39. /* check usage */
  40.    if (argc < 2) {
  41.       printf("Usage: wordcnt filename\n");
  42.       exit(3);
  43.       }
  44. /* open the file */
  45.    if (!(infile = fopen(argv[1],"rt"))) {
  46.       printf("Error opening file %s\n", argv[1]);
  47.       exit(3);
  48.       }
  49. /* the simple way of finding the size of the file */
  50. /* actually, this tends to overestimate the file size because text files */
  51. /* are saved using CR/LF pairs and are read into memory using only LF's  */
  52. /* so two bytes appear in the filesize for every newline and only one is */
  53. /* actually used */
  54.    if (fseek(infile, 0L, SEEK_END)) {
  55.       printf("Error scanning file\n");
  56.       exit(3);
  57.       }
  58.    if ((length = ftell(infile)) == -1) {
  59.       printf("Error scanning file\n");
  60.       exit(3);
  61.       }
  62. /* reset to the beginning of the file */
  63.    if (fseek(infile, 0L, SEEK_SET)) {
  64.       printf("Error scanning file\n");
  65.       exit(3);
  66.       }
  67. /* grab memory */
  68.    if (!(temp = (char huge *) farmalloc(length))) {
  69.       printf("File too big\n");
  70.       exit(3);
  71.       }
  72.    temp3 = temp;
  73. /* read into memory */
  74.    do {
  75. /* read into near memory */
  76.       if (!(readtemp = fread(holding, sizeof(char), HOLDINGSIZ, infile))) {
  77.          printf("Error reading file\n");
  78.          exit(3);
  79.          }
  80. /* move to far memory */
  81.       movedata(_DS, holding, FP_SEG(temp3), FP_OFF(temp3), readtemp);
  82.       temp3 += readtemp;
  83.       readchar += readtemp;
  84.    } while (readtemp == HOLDINGSIZ);
  85. /* close file */
  86.    if (fclose(infile)) {
  87.       printf("Error closing file\n");
  88.       exit(3);
  89.       }
  90. /* count words -- counter is incremented upon transition from word (alpha */
  91. /* and digits) to space (anything else) */
  92.    state = INSPACE;
  93.    oldstate = INSPACE;
  94.    temp3 = temp;
  95.    for (temp2 = 0; temp2 < readchar; temp2++, temp3++) {
  96.       if (isalnum(*temp3))
  97.          state = INWORD;
  98.         else {
  99.          if (*temp3 == '\n')
  100.             lines++;
  101.          state = INSPACE;
  102.          }
  103.       if (state == INSPACE && oldstate == INWORD)
  104.          words++;
  105.       oldstate = state;
  106.       }
  107. /* printout and exit */
  108.    printf("%ld words on %ld lines found in file %s\n", words, lines, argv[1]);
  109.    farfree(temp);
  110.    exit(0);
  111. }
  112.  
  113.  
  114.