diff --git a/src/exec/exec.c b/src/exec/exec.c index dadd00a..b2fc733 100644 --- a/src/exec/exec.c +++ b/src/exec/exec.c @@ -7,23 +7,6 @@ #include #include "../env/env.h" -int execute(cmd* input, char** env) -{ - int exitcode; - const int pid = fork(); - if (pid == 0) - { - dup2(input->fd_out, 1); - dup2(input->fd_in, 0); - exitcode = execve(input->executable, input->args, env); - } - else - { - waitpid(pid, &exitcode, 0); - } - return exitcode; -} - int len(void** list) { int index; @@ -31,24 +14,6 @@ int len(void** list) return index; } -int piper(cmd** cmds, char** env) -{ - int fds[2]; - for (int i=0; cmds[i] != NULL; i++) - { - if (pipe(fds) < 0) - return 1; - if (i < len((void**)cmds)-1) - { - cmds[i]->fd_out = fds[1]; - cmds[i+1]->fd_in = fds[0]; - } - execute(cmds[i], env); - - } - return 0; -} - int change_directory(char** args, lst** env) { char cwd[PATH_MAX]; @@ -84,10 +49,48 @@ int change_directory(char** args, lst** env) int builtin_execute(cmd* input, lst** env) { - int exitcode; if (strcmp(input->executable, "cd") == 0) { - exitcode = change_directory(input->args, env); + change_directory(input->args, env); + return 0; + } + else + { + return 1; + } +} + +int execute(cmd* input, lst** env) +{ + int exitcode; + const int pid = fork(); + if (pid == 0) + { + dup2(input->fd_out, 1); + dup2(input->fd_in, 0); + exitcode = execve(input->executable, input->args, get_env_str(env)); + } + else + { + waitpid(pid, &exitcode, 0); } return exitcode; } + +int command_list_exec(cmd** cmds, lst** env) +{ + int fds[2]; + for (int i=0; cmds[i] != NULL; i++) + { + if (pipe(fds) < 0) + return 1; + if (i < len((void**)cmds)-1) + { + cmds[i]->fd_out = fds[1]; + cmds[i+1]->fd_in = fds[0]; + } + if (builtin_execute(cmds[i], env) == 1) + execute(cmds[i], env); + } + return 0; +} diff --git a/src/exec/exec.h b/src/exec/exec.h index 03d2228..6ffe2fd 100644 --- a/src/exec/exec.h +++ b/src/exec/exec.h @@ -1,5 +1,5 @@ #pragma once -int execute(cmd* input, char** env); +int execute(cmd* input, lst** env); int builtin_execute(cmd* input, lst** env); -int piper(cmd** cmds, char** env); +int piper(cmd** cmds, lst** env);