TCU

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

commit df6822fea017355d8eb600d7b220877d9a71a778
parent c6ff66d0a1d91c421101cca611e44a012c1be2ee
Author: Tyler Clark <tyler.clark@foxide.xyz>
Date:   Sun,  9 Feb 2025 21:42:28 -0500

Working on improving wc

Diffstat:
Msrc/wc.c | 63+++++++++++++++++++++++++++++++++++++++------------------------
1 file changed, 39 insertions(+), 24 deletions(-)

diff --git a/src/wc.c b/src/wc.c @@ -4,14 +4,20 @@ /* The WC utility lists the number of lines, words, and bytes contained in each input file */ /* A 'word' is defined as a string of characters delimited by white space characters. */ +/* TODOs for program to be considered "complete" */ +/* DONE: Enable multiple arguments at the same time (i.g. -lw for line and word count) */ +/* TODO: Enable accepting files from STDIN */ +/* DONE: Fix formatting to match original program */ /* Flags that TCU accepts */ -/* HOLD: --libxo: Generate output via libxo in a selection of different human and machine readable formats. */ -/* DONE: -L Write the length of the line containing the most bytes (default) or characters (when -m is provided) to standard output. When more than one file argument is specified, the longest input line of all files is reported as the value of the final "total". */ +/* HOLD: --libxo: Generate output via libxo in a selection of different human and machine readable formats. */ +/* I will need to actually figure out what this argument /does/ to try to figure out how to implement it */ +/* DONE: -L Write the length of the line containing the most bytes (default) or characters (when -m is provided) to standard output. */ +/* When more than one file argument is specified, the longest input line of all files is reported as the value of the final "total". */ /* 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. */ /* DONE: -l the number of lines in each input file is written to the standard output. */ -/* HOLD: -m The number of characters in each input file is written to the standard output. If the current locale does not support multi-byte characters, this is equivalent to the -c option. This will cancel out and prior usage of the -c option. */ +/* HOLD: -m The number of characters in each input file is written to the standard output. If the current locale does not support */ +/* multi-byte characters, this is equivalent to the -c option. This will cancel out and prior usage of the -c option. */ /* DONE: -w The number of words in each input file is written to the standard output. */ -/* TODO: Enable multiple arguments at the same time (i.g. -lw for line and word count) */ #include<stdio.h> #include<stdlib.h> @@ -23,6 +29,8 @@ int main(int argc, char *argv[]) { /* Prep stuff */ FILE *fp; char ch; + /* This value will represent the first file in argc */ + int firstFile = 0; /* Initilizing values for counters */ int lValue = 0, wValue = 0, bValue = 0; /* Initializing values for totals */ @@ -30,6 +38,9 @@ int main(int argc, char *argv[]) { /* This will be used to iterate through files */ int i = 1; + if (argc == 1) printf("Please enter filename\n"); + + int countChars = strcmp(argv[i], "-c"); if (countChars == 0) i++; int countLines = strcmp(argv[i], "-l"); @@ -57,7 +68,7 @@ int main(int argc, char *argv[]) { } } fclose(fp); - printf("%4d%10s\n", lLine, argv[i]); + printf("%8d %s\n", lLine, argv[i]); if (lLine > llLine) llLine = lLine; } if (argc > 3) printf("%4d Total\n", llLine); @@ -65,6 +76,7 @@ int main(int argc, char *argv[]) { } /* rough draft of basic idea */ + firstFile = i + 1; for (i = i; i < argc; i++) { lValue = 0; wValue = 0; @@ -73,7 +85,6 @@ int main(int argc, char *argv[]) { while((ch = fgetc(fp)) != EOF) { if (ch == '\n') { lValue++; - wValue++; ch = fgetc(fp); if(ch == '\n') { lValue++; @@ -82,36 +93,40 @@ int main(int argc, char *argv[]) { ungetc(ch, fp); } } - if (ch == ' ') { + if ((ch == ' ') || (ch == '\n')) { wValue++; } bValue++; } - if (countLines == 0) { - printf("%4d%10s\n", lValue, argv[i]); + if ((countLines == 39) && (countWords == 39) && (countChars == 39)) { + printf("%8d%8d%8d %s\n", lValue, wValue, bValue, argv[i]); lTotal = lTotal + lValue; - fclose(fp); - } else if (countWords == 0) { - printf("%d%10s\n", wValue, argv[i]); wTotal = wTotal + wValue; - fclose(fp); - } else if (countChars == 0) { - printf("%d%10s\n", bValue, argv[i]); bTotal = bTotal + bValue; fclose(fp); } else { - printf("%4d%5d%5d%10s\n", lValue, wValue, bValue, argv[i]); - lTotal = lTotal + lValue; - wTotal = wTotal + wValue; - bTotal = bTotal + bValue; + if (countLines == 0) { + printf("%8d", lValue); + lTotal = lTotal + lValue; + } + if (countWords == 0) { + printf("%8d", wValue); + wTotal = wTotal + wValue; + } + if (countChars == 0) { + printf("%8d", bValue); + bTotal = bTotal + bValue; + } + printf(" %s\n", argv[i]); fclose(fp); } } - if (argc > 2) { - if (countLines == 0) printf("%4d Total\n", lTotal); - else if (countWords == 0) printf("%4d Total\n", wTotal); - else if (countChars == 0) printf("%4d Total\n", bTotal); - else printf("%4d%5d%5d Total\n", lTotal, wTotal, bTotal); + + if (firstFile != i) { + if (countLines == 39) printf("%8d", lTotal); + if (countLines == 39) printf("%8d", wTotal); + if (countLines == 39) printf("%8d", bTotal); + printf( " total\n"); } return 0;