WIP cd fonctionnalities

missing realpath() application to paths beginning by /
This commit is contained in:
Tanguy MAZE 2019-01-25 17:04:00 +01:00
parent 2fee92fc03
commit d619a7f3c1
3 changed files with 39 additions and 27 deletions

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)