From 6dd6c29e4ebad3a9adeab6069bc2d6387d745ea9 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Sat, 28 Jun 2025 17:01:37 -0400 Subject: usr.bin: cat: Use fgets() in cat Implement line-by-line reading with fgets() instead of relying on read() Signed-off-by: Ian Moffett --- usr.bin/cat/cat.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/usr.bin/cat/cat.c b/usr.bin/cat/cat.c index d40986b..b51e6a0 100644 --- a/usr.bin/cat/cat.c +++ b/usr.bin/cat/cat.c @@ -34,24 +34,23 @@ #include #include -/* - * TODO FIXME (BUG): Print line by line (fix line clobbering issue) - */ static void cat(const char *pathname) { - char buf[4096]; + FILE *file; + char buf[64]; int fd; - fd = open(pathname, O_RDONLY); - if (fd < 0) { - printf("cat: could not open %s\n", pathname); + file = fopen(pathname, "r"); + if (file == NULL) { return; } - read(fd, buf, sizeof(buf)); - printf("%s", buf); - close(fd); + while (fgets(buf, sizeof(buf), file) != NULL) { + printf("%s", buf); + } + + fclose(file); } int -- cgit v1.2.3