diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-08-05 19:59:43 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-08-05 19:59:43 +0200 |
commit | 1ec7baa32438f58990f813e8659b52d3bb8aa743 (patch) | |
tree | 0d65604d426c78479cffe023a57604bb10b1d6ba /src/b_cd.c | |
parent | FeelsGoodMan (diff) | |
download | 42-minishell-1ec7baa32438f58990f813e8659b52d3bb8aa743.tar.gz 42-minishell-1ec7baa32438f58990f813e8659b52d3bb8aa743.tar.bz2 42-minishell-1ec7baa32438f58990f813e8659b52d3bb8aa743.tar.xz 42-minishell-1ec7baa32438f58990f813e8659b52d3bb8aa743.tar.zst 42-minishell-1ec7baa32438f58990f813e8659b52d3bb8aa743.zip |
Fixed $PWD at startup
Diffstat (limited to 'src/b_cd.c')
-rw-r--r-- | src/b_cd.c | 75 |
1 files changed, 75 insertions, 0 deletions
@@ -13,7 +13,9 @@ #include <libft.h> #include <stdint.h> #include <unistd.h> +#include <limits.h> +#include "b_export_next.h" #include "f_fail.h" #include "s_destroy.h" #include "s_struct.h" @@ -43,9 +45,82 @@ static void } static void + b_set_oldpwd(t_msh *msh) +{ + char *pwd; + char *tmp; + char fmt[PATH_MAX]; + + if ((pwd = u_get_var_value("$PWD", msh)) == NULL) + { + if ((pwd = ft_strdup(msh->cwd)) == NULL) + f_fail_alloc_and_destroy(msh); + } + if ((tmp = u_get_var_value("$OLDPWD", msh)) == NULL) + { + ft_sprintf(fmt, "%s=%s", "OLDPWD", pwd); + b_export_with_equals(fmt, msh); + } + else + { + u_subst_var_value("$OLDPWD", pwd, msh); + ft_memdel((void*)&pwd); + ft_memdel((void*)&tmp); + } +} + +static void + b_fill_repath(char *repath[], + char *splited[]) +{ + size_t i; + size_t j; + + i = 0; + while (splited[i] != NULL) + { + if (ft_strncmp(splited[i], "..", 3) == 0) + { + j = ft_strlen(*repath); + while (*repath[j] != '/') + j--; + *repath[j] = '\0'; + } + i++; + } +} + +static void b_upgrade_pwd(const char path[], t_msh *msh) { char **splited; + char repath[262144]; + t_bool slash_first; + + b_set_oldpwd(msh); + slash_first = FALSE; + if (path[0] == '/') + slash_first = TRUE; + if (slash_first == TRUE) + ft_memcpy(repath, "/", 2); + else + { + /* msh->cwd; */ + } + if ((splited = ft_split(path, '/')) == NULL) + f_fail_alloc_and_destroy(msh); + b_fill_repath((char**)&repath, splited); + ft_delwords(splited); + u_subst_var_value("$PWD", repath, msh); + ft_printf("[%s]\n", repath); +} + +/* TODO: fix export OLDPWDQWE= */ + +static void + b_old_upgrade_pwd(const char path[], t_msh *msh) +{ + char **splited; char *rtmp; char repath[131072]; size_t i; |