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/sh.cc | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'src/user/sh.cc') diff --git a/src/user/sh.cc b/src/user/sh.cc index b4122f7..760f29e 100644 --- a/src/user/sh.cc +++ b/src/user/sh.cc @@ -67,11 +67,26 @@ struct RedirCommand : public Command void run() override { close(fd); - if (open(file, mode) < 0) + + int newfd = open(file, mode); + + if (newfd < 0) { printf("open %s failed\n", file); exit(1); } + + if (newfd != fd) + { + + if (dup(newfd) != fd) + { + printf("Redirection failed: could not move fd %d to %d\n", newfd, fd); + exit(1); + } + close(newfd); + } + sub_cmd->run(); } }; @@ -348,15 +363,6 @@ int main() { static char buf[100]; - // int fd; - // while ((fd = open("console", O_RDWR)) >= 0) - // { - // if (fd >= 3) - // { - // close(fd); - // break; - // } - // } printf(ANSI_GREEN"\n[Lume Shell] Welcome! \n"ANSI_RESET); printf(ANSI_YELLOW "[Lume Shell] Start your journey of exploration!"ANSI_RESET); -- cgit v1.2.3