From 3a410edddc1fd73edaf3edf35270ef9295a4eb50 Mon Sep 17 00:00:00 2001 From: emma Date: Mon, 28 Aug 2023 16:03:36 -0600 Subject: [PATCH] cat(1p): fixed storing EOF in buffer --- src/cat.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/cat.c b/src/cat.c index 6fd9671..4c1f17d 100644 --- a/src/cat.c +++ b/src/cat.c @@ -31,19 +31,15 @@ void cat(FILE *file, bool u) { char buf[4096]; /* buffer for buffered reading */ if (u) { - while (byte != EOF) { - byte = fgetc(file); - putchar(byte); - } + while ((byte = fgetc(file)) != EOF) { putchar(byte); } } else { - while (byte != EOF) { - byte = fgetc(file); - if (p > sizeof(buf)) { + while ((byte = fgetc(file)) != EOF) { + if (p > sizeof(buf) - 1) { fputs(buf, stdout); p = 0; } else { buf[p] = byte; - p += 1; + p++; } }