From 1ec7baa32438f58990f813e8659b52d3bb8aa743 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 5 Aug 2020 19:59:43 +0200 Subject: Fixed $PWD at startup --- src/s_init.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src/s_init.c') diff --git a/src/s_init.c b/src/s_init.c index 54be62f..1dbbbb7 100644 --- a/src/s_init.c +++ b/src/s_init.c @@ -14,7 +14,11 @@ #include #include #include +#include +#include +#include +#include "b_export_next.h" #include "d_define.h" #include "m_funptr.h" #include "s_init.h" @@ -73,6 +77,33 @@ static void ft_memdel((void*)&str_two); } +static char + *set_cwd(t_msh *msh) +{ + char *cwd; + char fmt[PATH_MAX]; + DIR *dir; + + if ((cwd = u_get_var_value("$PWD", msh)) == NULL) + { + cwd = getcwd(NULL, 0); + ft_sprintf(fmt, "%s=%s", "PWD", cwd); + b_export_with_equals(fmt, msh); + return (cwd); + } + if ((dir = opendir(cwd)) != 0) + closedir(dir); + else if (errno == ENOENT) + { + ft_memdel((void*)&cwd); + cwd = getcwd(NULL, 0); + ft_sprintf(fmt, "%s=%s", "PWD", cwd); + b_export_with_equals(fmt, msh); + return (cwd); + } + return (cwd); +} + t_msh *init_msh(char *const argv[], char *const envp[]) @@ -93,6 +124,7 @@ t_msh init_buptr(msh); msh->curr = NULL; msh->vars = NULL; + msh->cwd = set_cwd(msh); inc_shlvl(msh); return (msh); } -- cgit v1.2.3