diff --git a/kernel/init.c b/kernel/init.c index 2a28defaff..3b23cc223d 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -138,3 +138,26 @@ int create_stdio(const char *file) { current->files->files[2] = fd_retain(fd); return 0; } + +static struct fd *open_fd_from_actual_fd(int actual_fd) { + int fd_no = actual_fd; + if (fd_no < 0) + return ERR_PTR(errno_map()); + struct fd *fd = fd_create(&realfs_fdops); + fd->real_fd = actual_fd; + fd->dir = NULL; + return fd; +} + +int create_piped_stdio() { + struct fd *si = open_fd_from_actual_fd(STDIN_FILENO); + struct fd *so = open_fd_from_actual_fd(STDOUT_FILENO); + struct fd *se = open_fd_from_actual_fd(STDERR_FILENO); + si->refcount = 0; + so->refcount = 0; + se->refcount = 0; + current->files->files[0] = fd_retain(si); + current->files->files[1] = fd_retain(so); + current->files->files[2] = fd_retain(se); + return 0; +} diff --git a/xX_main_Xx.h b/xX_main_Xx.h index dd0b2b8b38..0dd0ce8a36 100644 --- a/xX_main_Xx.h +++ b/xX_main_Xx.h @@ -93,9 +93,14 @@ static inline int xX_main_Xx(int argc, char *const argv[], const char *envp) { if (err < 0) return err; tty_drivers[TTY_CONSOLE_MAJOR] = &real_tty_driver; - err = create_stdio(console); - if (err < 0) - return err; + if (isatty(fileno(stdin))) { + err = create_stdio(console); + if (err < 0) + return err; + } + else { + create_piped_stdio(); + } exit_hook = exit_handler; return 0; }