summaryrefslogtreecommitdiff
path: root/src/user/sh.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/user/sh.cc')
-rw-r--r--src/user/sh.cc26
1 files changed, 16 insertions, 10 deletions
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);