summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-08-04 17:54:15 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-08-04 17:54:15 +0200
commitf2924d712bfb49037763f232e0132d78334f4219 (patch)
treec184b2cd238c53b0293f29f69ed948931035ed59 /src
parentAdded -P to cwd (diff)
download42-minishell-f2924d712bfb49037763f232e0132d78334f4219.tar.gz
42-minishell-f2924d712bfb49037763f232e0132d78334f4219.tar.bz2
42-minishell-f2924d712bfb49037763f232e0132d78334f4219.tar.xz
42-minishell-f2924d712bfb49037763f232e0132d78334f4219.tar.zst
42-minishell-f2924d712bfb49037763f232e0132d78334f4219.zip
FeelsGoodMan
Diffstat (limited to '')
-rw-r--r--src/b_cd.c45
-rw-r--r--src/b_pwd.c15
-rw-r--r--src/s_destroy.c1
-rw-r--r--src/s_init.c3
-rw-r--r--src/s_struct.h1
5 files changed, 55 insertions, 10 deletions
diff --git a/src/b_cd.c b/src/b_cd.c
index c4b5c1b..07410f8 100644
--- a/src/b_cd.c
+++ b/src/b_cd.c
@@ -19,6 +19,7 @@
#include "s_struct.h"
#include "u_utils.h"
#include "u_vars.h"
+#include "u_vars_next.h"
static void
set_path(char **path,
@@ -41,6 +42,49 @@ static void
}
}
+static void
+ b_upgrade_pwd(const char path[], t_msh *msh)
+{
+ char **splited;
+ char *rtmp;
+ char repath[131072];
+ size_t i;
+ size_t j;
+
+ if (path[0] == '/')
+ {
+ u_subst_var_value("$PWD", path, msh);
+ /* TODO: /home///////////////////////////////////jozan////////////// */
+ return ;
+ }
+ rtmp = u_get_var_value("$PWD", msh);
+ ft_memcpy(repath, rtmp, ft_strlen(rtmp) + 1);
+ ft_memdel((void*)&rtmp);
+ splited = ft_split(path, '/');
+ i = 0;
+ while (splited[i] != '\0')
+ {
+ ft_printf("{%s}\n{%s}\n", path, path + i);
+ if (splited[i][0] == '.' && splited[i][1] == '.')
+ {
+ j = ft_strlen(repath);
+ ft_printf("[%s]\n", repath);
+ while (repath[j] != '/')
+ j--;
+ repath[j] = '\0';
+ }
+ else
+ {
+ j = ft_strlen(repath) + 1;
+ repath[j - 1] = '/';
+ ft_strlcpy(repath + j, splited[i], ft_strlen(splited[i]) + 1);
+ }
+ i++;
+ }
+ ft_delwords(splited);
+ u_subst_var_value("$PWD", repath, msh);
+}
+
uint8_t
b_cd(char *args[],
t_msh *msh)
@@ -66,6 +110,7 @@ uint8_t
ft_memdel((void*)&path);
return (1);
}
+ b_upgrade_pwd(path, msh);
ft_memdel((void*)&path);
return (0);
}
diff --git a/src/b_pwd.c b/src/b_pwd.c
index ac8d7d0..2b87091 100644
--- a/src/b_pwd.c
+++ b/src/b_pwd.c
@@ -15,20 +15,25 @@
#include <unistd.h>
#include "s_struct.h"
+#include "u_vars.h"
+#include "u_vars_next.h"
uint8_t
b_pwd(char *args[],
t_msh *msh)
{
- char *tmp;
+ char *cwd;
if (args[0] != NULL && ft_strncmp(args[0], "-P", 3) == 0)
{
- tmp = getcwd(NULL, 0);
- ft_printf("%s\n", tmp);
- ft_memdel((void*)&tmp);
+ cwd = getcwd(NULL, 0);
+ ft_printf("%s\n", cwd);
+ ft_memdel((void*)&cwd);
return (0);
}
- ft_printf("%s\n", msh->cwd);
+ if ((cwd = u_get_var_value("$PWD", msh)) == NULL)
+ cwd = getcwd(NULL, 0);
+ ft_printf("%s\n", cwd);
+ ft_memdel((void*)&cwd);
return (0);
}
diff --git a/src/s_destroy.c b/src/s_destroy.c
index f9d56eb..c96db09 100644
--- a/src/s_destroy.c
+++ b/src/s_destroy.c
@@ -19,7 +19,6 @@ void
s_destroy(t_msh *msh)
{
ft_memdel((void*)&msh->ps_one);
- ft_memdel((void*)&msh->cwd);
ft_memdel((void*)&msh->shname);
ft_delwords(msh->bu_ref);
ft_delwords(msh->envp);
diff --git a/src/s_init.c b/src/s_init.c
index cd03e72..54be62f 100644
--- a/src/s_init.c
+++ b/src/s_init.c
@@ -93,9 +93,6 @@ t_msh
init_buptr(msh);
msh->curr = NULL;
msh->vars = NULL;
- msh->cwd = NULL;
- if ((msh->cwd = u_get_var_value("$PWD", msh)) == NULL)
- msh->cwd = getcwd(NULL, 0);
inc_shlvl(msh);
return (msh);
}
diff --git a/src/s_struct.h b/src/s_struct.h
index 8c72012..f798ed7 100644
--- a/src/s_struct.h
+++ b/src/s_struct.h
@@ -56,7 +56,6 @@ typedef struct s_msh
{
char **envp;
char *ps_one;
- char *cwd;
uint8_t ret;
char *shname;
char **bu_ref;