TCU

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

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

Completed -c, -l, and -w arguments

Diffstat:
Msrc/wc.c | 54+++++++++++++++++++++++++++++++++++++++++-------------
1 file changed, 41 insertions(+), 13 deletions(-)

diff --git a/src/wc.c b/src/wc.c @@ -5,17 +5,19 @@ /* A 'word' is defined as a string of characters delimited by white space characters. */ /* Flags that TCU accepts */ -/* TODO: --libxo: Generate output via libxo in a selection of different human and machine readable formats. */ +/* HOLD: --libxo: Generate output via libxo in a selection of different human and machine readable formats. */ /* TODO: -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". */ -/* TODO: -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. */ -/* TODO: -l the number of lines in each input file is written to the standard output. */ -/* TODO: -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. */ -/* TODO: -w The number of words in each input file is written to the standard output. */ +/* 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. */ +/* DONE: -w The number of words in each input file is written to the standard output. */ #include<stdio.h> #include<stdlib.h> #include<string.h> +/* Functions */ + int main(int argc, char *argv[]) { /* Prep stuff */ FILE *fp; @@ -27,10 +29,16 @@ int main(int argc, char *argv[]) { /* This will be used to iterate through files */ int i = 1; - int countLines = strcmp(argv[1], "-l"); + int countChars = strcmp(argv[i], "-c"); + if (countChars == 0) i++; + int countLines = strcmp(argv[i], "-l"); + if (countLines == 0) i++; + int countWords = strcmp(argv[i], "-w"); + if (countWords == 0) i++; + printf("i == %d\n", i); /* rough draft of basic idea */ - for (i = 1; i < argc; i++) { + for (i = i; i < argc; i++) { lValue = 0; wValue = 0; bValue = 0; @@ -52,12 +60,32 @@ int main(int argc, char *argv[]) { } bValue++; } - printf("%4d%5d%5d%10s\n", lValue, wValue, bValue, argv[i]); - lTotal = lTotal + lValue; - wTotal = wTotal + wValue; - bTotal = bTotal + bValue; - fclose(fp); + if (countLines == 0) { + printf("%4d%10s\n", lValue, 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; + fclose(fp); + } } - if (argc > 2) printf("%4d%5d%5d Total\n", lTotal, wTotal, bTotal); + 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); + } + return 0; }