diff options
| author | Christopher Uhlig | 2024-10-25 20:44:14 +0200 |
|---|---|---|
| committer | Christopher Uhlig | 2024-10-25 20:44:14 +0200 |
| commit | eafa035f9a72961280f4b1bb8f405f40000d3212 (patch) | |
| tree | 3566b9bcfde24d59afc7591179647f393184cfaf /src | |
| parent | ca4acea03cda19c2a0f0fd168d3c8fd418d71e04 (diff) | |
| parent | 15d8385f8ecf30e1ca74025b12fed7e45349b706 (diff) | |
| download | minishell-eafa035f9a72961280f4b1bb8f405f40000d3212.tar.gz minishell-eafa035f9a72961280f4b1bb8f405f40000d3212.zip | |
resolved merge conflicts
Diffstat (limited to 'src')
| -rw-r--r-- | src/env.c | 5 | ||||
| -rw-r--r-- | src/env_to_strlst.c | 58 | ||||
| -rw-r--r-- | src/execute_cmd.c | 59 | ||||
| -rw-r--r-- | src/get_cmd_path.c | 82 | ||||
| -rw-r--r-- | src/interpreter.c | 55 | ||||
| -rw-r--r-- | src/main.c | 2 |
6 files changed, 203 insertions, 58 deletions
@@ -6,13 +6,14 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 14:31:07 by chuhlig #+# #+# */ -/* Updated: 2024/10/25 15:48:56 by chuhlig ### ########.fr */ +/* Updated: 2024/10/25 19:17:54 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "env.h" #include "get_next_line.h" #include "libft.h" +#include <stdlib.h> void getenvlst(t_env **env, char **en) { @@ -51,7 +52,7 @@ void free_envlst(t_env **env) } } -char *env_get(t_env *env, char *name) +char *env_get(t_env *env, char *name) { while (env != NULL) { diff --git a/src/env_to_strlst.c b/src/env_to_strlst.c new file mode 100644 index 0000000..3a58df9 --- /dev/null +++ b/src/env_to_strlst.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* env_to_strlst.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/21 14:52:08 by dkaiser #+# #+# */ +/* Updated: 2024/10/21 15:07:33 by dkaiser ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "minishell.h" + +static char *get_var_assign(t_env *cur); + +char **env_to_strlst(t_env *env) +{ + int size; + t_env *cur; + char **result; + int i; + + size = 0; + cur = env; + while (cur != NULL) + { + size++; + cur = cur->next; + } + result = malloc(sizeof(char *) * (size + 1)); + if (result == NULL) + return (NULL); + i = 0; + cur = env; + while (i < size) + { + result[i] = get_var_assign(cur); + cur = cur->next; + i++; + } + result[i] = NULL; + return (result); +} + +static char *get_var_assign(t_env *cur) +{ + char *left_side; + char *result; + + left_side = ft_strjoin(cur->name, "="); + if (left_side == NULL) + return (NULL); + result = ft_strjoin(left_side, cur->value); + free(left_side); + return (result); +} diff --git a/src/execute_cmd.c b/src/execute_cmd.c new file mode 100644 index 0000000..fa7677f --- /dev/null +++ b/src/execute_cmd.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* execute_cmd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/21 13:58:56 by dkaiser #+# #+# */ +/* Updated: 2024/10/25 13:31:16 by dkaiser ### ########.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); +} diff --git a/src/get_cmd_path.c b/src/get_cmd_path.c new file mode 100644 index 0000000..1ecf393 --- /dev/null +++ b/src/get_cmd_path.c @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_cmd_path.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/17 16:45:47 by dkaiser #+# #+# */ +/* Updated: 2024/10/17 17:11:27 by dkaiser ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "minishell.h" + +static char *get_absolute_cmd_path(char *cmd, t_env *env); +static char *find_in_path(char *cmd, t_env *env); +char **get_split_path(t_env *env); + +char *get_cmd_path(char *cmd, t_env *env) +{ + if (cmd[0] == '/') + return (ft_strdup(cmd)); + else if (ft_strchr(cmd, '/')) + return (get_absolute_cmd_path(cmd, env)); + else + return (find_in_path(cmd, env)); +} + +static char *get_absolute_cmd_path(char *cmd, t_env *env) +{ + char *cur_dir; + char *result; + + cur_dir = ft_strjoin(env_get(env, "PWD"), "/"); + if (!cur_dir) + return (NULL); + result = ft_strjoin(cur_dir, cmd); + free(cur_dir); + if (!result) + return (NULL); + if (access(result, X_OK) == -1) + { + free(result); + return (NULL); + } + return (result); +} + +static char *find_in_path(char *cmd, t_env *env) +{ + char *cur_path; + char *cmd_path; + char **path; + + path = get_split_path(env); + cmd_path = NULL; + while (*path) + { + if (cmd_path) + free(cmd_path); + cur_path = ft_strjoin(*path, "/"); + if (!cur_path) + return (NULL); + cmd_path = ft_strjoin(cur_path, cmd); + free(cur_path); + if (!cmd_path) + return (NULL); + if (access(cmd_path, X_OK) != -1) + return (cmd_path); + path++; + } + return (NULL); +} + +char **get_split_path(t_env *env) +{ + char *path; + + path = env_get(env, "PATH"); + return (ft_split(path, ':')); +} diff --git a/src/interpreter.c b/src/interpreter.c index 13f10ed..e69de29 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -1,55 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* interpreter.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/08/05 13:15:24 by dkaiser #+# #+# */ -/* Updated: 2024/10/25 15:46:53 by chuhlig ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" -#include <stdio.h> - -static int eval_pipe(t_pipe *pipe, t_env *env); -static int eval_cmd(t_cmd *cmd, t_env **env); - -int eval(t_node *node, t_env **env) -{ - if (node->type == PIPE_NODE) - return (eval_pipe(&node->content.pipe, *env)); - else if (node->type == CMD_NODE) - return (eval_cmd(&node->content.cmd, env)); - else - { - panic(UNREACHABLE); - return (-1); - } -} - -static int eval_pipe(t_pipe *pipe, t_env *env) -{ - dbg("TODO: PIPE"); - eval_cmd(&pipe->left->content.cmd, &env); - eval_cmd(&pipe->right->content.cmd, &env); - return (0); -} - -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); -} @@ -6,7 +6,7 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:03 by dkaiser #+# #+# */ -/* Updated: 2024/10/25 15:53:10 by chuhlig ### ########.fr */ +/* Updated: 2024/10/25 16:06:32 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ |
