diff options
| author | Dominik Kaiser | 2025-01-20 20:14:32 +0100 |
|---|---|---|
| committer | GitHub | 2025-01-20 20:14:32 +0100 |
| commit | bd8c817797d5f2b1affe6957ffc51846a38e70ec (patch) | |
| tree | 2fc0f567b1c4f2f168a931ad0bff69e52c6c226c /src/execute_cmd.c | |
| parent | a9aba07b52cbf98eb9c52cd8ee0cd5f5021d2931 (diff) | |
| parent | dc6a4f2d0de92984c2584ef905011e2a60792850 (diff) | |
| download | minishell-bd8c817797d5f2b1affe6957ffc51846a38e70ec.tar.gz minishell-bd8c817797d5f2b1affe6957ffc51846a38e70ec.zip | |
Merge interpreter changes into main
Miau
Diffstat (limited to 'src/execute_cmd.c')
| -rw-r--r-- | src/execute_cmd.c | 130 |
1 files changed, 79 insertions, 51 deletions
diff --git a/src/execute_cmd.c b/src/execute_cmd.c index 6386fc0..1438f9c 100644 --- a/src/execute_cmd.c +++ b/src/execute_cmd.c @@ -5,72 +5,100 @@ /* +:+ +:+ +:+ */ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/10/21 13:58:56 by dkaiser #+# #+# */ -/* Updated: 2024/10/25 20:59:22 by chuhlig ### ########.fr */ +/* Created: 2024/12/17 19:21:35 by chuhlig #+# #+# */ +/* Updated: 2025/01/20 20:04:31 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +#include <stdio.h> #include <stdlib.h> -#include <sys/_types/_pid_t.h> -#include <sys/_types/_s_ifmt.h> -#include <sys/fcntl.h> -#include <unistd.h> -#include <fcntl.h> -int execute_cmd(t_cmd *cmd, t_env *env) +static void establish_pipeline(int original_stdin, int original_stdout); +static int exec_cmd(t_cmd *cmd, t_env **env, int original_std[2], int result); + +int is_builtin(char *cmd) { - int result; - char *cmd_path; - int fd; + return ((ft_strcmp(cmd, "export") == 0) || (ft_strcmp(cmd, "unset") == 0) + || (ft_strcmp(cmd, "cd") == 0) || (ft_strcmp(cmd, "exit") == 0) + || (ft_strcmp(cmd, "echo") == 0) || (ft_strcmp(cmd, "pwd") == 0) + || (ft_strcmp(cmd, "env") == 0)); +} - cmd_path = get_cmd_path(cmd->args[0], env); - cmd->args[0] = cmd_path; +int execute_builtin(char **args, t_env **env) +{ + if (ft_strcmp(args[0], "export") == 0) + return (export(args, env, ft_atoi("0"))); + else if (ft_strcmp(args[0], "unset") == 0) + return (unset(args, env)); + else if (ft_strcmp(args[0], "cd") == 0) + return (cd(env, args)); + else if (ft_strcmp(args[0], "echo") == 0) + return (echo(args)); + else if (ft_strcmp(args[0], "pwd") == 0) + return (pwd(*env)); + else if (ft_strcmp(args[0], "env") == 0) + return (ft_env(*env)); + else if (ft_strcmp(args[0], "exit") == 0) + return (builtin_exit(args, env)); + return (1); +} - if (cmd->redirs[0].type == INPUT_FILE) +int execute_cmd(t_cmd *cmd, t_env **env) +{ + int original_std[2]; + int result; + + original_std[1] = dup(STDOUT_FILENO); + original_std[0] = dup(STDIN_FILENO); + create_files(cmd->create_files); + if (handle_redirections(cmd->redirs) == -1) { - fd = open(cmd->redirs[0].specifier, O_RDONLY); - if (fd < 0) - return (EXIT_FAILURE); - dup2(fd, STDIN_FILENO); + establish_pipeline(original_std[0], original_std[1]); + return (EXIT_FAILURE); } - else if (cmd->redirs[0].type == INPUT_LIMITER) + if (is_builtin(cmd->args[0])) { - dbg("INPUT_LIMITER"); + result = execute_builtin(cmd->args, env); + establish_pipeline(original_std[0], original_std[1]); + return (result); } - if (cmd->redirs[1].type == OUTPUT_APPEND) + return (exec_cmd(cmd, env, original_std, EXIT_SUCCESS)); +} + +static void establish_pipeline(int original_stdin, int original_stdout) +{ + dup2(original_stdout, STDOUT_FILENO); + dup2(original_stdin, STDIN_FILENO); + close(original_stdout); + close(original_stdin); +} + +static int exec_cmd(t_cmd *cmd, t_env **env, int original_std[2], int result) +{ + int i; + int status; + char *cmd_path; + pid_t pid; + + pid = fork(); + if (pid == -1) { - dbg("OUTPUT_APPEND"); - fd = open(cmd->redirs[1].specifier, O_WRONLY | O_CREAT | O_APPEND); - if (fd < 0) - return (EXIT_FAILURE); - dup2(fd, STDOUT_FILENO); + perror("fork"); + establish_pipeline(original_std[0], original_std[1]); + return (EXIT_FAILURE); } - else if (cmd->redirs[1].type == OUTPUT_OVERRIDE) + if (pid == 0) { - fd = open(cmd->redirs[1].specifier, O_WRONLY | O_CREAT | O_TRUNC); - if (fd < 0) - return (EXIT_FAILURE); - dup2(fd, STDOUT_FILENO); - dbg("OUTPUT_OVERRIDE"); + i = 0; + while (cmd->args[i][0] == '\0') + i++; + cmd_path = get_cmd_path(cmd->args[i], *env, &result); + if (cmd_path != NULL) + execve(cmd_path, &(cmd->args[i]), env_to_strlst(*env)); + exit(result); } - result = execve(cmd->args[0], cmd->args, env_to_strlst(env)); - return (result); + waitpid(pid, &status, 0); + establish_pipeline(original_std[0], original_std[1]); + return ((status >> 8) & 255); } - -static int eval_cmd(t_cmd *cmd, t_env **env) -{ - if (ft_strncmp(cmd->args[0], "pwd", 4) == 0) - pwd(*env); - else if (ft_strncmp(cmd->args[0], "echo", 5) == 0) - echo(cmd->args); - else if (ft_strncmp(cmd->args[0], "env", 4) == 0) - ft_env(*env); - else if (ft_strncmp(cmd->args[0], "cd", 3) == 0) - cd(env, cmd->args); - else if (ft_strncmp(cmd->args[0], "unset", 6) == 0) - unset(cmd->args, env); - else if (ft_strncmp(cmd->args[0], "export", 7) == 0) - export(cmd->args, env); - return (0); -}
\ No newline at end of file |
