diff options
-rw-r--r-- | src/b_cd.c | 45 | ||||
-rw-r--r-- | src/b_pwd.c | 15 | ||||
-rw-r--r-- | src/s_destroy.c | 1 | ||||
-rw-r--r-- | src/s_init.c | 3 | ||||
-rw-r--r-- | src/s_struct.h | 1 |
5 files changed, 55 insertions, 10 deletions
@@ -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; |