diff --git a/includes/minishell.h b/includes/minishell.h index 2149d52..21d7ccc 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2018/11/18 13:12:34 by tmaze #+# #+# */ -/* Updated: 2019/01/21 13:14:47 by tmaze ### ########.fr */ +/* Updated: 2019/01/25 16:25:35 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,8 +43,8 @@ int cmd_env(char **argv, t_list **env); int cmd_setenv(char **argv, t_list **env); int cmd_unsetenv(char **argv, t_list **env); -int ft_isin(char *str, char c); -char *check_path_slash(char *exec); -char *check_path_dot(char *exec, t_list *env); +int ft_isin(char *str, char c); +char *check_path_slash(char *exec); +char *check_path_dot(char *exec, t_list *env); #endif diff --git a/srcs/cmd_cd.c b/srcs/cmd_cd.c index d17e26c..5bcff10 100644 --- a/srcs/cmd_cd.c +++ b/srcs/cmd_cd.c @@ -6,7 +6,7 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/01/07 16:44:40 by tmaze #+# #+# */ -/* Updated: 2019/01/24 17:26:24 by tmaze ### ########.fr */ +/* Updated: 2019/01/25 16:36:51 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,7 +65,7 @@ char *ft_tabtopath(char **tab_path) while (tab_path[i]) { ft_strcat(ret, "/"); - ft_strcat(ret, tab_path[i]); + ft_strcat(ret, tab_path[i++]); } return (ret); } @@ -75,36 +75,35 @@ char *ft_realpath(char *r_path, char **a_path) char **tab_path; size_t i; size_t j; + int strcmp; + i = 0; *a_path = NULL; if ((tab_path = ft_strsplit(r_path, '/')) == NULL) return (*a_path); while (tab_path[i]) { - if (tab_path[i] == '.' || (ft_strcmp(tab_path[i], "..") == 0 && i == 1)) + if (ft_strcmp(tab_path[i], ".") == 0 + || (strcmp = ft_strcmp(tab_path[i], "..")) == 0 + || (strcmp == 0 && i == 0)) { j = i; - ft_strdel(tab_path[i]); + if (strcmp == 0 && i != 0) + i = i - 1; + ft_strdel(&(tab_path[i])); + if (strcmp == 0 && i != 0) + ft_strdel(&(tab_path[j])); while (tab_path[++j]) - tab_path[j - 1] = tab_path[j]; - tab_path[j - 1] = NULL; - } - else if (ft_strcmp(tab_path[i], "..") == 0) - { - j = i; - i = i - 1; - ft_strdel(tab_path[i]); - ft_strdel(tab_path[j]); - while (tab_path[++j]) - tab_path[j - 2] = tab_path[j]; - tab_path[j - 2] = NULL; + tab_path[j - (strcmp == 0 && i != 0) ? 2 : 1)] = tab_path[j]; + if (strcmp == 0 && i != 0) + tab_path[j - 2] = NULL; tab_path[j - 1] = NULL; } else i++; } *a_path = ft_tabtopath(tab_path); - ft_del_tables_words(&tab_path); + ft_del_words_tables(&tab_path); return (*a_path); } @@ -112,7 +111,7 @@ int cmd_cd_core(char *path, t_list **env) { t_envelem *pwd; char *oldpwd; - + if (check_path_slash_cd(path) == NULL) { put_error_cd(path, "invalid path"); @@ -190,13 +189,26 @@ int cmd_cd(char **argv, t_list **env) ft_strcpy(tmp, elem->val); tmp[ft_strlen(tmp)] = '/'; ft_strcat(tmp, argv[1]); - ft_putendl(tmp); if (ft_realpath(tmp, &path) == NULL) put_error_cd(argv[1], "memory error"); - + ret = cmd_cd_core(path, env); + ft_strdel(&tmp); + ft_strdel(&path); + return (ret); + } + else if (argv[1] && (elem = env_getelemfromkey("PWD", *env)) != NULL && elem->val != NULL) + { + if ((path = ft_strnew(ft_strlen(elem->val) + ft_strlen(argv[1]) + 1)) == NULL) + put_error_cd(argv[1], "memory error"); + if (path == NULL) + return (1); + ft_strcpy(path, elem->val); + path[ft_strlen(path)] = '/'; + ft_strcat(path, argv[1]); + ft_putendl(path); ret = cmd_cd_core(path, env); ft_strdel(&path); return (ret); } - return (0); + return (1); } diff --git a/srcs/ms_env.c b/srcs/ms_env.c index c90fac8..eec3261 100644 --- a/srcs/ms_env.c +++ b/srcs/ms_env.c @@ -6,7 +6,7 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/01/13 14:44:20 by tmaze #+# #+# */ -/* Updated: 2019/01/13 19:02:04 by tmaze ### ########.fr */ +/* Updated: 2019/01/25 16:05:29 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,7 +52,7 @@ t_envelem *env_addupdate(char *key, char *val, t_list **env) t_list *new; t_envelem *elem; char *tmp; - + if ((elem = env_getelemfromkey(key, *env)) != NULL) { if (val == NULL)