diff options
| author | Ákos Kőrösi <korakos99@gmail.com> | 2026-05-18 13:19:15 +0200 |
|---|---|---|
| committer | Ákos Kőrösi <korakos99@gmail.com> | 2026-05-18 13:19:15 +0200 |
| commit | 24ba34266cc977b22d777bceca9683a45dd73042 (patch) | |
| tree | d9202b0f6d2e9d5ec9e14926e30ead18daa45ae8 /exec.c | |
| parent | d31d9ce64c3085bea1a094e649f62176b1a896a6 (diff) | |
start outlining fd-based redirecting
Diffstat (limited to 'exec.c')
| -rw-r--r-- | exec.c | 36 |
1 files changed, 27 insertions, 9 deletions
@@ -2,6 +2,7 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <fcntl.h> #include <signal.h> #include <sys/wait.h> @@ -108,14 +109,6 @@ char** make_cmd_args(Command command, ShellState* shstate) { } -typedef struct { - char** args; - int stdin_fd; - int stdout_fd; - IntVec child_close_fds; - IntVec parent_close_fds; -} ExecutableCommand; - void execute_command(ExecutableCommand cmd, int* pgid) { pid_t pid = fork(); @@ -130,6 +123,11 @@ void execute_command(ExecutableCommand cmd, int* pgid) { dup2(cmd.stdin_fd, STDIN_FILENO); dup2(cmd.stdout_fd, STDOUT_FILENO); + for (int j = 0; j < cmd.redirs.count; j++) { + FdRedirect fd_rd = *get_from_FdRedirectVec(&cmd.redirs, j); + dup2(fd_rd.old_fd, fd_rd.new_fd); + } + for (int i = 0; i < cmd.child_close_fds.count; i++) { close(*get_from_IntVec(&cmd.child_close_fds, i)); } @@ -164,7 +162,11 @@ void run_pipeline(Pipeline pl, ShellState* shstate) { for (int i = 0; i < pl.count; i++) { PipelineElement elem = *get_from_Pipeline(&pl, i); - ExecutableCommand cmd = {.child_close_fds = make_IntVec(64), .parent_close_fds = make_IntVec(64)}; + ExecutableCommand cmd = { + .child_close_fds = make_IntVec(64), + .parent_close_fds = make_IntVec(64), + .redirs = make_FdRedirectVec(32) + }; cmd.args = make_cmd_args(elem.cmd, shstate); if (i == 0) { @@ -186,6 +188,22 @@ void run_pipeline(Pipeline pl, ShellState* shstate) { cmd.stdout_fd = STDOUT_FILENO; } + for (int j = 0; j < elem.cmd.redirects.count; j++) { + Redirect rd = *get_from_RedirectVec(&elem.cmd.redirects, j); + switch (rd.kind) { + case STDIN_REDIR: + /* + int in_fd = open(rd.as.std_redir_filename, O_RDONLY, 0644); + push_to_FdRedirectVec(&cmd.redirs, (FdRedirect){.old_fd = STDIN_REDIR, .new_fd = in_fd}); + push_to_IntVec(&cmd.child_close_fds, in_fd); + push_to_IntVec(&cmd.parent_close_fds, in_fd); + */ + break; + default: + exit(69); + } + } + execute_command(cmd, &pgid); for (int j = 0; j < cmd.parent_close_fds.count; j++) { |
