diff options
| author | Dominik Kaiser | 2025-01-14 16:40:30 +0100 |
|---|---|---|
| committer | GitHub | 2025-01-14 16:40:30 +0100 |
| commit | 398b0d39cbbe2cdabbfae00f799181a37754d5c1 (patch) | |
| tree | 397ae613dd39a35a6c91e7e30426f86ade4e24bb /src/execute_cmd.c | |
| parent | 00ad7429f223c85e99da6ffa8f7dade0c73c97b5 (diff) | |
| parent | 553204e584dd08987902c7693e47744192e6bd85 (diff) | |
| download | minishell-398b0d39cbbe2cdabbfae00f799181a37754d5c1.tar.gz minishell-398b0d39cbbe2cdabbfae00f799181a37754d5c1.zip | |
Merge branch 'main' into echo-builtint
Diffstat (limited to 'src/execute_cmd.c')
| -rw-r--r-- | src/execute_cmd.c | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/execute_cmd.c b/src/execute_cmd.c new file mode 100644 index 0000000..6386fc0 --- /dev/null +++ b/src/execute_cmd.c @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* execute_cmd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* 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 */ +/* */ +/* ************************************************************************** */ + +#include "minishell.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) +{ + int result; + char *cmd_path; + int fd; + + cmd_path = get_cmd_path(cmd->args[0], env); + cmd->args[0] = cmd_path; + + if (cmd->redirs[0].type == INPUT_FILE) + { + fd = open(cmd->redirs[0].specifier, O_RDONLY); + if (fd < 0) + return (EXIT_FAILURE); + dup2(fd, STDIN_FILENO); + } + else if (cmd->redirs[0].type == INPUT_LIMITER) + { + dbg("INPUT_LIMITER"); + } + if (cmd->redirs[1].type == OUTPUT_APPEND) + { + 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); + } + else if (cmd->redirs[1].type == OUTPUT_OVERRIDE) + { + 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"); + } + result = execve(cmd->args[0], cmd->args, env_to_strlst(env)); + return (result); +} + +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 |
