diff options
Diffstat (limited to '')
-rw-r--r-- | src/s_init.c | 32 |
1 files changed, 32 insertions, 0 deletions
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 <stdint.h> #include <stdlib.h> #include <unistd.h> +#include <limits.h> +#include <dirent.h> +#include <errno.h> +#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); } |