summaryrefslogtreecommitdiffstats
path: root/src/s_init.c
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-08-05 19:59:43 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-08-05 19:59:43 +0200
commit1ec7baa32438f58990f813e8659b52d3bb8aa743 (patch)
tree0d65604d426c78479cffe023a57604bb10b1d6ba /src/s_init.c
parentFeelsGoodMan (diff)
download42-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 '')
-rw-r--r--src/s_init.c32
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);
}