From 3886749b1c8d79d4701bf6b8e66249c4e1491648 Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Sun, 4 Jan 2026 18:06:31 +0800 Subject: dev(shell): implement basic shell tools fixed(file-system): fixed read and write bugs --- src/user/cat.cc | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/user/cat.cc (limited to 'src/user/cat.cc') diff --git a/src/user/cat.cc b/src/user/cat.cc new file mode 100644 index 0000000..67feb1c --- /dev/null +++ b/src/user/cat.cc @@ -0,0 +1,48 @@ +#include "ulib/stdio.h" +#include "ulib/stdlib.h" +#include "ulib/string.h" +#include "user/user.h" +#include "common/fcntl.h" + +char buf[512]; + +void cat(int fd) +{ + int n; + while ((n = read(fd, buf, sizeof(buf))) > 0) + { + if (write(1, buf, n) != n) + { + printf("cat: write error\n"); + exit(1); + } + } + if (n < 0) + { + printf("cat: read error\n"); + exit(1); + } +} + +int main(int argc, char *argv[]) +{ + int fd, i; + + if (argc <= 1) + { + cat(0); + exit(0); + } + + for (i = 1; i < argc; i++) + { + if ((fd = open(argv[i], O_RDONLY)) < 0) + { + printf("cat: cannot open %s\n", argv[i]); + exit(1); + } + cat(fd); + close(fd); + } + exit(0); +} \ No newline at end of file -- cgit v1.2.3