From 0c5f04cf56527dda52e5a103843b15a89c162dc4 Mon Sep 17 00:00:00 2001 From: g1n Date: Wed, 25 Aug 2021 15:56:46 +0300 Subject: [PATCH] Initial commit: add cat and yes --- README.org | 1 + src/.gitignore | 5 +++++ src/Makefile | 12 ++++++++++++ src/cat.c | 18 ++++++++++++++++++ src/yes.c | 17 +++++++++++++++++ tests/cat-test.txt | 3 +++ 6 files changed, 56 insertions(+) create mode 100644 README.org create mode 100644 src/.gitignore create mode 100644 src/Makefile create mode 100644 src/cat.c create mode 100644 src/yes.c create mode 100644 tests/cat-test.txt diff --git a/README.org b/README.org new file mode 100644 index 0000000..f3b703e --- /dev/null +++ b/README.org @@ -0,0 +1 @@ +#+TITLE: GRU coreutils diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 0000000..73e18e1 --- /dev/null +++ b/src/.gitignore @@ -0,0 +1,5 @@ +* +!.gitignore +!cat.c +!yes.c +!Makefile diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..9246988 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,12 @@ +CC = gcc +CFLAGS= -Wall -Wextra +LFLAGS= + +all: cat yes + +main: $(OBJFILES) + $(CC) cat.c -o cat + $(CC) yes.c -o yes + +test: main + ./cat ../tests/test.txt diff --git a/src/cat.c b/src/cat.c new file mode 100644 index 0000000..06c3678 --- /dev/null +++ b/src/cat.c @@ -0,0 +1,18 @@ +#include + +int main(int argc, char *argv[]){ + char ch; + for (int i = 1; i < argc; i++) { + FILE *file = fopen(argv[i], "r"); + if (file == NULL ) { + printf("Cannot find file: %s\n", argv[1]); + return 1; + } + + while ((ch = fgetc(file)) != EOF) + { + printf("%c", ch); + } + fclose(file); + } +} diff --git a/src/yes.c b/src/yes.c new file mode 100644 index 0000000..4868dfd --- /dev/null +++ b/src/yes.c @@ -0,0 +1,17 @@ +#include +#include + +int main(int argc, char *argv[]) { + char *output = "y"; + if (argc >= 1) { + while (1) { + for (int i = 1; i < argc; i++) + printf("%s ", argv[i]); + printf("\n"); + } + } else { + while (1) { + printf("%s\n", output); + } + } +} diff --git a/tests/cat-test.txt b/tests/cat-test.txt new file mode 100644 index 0000000..73b6d77 --- /dev/null +++ b/tests/cat-test.txt @@ -0,0 +1,3 @@ +Test +Test +123