From c40f400f6ab879e08d54de753aabc987fd6ba07a Mon Sep 17 00:00:00 2001 From: Jacob McDonnell Date: Tue, 5 Mar 2024 18:46:44 -0500 Subject: Added a simple shell, jsh --- src/cmd/cat/cat.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/cmd/cat/makefile | 12 ++++++++++++ 2 files changed, 66 insertions(+) create mode 100755 src/cmd/cat/cat.c create mode 100755 src/cmd/cat/makefile (limited to 'src/cmd/cat') diff --git a/src/cmd/cat/cat.c b/src/cmd/cat/cat.c new file mode 100755 index 0000000..1c3e40a --- /dev/null +++ b/src/cmd/cat/cat.c @@ -0,0 +1,54 @@ +#include +#include +#include +#include + +void fileoutput(FILE *, const char *); +bool isdir(const char *); + +/* concatinate files to a file stream */ +int main(int argc, char **argv) +{ + FILE *fp; + if (argc == 1) { + fileoutput(stdin, "stdin"); + } else { + while (--argc > 0 && !ferror(stdout)) { + if (isdir(*++argv)) { + perror("cat"); + continue; + } + if ((fp = fopen(*argv, "r")) == NULL) { + perror("cat"); + continue; + } + fileoutput(fp, *argv); + } + } + if (ferror(stdout)) { + perror("cat"); + return -1; + } + return 0; +} + +/* fileoutput: output a file to stdout */ +void fileoutput(FILE *fp, const char *name) +{ + int c; + while ((c = getc(fp)) != EOF) + putc(c, stdout); + if (ferror(fp)) { + perror("cat"); + } + fclose(fp); +} + +/* isdir: return true if the file as the path is a directory */ +bool isdir(const char *path) +{ + struct stat sbuf; + stat(path, &sbuf); + return S_ISDIR(sbuf.st_mode); +} + diff --git a/src/cmd/cat/makefile b/src/cmd/cat/makefile new file mode 100755 index 0000000..bb8db9e --- /dev/null +++ b/src/cmd/cat/makefile @@ -0,0 +1,12 @@ +CFLAGS=-Wall -Werror + +build: cat + @cp cat $(PROROOT)/build/bin/cat + +cat: cat.c + $(CC) $(CFLAGS) -o cat cat.c + +clean: + @rm -r cat + +.PHONY: build clean -- cgit v1.2.3