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 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100755 src/cmd/cat/cat.c (limited to 'src/cmd/cat/cat.c') 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); +} + -- cgit v1.2.3