Initial commit: add cat and yes

This commit is contained in:
g1n 2021-08-25 15:56:46 +03:00
commit 0c5f04cf56
6 changed files with 56 additions and 0 deletions

1
README.org Normal file
View File

@ -0,0 +1 @@
#+TITLE: GRU coreutils

5
src/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
*
!.gitignore
!cat.c
!yes.c
!Makefile

12
src/Makefile Normal file
View File

@ -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

18
src/cat.c Normal file
View File

@ -0,0 +1,18 @@
#include <stdio.h>
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);
}
}

17
src/yes.c Normal file
View File

@ -0,0 +1,17 @@
#include <stdio.h>
#include <string.h>
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);
}
}
}

3
tests/cat-test.txt Normal file
View File

@ -0,0 +1,3 @@
Test
Test
123