diff options
author | Ian Moffett <ian@osmora.org> | 2025-06-28 17:01:37 -0400 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2025-06-28 17:01:37 -0400 |
commit | 6dd6c29e4ebad3a9adeab6069bc2d6387d745ea9 (patch) | |
tree | f0522f52a4d9ea68839d75695ab9731a604ce307 | |
parent | 1423fd8f0cfbf90caf0733f8f3881ac72dc63fcb (diff) |
usr.bin: cat: Use fgets() in cat
Implement line-by-line reading with fgets() instead of relying on
read()
Signed-off-by: Ian Moffett <ian@osmora.org>
-rw-r--r-- | usr.bin/cat/cat.c | 19 |
1 files 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 <stdio.h> #include <stdlib.h> -/* - * 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 |