TCU

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

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

Commiting work on WC and updating the Makefile to build wc with 'wordcount'

Diffstat:
MMakefile | 2+-
Msrc/wc.c | 33++++++++++++++++++++++++++++++---
2 files changed, 31 insertions(+), 4 deletions(-)

diff --git a/Makefile b/Makefile @@ -19,7 +19,7 @@ seq: $(CC) src/seq.c -o bin/seq true: $(CC) src/true.c -o bin/true -wc: +wordcount: $(CC) src/wc.c -o bin/wc yes: $(CC) src/yes.c -o bin/yes diff --git a/src/wc.c b/src/wc.c @@ -6,11 +6,12 @@ /* Flags that TCU accepts */ /* 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". */ +/* 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. */ /* 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,7 +24,7 @@ int main(int argc, char *argv[]) { FILE *fp; char ch; /* Initilizing values for counters */ - int lValue, wValue, bValue; + int lValue = 0, wValue = 0, bValue = 0; /* Initializing values for totals */ int lTotal = 0, wTotal = 0, bTotal = 0; /* This will be used to iterate through files */ @@ -36,7 +37,33 @@ int main(int argc, char *argv[]) { int countWords = strcmp(argv[i], "-w"); if (countWords == 0) i++; - printf("i == %d\n", i); + /* This argument is handled differently than the others */ + int longestLine = strcmp(argv[i], "-L"); + if (longestLine == 0) { + i++; + int llLine = 0; + for (i = i; i < argc; i++) { + fp = fopen(argv[i], "r"); + int lLine = 0; + bValue = 0; + while((ch = fgetc(fp)) != EOF) { + if (ch == '\n') { + if (bValue > lLine) { + lLine = bValue; + } + bValue = 0; + } else { + bValue++; + } + } + fclose(fp); + printf("%4d%10s\n", lLine, argv[i]); + if (lLine > llLine) llLine = lLine; + } + if (argc > 3) printf("%4d Total\n", llLine); + return 0; + } + /* rough draft of basic idea */ for (i = i; i < argc; i++) { lValue = 0;