TCU

A reimplementation of the BSD core utilities written in C.
Log | Files | Refs | README | LICENSE

wc.c (4368B)


      1 /* Terrible implementation of the wc core util */
      2 /* for licensing information please see included LICENSE file */
      3 
      4 /* The WC utility lists the number of lines, words, and bytes contained in each input file */
      5 /* A 'word' is defined as a string of characters delimited by white space characters. */
      6 
      7 /* TODOs for program to be considered "complete" */
      8 /* DONE: Enable multiple arguments at the same time (i.g. -lw for line and word count) */
      9 /* TODO: Enable accepting files from STDIN */
     10 /* DONE: Fix formatting to match original program */
     11 /* Flags that TCU accepts */
     12 /* HOLD: --libxo: Generate output via libxo in a selection of different human and machine readable formats.    */
     13 /*       I will need to actually figure out what this argument /does/ to try to figure out how to implement it */
     14 /* DONE: -L Write the length of the line containing the most bytes (default) or characters (when -m is provided) to standard output. */
     15 /*        When more than one file argument is specified, the longest input line of all files is reported as the value of the final "total". */
     16 /* DONE: -c The number of bytes in each input file is written to the standard output. This will cancel out an any prior usage of the -m option. */
     17 /* DONE: -l the number of lines in each input file is written to the standard output. */
     18 /* HOLD: -m The number of characters in each input file is written to the standard output. If the current locale does not support */
     19 /*       multi-byte characters, this is equivalent to the -c option. This will cancel out and prior usage of the -c option. */
     20 /* DONE: -w The number of words in each input file is written to the standard output. */
     21 
     22 #include<stdio.h>
     23 #include<stdlib.h>
     24 #include<string.h>
     25 
     26 /* Functions */
     27 
     28 int main(int argc, char *argv[]) {
     29   /* Prep stuff */
     30   FILE *fp;
     31   char ch;
     32   /* This value will represent the first file in argc */
     33   int firstFile = 0;
     34   /* Initilizing values for counters */
     35   int lValue = 0, wValue = 0, bValue = 0;
     36   /* Initializing values for totals */
     37   int lTotal = 0, wTotal = 0, bTotal = 0;
     38   /* This will be used to iterate through files */
     39   int i = 1;
     40 
     41   if (argc == 1) printf("Please enter filename\n");
     42 
     43 
     44   int countChars = strcmp(argv[i], "-c");
     45   if (countChars == 0) i++;
     46   int countLines = strcmp(argv[i], "-l");
     47   if (countLines == 0) i++;
     48   int countWords = strcmp(argv[i], "-w");
     49   if (countWords == 0) i++;
     50 
     51   /* This argument is handled differently than the others */
     52   int longestLine = strcmp(argv[i], "-L");
     53   if (longestLine == 0) {
     54     i++;
     55     int llLine = 0;
     56     for (i = i; i < argc; i++) {
     57       fp = fopen(argv[i], "r");
     58       int lLine = 0;
     59       bValue = 0;
     60       while((ch = fgetc(fp)) != EOF) {
     61         if (ch == '\n') {
     62           if (bValue > lLine) {
     63             lLine = bValue;
     64           }
     65           bValue = 0;
     66         } else {
     67           bValue++;
     68         }
     69       }
     70       fclose(fp);
     71       printf("%8d %s\n", lLine, argv[i]);
     72       if (lLine > llLine) llLine = lLine;
     73     }
     74     if (argc > 3) printf("%4d Total\n", llLine);
     75     return 0;
     76   }
     77 
     78   /* rough draft of basic idea */
     79   firstFile = i + 1;
     80   for (i = i; i < argc; i++) {
     81     lValue = 0;
     82     wValue = 0;
     83     bValue = 0;
     84     fp = fopen(argv[i], "r");
     85     while((ch = fgetc(fp)) != EOF) {
     86       if (ch == '\n') {
     87         lValue++;
     88         ch = fgetc(fp);
     89         if(ch == '\n') {
     90           lValue++;
     91           bValue++;
     92         } else {
     93           ungetc(ch, fp);
     94         }
     95       }
     96       if ((ch == ' ') || (ch == '\n')) {
     97         wValue++;
     98       }
     99       bValue++;
    100     }
    101     if ((countLines == 39) && (countWords == 39) && (countChars == 39)) {
    102       printf("%8d%8d%8d %s\n", lValue, wValue, bValue, argv[i]);
    103       lTotal = lTotal + lValue;
    104       wTotal = wTotal + wValue;
    105       bTotal = bTotal + bValue;
    106       fclose(fp);
    107     } else {
    108       if (countLines == 0) {
    109         printf("%8d", lValue);
    110         lTotal = lTotal + lValue;
    111       }
    112       if (countWords == 0) {
    113         printf("%8d", wValue);
    114         wTotal = wTotal + wValue;
    115       }
    116       if (countChars == 0) {
    117         printf("%8d", bValue);
    118         bTotal = bTotal + bValue;
    119       }
    120       printf(" %s\n", argv[i]);
    121       fclose(fp);
    122     }
    123   }
    124 
    125   if (firstFile != i) {
    126     if (countLines == 39) printf("%8d", lTotal);
    127     if (countLines == 39) printf("%8d", wTotal);
    128     if (countLines == 39) printf("%8d", bTotal);
    129     printf( " total\n");
    130   }
    131 
    132   return 0;
    133 }