diff options
author | salad <fmoenne-@student.le-101.fr> | 2020-04-29 11:47:02 +0200 |
---|---|---|
committer | salad <fmoenne-@student.le-101.fr> | 2020-04-29 11:47:02 +0200 |
commit | 961bda4f42562bf8ceb70a888730362d021c5552 (patch) | |
tree | 820577dc790c3a8884c44b329d04a8d42ed1ca44 /src | |
parent | simple really (diff) | |
parent | Trying vars (diff) | |
download | 42-minishell-961bda4f42562bf8ceb70a888730362d021c5552.tar.gz 42-minishell-961bda4f42562bf8ceb70a888730362d021c5552.tar.bz2 42-minishell-961bda4f42562bf8ceb70a888730362d021c5552.tar.xz 42-minishell-961bda4f42562bf8ceb70a888730362d021c5552.tar.zst 42-minishell-961bda4f42562bf8ceb70a888730362d021c5552.zip |
merge master into fmoenne_indahouse
Diffstat (limited to 'src')
-rw-r--r-- | src/ft_b_cd.c | 59 | ||||
-rw-r--r-- | src/ft_b_exit.c | 11 | ||||
-rw-r--r-- | src/ft_e_builtins.c | 24 | ||||
-rw-r--r-- | src/ft_e_lcom.c | 1 | ||||
-rw-r--r-- | src/ft_f_chdir.c | 27 | ||||
-rw-r--r-- | src/ft_f_chdir.h | 22 | ||||
-rw-r--r-- | src/ft_f_errno.c | 26 | ||||
-rw-r--r-- | src/ft_f_errno.h | 21 | ||||
-rw-r--r-- | src/ft_f_fail.h | 3 | ||||
-rw-r--r-- | src/ft_m_argv.c | 2 | ||||
-rw-r--r-- | src/ft_m_argv.h | 2 | ||||
-rw-r--r-- | src/ft_m_redirs.c | 2 | ||||
-rw-r--r-- | src/ft_s_init.c | 50 | ||||
-rw-r--r-- | src/ft_s_init.h | 4 | ||||
-rw-r--r-- | src/ft_s_struct.h | 8 | ||||
-rw-r--r-- | src/ft_u_utils.c | 20 | ||||
-rw-r--r-- | src/ft_u_utils.h | 1 | ||||
-rw-r--r-- | src/minishell.c | 4 |
18 files changed, 223 insertions, 64 deletions
diff --git a/src/ft_b_cd.c b/src/ft_b_cd.c index cbb392a..fbd0282 100644 --- a/src/ft_b_cd.c +++ b/src/ft_b_cd.c @@ -14,27 +14,32 @@ #include <stdint.h> #include <unistd.h> +#include "ft_f_fail.h" +#include "ft_s_destroy.h" #include "ft_s_struct.h" #include "ft_u_utils.h" +#include "ft_u_vars.h" -/* static void */ -/* ft_switch_env_var(char **envp) */ -/* { */ -/* char **ptr; */ -/* char *path; */ - -/* ptr = envp; */ -/* while (*ptr) */ -/* { */ -/* if (ft_strncmp("HOME", *ptr, 4) == 0) */ -/* { */ -/* path = ft_substr(*ptr, 5, ft_strlen(*ptr + 5)); */ -/* return (path); */ -/* } */ -/* ptr++; */ -/* } */ -/* return (NULL); */ -/* } */ +static void + ft_set_path(char **path, + char *args[], + t_msh *msh) +{ + if (!(*path = ft_strdup(*args))) + { + ft_s_destroy(msh); + ft_fail_alloc(msh); + } + if (!ft_strncmp("~/", *path, 2) || !ft_strncmp("~", *path, 2)) + { + if (!(*path = ft_strsubst(*path, + "~", ft_subst_var_value("$HOME", msh)))) + { + ft_s_destroy(msh); + ft_fail_alloc(msh); + } + } +} uint8_t ft_b_cd(char *args[], @@ -43,18 +48,26 @@ uint8_t const uint64_t argc = ft_get_argc((const char**)args); char *path; - if (argc == 0) + if (argc >= 2) + { + ft_fail_too_many_args("cd", msh); + return (1); + } + else if (argc == 0) { - path = ft_get_home_dir(msh->envp); + if (!(path = ft_subst_var_value("$HOME", msh))) + return (1); } + else + ft_set_path(&path, args, msh); if (chdir(path) != 0) { - /* TODO: handle cd failed */ + ft_fail_chd("cd", path, msh); + ft_memdel((void*)&path); + return (1); } - /* TODO: ft_switch_env_var() */ ft_memdel((void*)&msh->cwd); msh->cwd = getcwd(NULL, 0); ft_memdel((void*)&path); - /* TODO: finish cd */ return (0); } diff --git a/src/ft_b_exit.c b/src/ft_b_exit.c index ae421cf..7a7e5e8 100644 --- a/src/ft_b_exit.c +++ b/src/ft_b_exit.c @@ -13,6 +13,8 @@ #include <libft.h> #include <stdlib.h> #include <stdint.h> +#include <unistd.h> + #include "ft_f_fail.h" #include "ft_s_lcom.h" #include "ft_s_destroy.h" @@ -36,13 +38,6 @@ uint8_t ret = ft_atoi(args[0]); /* TODO: non numeric args[0] */ } - else - { - ret = msh->ret; - } - ft_lcom_clear(&msh->curr); - ft_s_destroy(msh); - ft_printf("exit\n"); - exit(ret); + ft_dprintf(STDERR_FILENO, "exit\n"); return (0); } diff --git a/src/ft_e_builtins.c b/src/ft_e_builtins.c index bc3eece..382f85d 100644 --- a/src/ft_e_builtins.c +++ b/src/ft_e_builtins.c @@ -37,13 +37,33 @@ static void static void ft_e_builtin_parent(pid_t pid, + const t_lcom *ptr, + uint8_t bu_id, t_msh *msh) { int32_t status; + int32_t ret; while (wait(&status) != pid) ; - msh->ret = WEXITSTATUS(status); + ret = WEXITSTATUS(status); + if (bu_id != 6) + msh->ret = ret; + if (bu_id == 1 && msh->ret == 0) + { + msh->bu_ptr[bu_id](ptr->argv + 1, msh); + /* TODO: export $PWD */ + } + else if (bu_id == 6 && ret == 0) + { + if (ptr->argv[1]) + ret = ft_atoi(ptr->argv[1]); + else + ret = msh->ret; + ft_lcom_clear(&msh->curr); + ft_s_destroy(msh); + exit(ret); + } } void @@ -64,6 +84,6 @@ void } else { - ft_e_builtin_parent(pid, msh); + ft_e_builtin_parent(pid, ptr, bu_id, msh); } } diff --git a/src/ft_e_lcom.c b/src/ft_e_lcom.c index 3288112..4a57f7b 100644 --- a/src/ft_e_lcom.c +++ b/src/ft_e_lcom.c @@ -11,6 +11,7 @@ /* ************************************************************************** */ #include <libft.h> + #include "ft_e_builtins.h" #include "ft_e_externs.h" #include "ft_s_struct.h" diff --git a/src/ft_f_chdir.c b/src/ft_f_chdir.c new file mode 100644 index 0000000..1386626 --- /dev/null +++ b/src/ft_f_chdir.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_f_chdir.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <string.h> +#include <errno.h> +#include <unistd.h> + +#include "ft_s_struct.h" + +void + ft_fail_chd(const char concern[], + const char path[], + t_msh *msh) +{ + ft_dprintf(STDERR_FILENO, "%s: %s: %s: %s\n", + msh->shname, concern, path, strerror(errno)); +} diff --git a/src/ft_f_chdir.h b/src/ft_f_chdir.h new file mode 100644 index 0000000..fae82d7 --- /dev/null +++ b/src/ft_f_chdir.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_f_chdir.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_F_CHDIR_H +#define FT_F_CHDIR_H + +#include "ft_s_struct.h" + +void ft_fail_chd(const char concern[], + const char pathp[], + t_msh *msh); + +#endif diff --git a/src/ft_f_errno.c b/src/ft_f_errno.c new file mode 100644 index 0000000..22ee7a9 --- /dev/null +++ b/src/ft_f_errno.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_f_errno.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <string.h> +#include <errno.h> +#include <unistd.h> + +#include "ft_s_struct.h" + +void + ft_f_dump_errno(const char concern[], + t_msh *msh) +{ + ft_dprintf(STDERR_FILENO, "%s: %s: %s\n", + msh->shname, concern, strerror(errno)); +} diff --git a/src/ft_f_errno.h b/src/ft_f_errno.h new file mode 100644 index 0000000..86fce83 --- /dev/null +++ b/src/ft_f_errno.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_f_errno.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_F_ERRNO_H +#define FT_F_ERRNO_H + +#include "ft_s_struct.h" + +void ft_f_dump_errno(const char concern[], + t_msh *msh); + +#endif diff --git a/src/ft_f_fail.h b/src/ft_f_fail.h index 5091810..b176cbb 100644 --- a/src/ft_f_fail.h +++ b/src/ft_f_fail.h @@ -13,6 +13,9 @@ #ifndef FT_F_FAIL_H #define FT_F_FAIL_H +#include "ft_f_chdir.h" +#include "ft_f_errno.h" +#include "ft_f_redir.h" #include "ft_s_struct.h" void ft_fail_no_options(const char concern[], diff --git a/src/ft_m_argv.c b/src/ft_m_argv.c index 684d396..7b91fbb 100644 --- a/src/ft_m_argv.c +++ b/src/ft_m_argv.c @@ -20,7 +20,7 @@ uint8_t ft_m_argv(int argc, - const char *argv[], + char *const argv[], t_msh *msh) { /* TODO: better argv handling */ diff --git a/src/ft_m_argv.h b/src/ft_m_argv.h index 2f3260d..783c7e4 100644 --- a/src/ft_m_argv.h +++ b/src/ft_m_argv.h @@ -18,7 +18,7 @@ #include "ft_s_struct.h" uint8_t ft_m_argv(int argc, - const char *argv[], + char *const argv[], t_msh *msh); #endif diff --git a/src/ft_m_redirs.c b/src/ft_m_redirs.c index bdb5b21..701a2a4 100644 --- a/src/ft_m_redirs.c +++ b/src/ft_m_redirs.c @@ -14,7 +14,7 @@ #include <unistd.h> #include <errno.h> -#include "ft_f_redir.h" +#include "ft_f_fail.h" #include "ft_s_destroy.h" #include "ft_s_lcom.h" #include "ft_s_struct.h" diff --git a/src/ft_s_init.c b/src/ft_s_init.c index 46dac90..88a3817 100644 --- a/src/ft_s_init.c +++ b/src/ft_s_init.c @@ -11,6 +11,7 @@ /* ************************************************************************** */ #include <libft.h> +#include <stdint.h> #include <stdlib.h> #include <unistd.h> @@ -18,9 +19,48 @@ #include "ft_m_funptr.h" #include "ft_s_init.h" +static char + **ft_dupenv_del(char **nenvp, + uint64_t i) +{ + while (i > 0) + { + ft_memdel((void*)&nenvp[i]); + i--; + } + ft_memdel((void*)&nenvp); + return (NULL); +} + +static char + **ft_dupenv(char *const envp[]) +{ + uint64_t i; + char **nenvp; + + i = 0; + while (envp[i]) + { + i++; + } + if (!(nenvp = (char**)malloc((i + 1) * sizeof(char*)))) + { + return (NULL); + } + i = 0; + while (envp[i]) + { + if (!(nenvp[i] = ft_strdup(envp[i]))) + return (ft_dupenv_del(nenvp, i)); + i++; + } + nenvp[i] = NULL; + return (nenvp); +} + t_msh - *ft_init_msh(const char *argv[], - char *envp[]) + *ft_init_msh(char *const argv[], + char *const envp[]) { t_msh *msh; @@ -30,12 +70,16 @@ t_msh return (NULL); if (!(msh->shname = ft_strdup(argv[0]))) return (NULL); + /* TODO: shname: care about "./", try with symlinks */ msh->cwd = NULL; msh->cwd = getcwd(NULL, 0); /* TODO: handle getcwd failed */ - msh->envp = envp; + msh->envp = NULL; + if (!(msh->envp = ft_dupenv(envp))) + return (NULL); msh->ret = 0; ft_init_buptr(msh); msh->curr = NULL; + msh->vars = NULL; return (msh); } diff --git a/src/ft_s_init.h b/src/ft_s_init.h index e860925..bd8edc7 100644 --- a/src/ft_s_init.h +++ b/src/ft_s_init.h @@ -16,7 +16,7 @@ #include <stdlib.h> #include "ft_s_struct.h" -t_msh *ft_init_msh(const char *argv[], - char *envp[]); +t_msh *ft_init_msh(char *const argv[], + char *const envp[]); #endif diff --git a/src/ft_s_struct.h b/src/ft_s_struct.h index 38eeb43..5b99acb 100644 --- a/src/ft_s_struct.h +++ b/src/ft_s_struct.h @@ -26,6 +26,13 @@ ** 0: means no redirection */ +typedef struct s_vars +{ + char *name; + char *val; + struct s_vars *next; +} t_vars; + typedef struct s_lcom { char *com; @@ -46,6 +53,7 @@ typedef struct s_msh char **bu_ref; uint8_t (*bu_ptr[FT_BUILTINS_COUNT])(char **, struct s_msh*); struct s_lcom *curr; + struct s_vars *vars; } t_msh; #endif diff --git a/src/ft_u_utils.c b/src/ft_u_utils.c index 7e2eb53..b7ab7d8 100644 --- a/src/ft_u_utils.c +++ b/src/ft_u_utils.c @@ -26,23 +26,3 @@ uint64_t } return (argc); } - -char - *ft_get_home_dir(char **envp) -{ - char **ptr; - char *path; - - ptr = envp; - while (*ptr) - { - /* TODO: rework this correctly */ - if (ft_strncmp("HOME", *ptr, 4) == 0) - { - path = ft_substr(*ptr, 5, ft_strlen(*ptr + 5)); - return (path); - } - ptr++; - } - return (NULL); -} diff --git a/src/ft_u_utils.h b/src/ft_u_utils.h index d025cb2..1a3b324 100644 --- a/src/ft_u_utils.h +++ b/src/ft_u_utils.h @@ -16,6 +16,5 @@ #include <stdint.h> uint64_t ft_get_argc(const char *args[]); -char *ft_get_home_dir(char *envp[]); #endif diff --git a/src/minishell.c b/src/minishell.c index ce5dd30..0e8dbdd 100644 --- a/src/minishell.c +++ b/src/minishell.c @@ -24,8 +24,8 @@ int main(int argc, - const char *argv[], - char *envp[]) + char *const argv[], + char *const envp[]) { t_msh *msh; int32_t ret; |