TCU

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

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

first commit

Diffstat:
ALICENSE | 8++++++++
AREADME | 3+++
Abin/echo | 0
Abin/false | 0
Abin/yes | 0
Ahead.c | 87+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/cat.c | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/echo.c | 20++++++++++++++++++++
Asrc/false.c | 5+++++
Asrc/head.c | 87+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/seq.c | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/yes.c | 17+++++++++++++++++
12 files changed, 346 insertions(+), 0 deletions(-)

diff --git a/LICENSE b/LICENSE @@ -0,0 +1,8 @@ +Copyright (c) 2022, Tyler Clark + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README b/README @@ -0,0 +1,3 @@ +Terrible Core Utils - TCU +This is a collection of core utilities written by someone that does not know what they are doing. +These core utilities are likely to be absolutely terrible, and are not to be used as the primary core utilities of a system. diff --git a/bin/echo b/bin/echo Binary files differ. diff --git a/bin/false b/bin/false Binary files differ. diff --git a/bin/yes b/bin/yes Binary files differ. diff --git a/head.c b/head.c @@ -0,0 +1,87 @@ +/* + * ARGUMENTS + * ========= + * -c, --bytes: prints specified number of bytes + * FINISHED: -n, --lines: Prints specified number of lines + * ========= + * FINISHED: if there are multiple files specified ==> foo <== will be printed before + * the lines in each file, where foo is the name of the file. + */ +#include<stdio.h> +#include<stdlib.h> +#include<string.h> + +int main(int argc, char *argv[]) { + FILE *fp; + char ch; + int lines = 10; // Default number of lines, can be chaged at runtime with -n arg + int i = 0; + int files = 1; + int delimeter = 0; + + int argByte = strcmp(argv[1], "-c"); + if (argByte == 0) { + printf("Some stuff would happen here\n"); + int bytes = strtol(argv[2], NULL, 10); + printf("It would print %d bytes\n", bytes); + files = 3; + if (files == (argc - 1)) { + fp = fopen(argv[files], "r"); + i = 0; + while(((ch = fgetc(fp)) != EOF) && (i < bytes)) { + printf("%c", ch); + i++; + } + return 0; + } else { + fp = fopen(argv[files], "r"); + i = 0; + while(((ch = fgetc(fp)) != EOF) && (i < bytes)) { + printf("%c", ch); + i++; + } + return 0; + } + } + + int argLine = strcmp(argv[1], "-n"); + if (argLine == 0) { + lines = strtol(argv[2], NULL, 10); + files = 3; + } + + if (files == (argc - 1)) { + fp = fopen(argv[files], "r"); + while((ch = fgetc(fp)) != EOF) { + if (i >= lines) { + fclose(fp); + i = 0; + break; + } else if (ch == '\n') { + printf("%c", ch); + i++; + } else { + printf("%c", ch); + } + } + return 0; + } + for (files = files; files < argc; files++) { + fp = fopen(argv[files], "r"); + printf("==> %s <==\n", argv[files]); + while((ch = fgetc(fp)) != EOF) { + if (i >= lines) { + fclose(fp); + i = 0; + break; + } else if (ch == '\n') { + printf("%c", ch); + i++; + } else { + printf("%c", ch); + } + } + fclose(fp); + } + return 0; +} diff --git a/src/cat.c b/src/cat.c @@ -0,0 +1,61 @@ +/* + * -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. + * 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. + */ +#include<stdio.h> +#include<stdlib.h> +#include<string.h> + +int main(int argc, char *argv[]) { + + int lNums = strcmp(argv[1], "-n"); + int nFiles = argc - 1; + int i; + char ch; + FILE *fp; + + if (lNums == 0) { + int line = 1; + fp = fopen(argv[2], "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; + } +} diff --git a/src/echo.c b/src/echo.c @@ -0,0 +1,20 @@ +/* Terrible implmentation of the core utility 'Echo' */ +/* For licnese information see the LICENSE file included in the repo */ +#include<stdio.h> +#include<string.h> + +int main(int argc, char *argv[]) +{ + if (argc == 1) + { + printf("\t\n"); + return 0; + } + int value = strcmp(argv[1], "-n"); + if (value == 0) { + printf("%s", argv[2]); + } else { + printf("%s\n", argv[1]); + } + return 0; +} diff --git a/src/false.c b/src/false.c @@ -0,0 +1,5 @@ +/* Terrible implementation of the false core util */ +/* For license infomration see the LICENSE file included in this repo */ +int main() { + return 1; +} diff --git a/src/head.c b/src/head.c @@ -0,0 +1,87 @@ +/* + * ARGUMENTS + * ========= + * -c, --bytes: prints specified number of bytes + * FINISHED: -n, --lines: Prints specified number of lines + * ========= + * FINISHED: if there are multiple files specified ==> foo <== will be printed before + * the lines in each file, where foo is the name of the file. + */ +#include<stdio.h> +#include<stdlib.h> +#include<string.h> + +int main(int argc, char *argv[]) { + FILE *fp; + char ch; + int lines = 10; // Default number of lines, can be chaged at runtime with -n arg + int i = 0; + int files = 1; + int delimeter = 0; + + int argByte = strcmp(argv[1], "-c"); + if (argByte == 0) { + printf("Some stuff would happen here\n"); + int bytes = strtol(argv[2], NULL, 10); + printf("It would print %d bytes\n", bytes); + files = 3; + if (files == (argc - 1)) { + fp = fopen(argv[files], "r"); + i = 0; + while(((ch = fgetc(fp)) != EOF) && (i < bytes)) { + printf("%c", ch); + i++; + } + return 0; + } else { + fp = fopen(argv[files], "r"); + i = 0; + while(((ch = fgetc(fp)) != EOF) && (i < bytes)) { + printf("%c", ch); + i++; + } + return 0; + } + } + + int argLine = strcmp(argv[1], "-n"); + if (argLine == 0) { + lines = strtol(argv[2], NULL, 10); + files = 3; + } + + if (files == (argc - 1)) { + fp = fopen(argv[files], "r"); + while((ch = fgetc(fp)) != EOF) { + if (i >= lines) { + fclose(fp); + i = 0; + break; + } else if (ch == '\n') { + printf("%c", ch); + i++; + } else { + printf("%c", ch); + } + } + return 0; + } + for (files = files; files < argc; files++) { + fp = fopen(argv[files], "r"); + printf("==> %s <==\n", argv[files]); + while((ch = fgetc(fp)) != EOF) { + if (i >= lines) { + fclose(fp); + i = 0; + break; + } else if (ch == '\n') { + printf("%c", ch); + i++; + } else { + printf("%c", ch); + } + } + fclose(fp); + } + return 0; +} diff --git a/src/seq.c b/src/seq.c @@ -0,0 +1,58 @@ +/* + * -f, --format: Use a printf style format + * -s, --separator: Use string to separate numbers + * -t, --terminiator: Use string to terminate sequence of numbers + * -w, --fixed-width: Equlize the widths of all numbers by padding with zeros as necessary. + */ +#include<stdio.h> +#include<stdlib.h> + +int sequence(long f_num, int incr, int l_num) +{ + long i; + if (f_num < l_num) { + for (i = f_num; i <= l_num; i = (i + incr)) { + printf("%ld\n", i); + } + } else if (l_num < f_num) { + for (i = l_num; i <= f_num; i = (i + incr)) { + printf("%ld\n", i); + } + } + + return 0; +} + +int main(const int argc, const char *argv[]) +{ + printf("Number of arguments: %d\n", (argc - 1)); + long nums[argc - 1]; + int i, j; + + //nums[0] = strtold(argv[j], NULL); + + for (i = 1; i < argc; i++) { + j = 0; + printf("Beginning loop\n"); + nums[j] = strtol(argv[i], NULL, 10); + printf("argument %d: %ld\n", i, nums[j]); + printf("End loop\n\n"); + } + + int format = 0, sep = 0, term = 0, fw = 0; + + for (i = 0; i < argc; i++) { + printf("%d in array is: %ld\n", i, nums[i]); + } + + sequence(nums[0], 1, nums[1]); + + /* + if (argc == 3) { + sequence(nums[0], nums[1], nums[2]); + } else { + sequence(nums[0], 1, nums[1]); + } + */ + +} diff --git a/src/yes.c b/src/yes.c @@ -0,0 +1,17 @@ +/* This is a terrible implementation of the core utility 'yes' */ +/* For license information see the LICENSE file included in the repository */ +#include<stdio.h> + +int main(int argc, char *argv[]) +{ + if(argc == 2) { + while ( 1 == 1) { + printf("%s\n", argv[1]); + } + return 0; + } + while ( 1 == 1) { + printf("yes\n"); + } + return 0; +}