summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/b_cd.c75
-rw-r--r--src/s_destroy.c1
-rw-r--r--src/s_init.c32
-rw-r--r--src/s_struct.h1
4 files changed, 109 insertions, 0 deletions
diff --git a/src/b_cd.c b/src/b_cd.c
index 07410f8..8bd1d2f 100644
--- a/src/b_cd.c
+++ b/src/b_cd.c
@@ -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;
diff --git a/src/s_destroy.c b/src/s_destroy.c
index c96db09..a6f84e3 100644
--- a/src/s_destroy.c
+++ b/src/s_destroy.c
@@ -20,6 +20,7 @@ void
{
ft_memdel((void*)&msh->ps_one);
ft_memdel((void*)&msh->shname);
+ ft_memdel((void*)&msh->cwd);
ft_delwords(msh->bu_ref);
ft_delwords(msh->envp);
lvars_clear(&msh->vars);
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);
}
diff --git a/src/s_struct.h b/src/s_struct.h
index f798ed7..0c5acf0 100644
--- a/src/s_struct.h
+++ b/src/s_struct.h
@@ -59,6 +59,7 @@ typedef struct s_msh
uint8_t ret;
char *shname;
char **bu_ref;
+ char *cwd;
uint8_t (*bu_ptr[FT_BUILTINS_COUNT])(char **, struct s_msh*);
struct s_lcom *curr;
struct s_lvars *vars;