TCU

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

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

Updating licese file and adding work on cat

Diffstat:
MLICENSE | 2+-
Msrc/cat.c | 70++++++++++++++++++++++++++++++++++++----------------------------------
2 files changed, 37 insertions(+), 35 deletions(-)

diff --git a/LICENSE b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2022, Tyler Clark +Copyright (c) 2023, Tyler Clark Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/src/cat.c b/src/cat.c @@ -1,12 +1,12 @@ /* - * -b: Number the non-blank output lines, starting at 1 - * -e: Display non-printing characters (see the -v option), and display a dollar sign ('$') at the end of each line - * -l: Set an exclusive advisory lock on the standard output file descriptor. + * PROG: -b: Number the non-blank output lines, starting at 1 + * TODO: -e: Display non-printing characters (see the -v option), and display a dollar sign ('$') at the end of each line + * TODO: -l: Set an exclusive advisory lock on the standard output file descriptor. * DONE: -n: Number the output lines, starting at 1 - * -s: Squeeze multiple adjacent empty lines, causing the output to be single spaced - * -t: Display non-printing characters (see the -v option), and display tab characters as '^I' - * -u: Disable output buffering - * -v: Display non-printing characters so they are visible. Control characters print as '^X' for control-X; the delete character (octal 0177) prints as '^?'. Non-ASCII characters (with the high bit set) are printed as 'M-' (for meta) followed by the character for the low 7 bits. + * TODO: -s: Squeeze multiple adjacent empty lines, causing the output to be single spaced + * TODO: -t: Display non-printing characters (see the -v option), and display tab characters as '^I' + * TODO: -u: Disable output buffering + * TODO: -v: Display non-printing characters so they are visible. Control characters print as '^X' for control-X; the delete character (octal 0177) prints as '^?'. Non-ASCII characters (with the high bit set) are printed as 'M-' (for meta) followed by the character for the low 7 bits. */ #include<stdio.h> #include<stdlib.h> @@ -14,48 +14,50 @@ int main(int argc, char *argv[]) { - int lNums = strcmp(argv[1], "-n"); int nFiles = argc - 1; int i; char ch; FILE *fp; + /* Implements the '-n' flag */ + int lNums = strcmp(argv[1], "-n"); if (lNums == 0) { int line = 1; - fp = fopen(argv[2], "r"); + 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); + } else { + printf("%c", ch); + } + } + + fclose(fp); + } + return 0; + } + + /* The following code creates the basic functionality of cat */ + for (i = 1; i <= nFiles; i++) { + fp = fopen(argv[i], "r"); if (fp == NULL) { perror("error while opening file\n"); exit(EXIT_FAILURE); } - printf("%d\t", line); - line++; while((ch = fgetc(fp)) != EOF) { - if (ch == '\n') { - printf("\n%d\t", line); - line++; - } else { - printf("%c", ch); - } - } - - fclose(fp); - printf("\n"); - return 0; - } else { - for (i = 1; i <= nFiles; i++) { - fp = fopen(argv[i], "r"); - if (fp == NULL) - { - perror("error while opening file\n"); - exit(EXIT_FAILURE); - } - while((ch = fgetc(fp)) != EOF) { printf("%c", ch); - } + } fclose(fp); - } - return 0; } + return 0; }