diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-04-28 16:05:07 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-04-28 16:05:07 +0200 |
commit | 8b6c386883878eec839103305b20cb4d63eef873 (patch) | |
tree | 0bc4c1bcbf67c77a66691bd9f5bc968e953d55cd /src/ft_s_init.c | |
parent | exit is fine (diff) | |
download | 42-minishell-8b6c386883878eec839103305b20cb4d63eef873.tar.gz 42-minishell-8b6c386883878eec839103305b20cb4d63eef873.tar.bz2 42-minishell-8b6c386883878eec839103305b20cb4d63eef873.tar.xz 42-minishell-8b6c386883878eec839103305b20cb4d63eef873.tar.zst 42-minishell-8b6c386883878eec839103305b20cb4d63eef873.zip |
duped completely envp
Diffstat (limited to '')
-rw-r--r-- | src/ft_s_init.c | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/src/ft_s_init.c b/src/ft_s_init.c index 76bf72d..463cb0b 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,6 +19,45 @@ #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 *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[]) @@ -34,7 +74,9 @@ t_msh 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; |