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/wc.cc | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/user/wc.cc (limited to 'src/user/wc.cc') diff --git a/src/user/wc.cc b/src/user/wc.cc new file mode 100644 index 0000000..654aac2 --- /dev/null +++ b/src/user/wc.cc @@ -0,0 +1,56 @@ +#include "ulib/stdio.h" +#include "ulib/stdlib.h" +#include "ulib/string.h" +#include "user/user.h" +#include "common/fcntl.h" + +char buf[512]; + +void wc(int fd, char *name) +{ + int i, n; + int l, w, c, inword; + + l = w = c = 0; + inword = 0; + while ((n = read(fd, buf, sizeof(buf))) > 0) + { + for (i = 0; i < n; i++) + { + c++; + if (buf[i] == '\n') + l++; + if (strchr(" \r\t\n\v", buf[i])) + inword = 0; + else if (!inword) + { + w++; + inword = 1; + } + } + } + printf("%d %d %d %s\n", l, w, c, name); +} + +int main(int argc, char *argv[]) +{ + int fd, i; + + if (argc <= 1) + { + wc(0, ""); + exit(0); + } + + for (i = 1; i < argc; i++) + { + if ((fd = open(argv[i], O_RDONLY)) < 0) + { + printf("wc: cannot open %s\n", argv[i]); + exit(1); + } + wc(fd, argv[i]); + close(fd); + } + exit(0); +} \ No newline at end of file -- cgit v1.2.3