From 9adde985d9c590220eb4513dedb2ef5715631984 Mon Sep 17 00:00:00 2001 From: emma Date: Mon, 24 Jul 2023 09:01:41 -0600 Subject: [PATCH] fixed segfault --- src/cat.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/cat.c b/src/cat.c index 74df7d5..1807acf 100644 --- a/src/cat.c +++ b/src/cat.c @@ -18,6 +18,7 @@ * along with this program. If not, see https://www.gnu.org/licenses/. */ +#include #include #include #include @@ -71,11 +72,12 @@ int main(int argc, char *argv[]) { * but shall accept multiple occurrences of '-' as a file * operand. */ - printf("argv[%d]: %s\n", i, argv[1]); - - if (argc == 1 || argv[i] == "-") { file = fdopen(1, "r"); } - else if ((file = fopen(argv[i], O_RDONLY)) == NULL) { - fputs(usage, stderr); + printf("argv[%d]: %s\n", i, argv[i]); + if (argv[i] == "-" || u && (argc - 1) == 1 || !u && argc == 1) { + file = fdopen(0, "r"); + } else if ((file = fopen(argv[i], "r")) == NULL) { + // TODO: Add error handling + fputs(usage, stderr); return EX_NOINPUT; } @@ -87,7 +89,11 @@ int main(int argc, char *argv[]) { putchar(byte); } } else { - char *buf = calloc(4096, 1); + char *buf = calloc(1, 4096); + while (byte != EOF) { + byte = fgetc(file); + fputc(byte, fdopen(1, "r")); + } } if (fclose(file) == -1) { fprintf(stderr, "%s: %s: Error closing file.\n", argv[0], argv[i]);