TCU

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

commit 5757793814f467f4c7239d805074bdf4bb401e87
parent d6263575032d47d7523444f583f8f48e817c3ea9
Author: Tyler Clark <tyler.clark@foxide.xyz>
Date:   Sun,  9 Feb 2025 21:42:27 -0500

Starting work on WC, and working on cat

Diffstat:
Msrc/cat.c | 34++++++++++++++++++++++++++++++++++
Asrc/wc.c | 50++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 84 insertions(+), 0 deletions(-)

diff --git a/src/cat.c b/src/cat.c @@ -19,6 +19,40 @@ int main(int argc, char *argv[]) { char ch; FILE *fp; + /* Implements the '-b' flag */ + /* This flag should be re-done to recycle */ + /* code rather than re-doing it */ + int bNums = strcmp(argv[1], "-b"); + if (bNums == 0) { + int line = 1; + for (i = 2; i <= nFiles; i++) { + fp = fopen(argv[i], "r"); + if (fp == NULL) + { + perror("error while opening file\n"); + exit(EXIT_FAILURE); + } + printf("\t%d ", line); + while((ch = fgetc(fp)) != EOF) { + if (ch == '\n') { + line++; + printf("\n\t%d ", line); + //printf("\n"); + ch = fgetc(fp); + if (ch == '\n') { + printf("\n"); + } else { + ungetc(ch, fp); + } + } else { + printf("%c", ch); + } + } + fclose(fp); + } + return 0; + } + /* Implements the '-n' flag */ int lNums = strcmp(argv[1], "-n"); if (lNums == 0) { diff --git a/src/wc.c b/src/wc.c @@ -0,0 +1,50 @@ +/* wc utility from TCU */ +/* for licensing information please see included LICENSE file */ + +/* 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. */ + +/* Flags that TCU accepts */ +/* TODO: --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. */ + +#include<stdio.h> +#include<string.h> + +int main(int argc, char *argv[]) { + /* Prep stuff */ + FILE *fp; + char ch; + /* relatively setting the counters for lines, words, and bytes to zero */ + int lValue = 0, wValue = 0, bValue = 0; + /* This will be used to iterate through files */ + int i = 0; + + /* rough draft of basic idea */ + for (i = 0; i <= argc; i++) { + fp = fopen(argv[i], "r"); + while((ch = fgetc(fp)) != EOF) { + if (ch == '\n') { + lValue++; + wValue++; + ch = fgetc(fp); + if(ch == '\n') { + lValue++; + bValue++; + } else { + ungetc(ch, fp); + } + } + if (ch == ' ') { + wValue++; + } + bValue++; + } + printf("%4d%5d%5d%8s\n", lValue, wValue, bValue, argv[1]); + } + return 0; +}