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> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/18 13:12:34 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/07 16:44:40 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]) while (tab_path[i])
{ {
ft_strcat(ret, "/"); ft_strcat(ret, "/");
ft_strcat(ret, tab_path[i]); ft_strcat(ret, tab_path[i++]);
} }
return (ret); return (ret);
} }
@ -75,28 +75,27 @@ char *ft_realpath(char *r_path, char **a_path)
char **tab_path; char **tab_path;
size_t i; size_t i;
size_t j; size_t j;
int strcmp;
i = 0;
*a_path = NULL; *a_path = NULL;
if ((tab_path = ft_strsplit(r_path, '/')) == NULL) if ((tab_path = ft_strsplit(r_path, '/')) == NULL)
return (*a_path); return (*a_path);
while (tab_path[i]) 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
j = i; || (strcmp == 0 && i == 0))
ft_strdel(tab_path[i]);
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; j = i;
if (strcmp == 0 && i != 0)
i = i - 1; i = i - 1;
ft_strdel(tab_path[i]); ft_strdel(&(tab_path[i]));
ft_strdel(tab_path[j]); if (strcmp == 0 && i != 0)
ft_strdel(&(tab_path[j]));
while (tab_path[++j]) while (tab_path[++j])
tab_path[j - 2] = tab_path[j]; 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 - 2] = NULL;
tab_path[j - 1] = NULL; tab_path[j - 1] = NULL;
} }
@ -104,7 +103,7 @@ char *ft_realpath(char *r_path, char **a_path)
i++; i++;
} }
*a_path = ft_tabtopath(tab_path); *a_path = ft_tabtopath(tab_path);
ft_del_tables_words(&tab_path); ft_del_words_tables(&tab_path);
return (*a_path); return (*a_path);
} }
@ -190,13 +189,26 @@ int cmd_cd(char **argv, t_list **env)
ft_strcpy(tmp, elem->val); ft_strcpy(tmp, elem->val);
tmp[ft_strlen(tmp)] = '/'; tmp[ft_strlen(tmp)] = '/';
ft_strcat(tmp, argv[1]); ft_strcat(tmp, argv[1]);
ft_putendl(tmp);
if (ft_realpath(tmp, &path) == NULL) if (ft_realpath(tmp, &path) == NULL)
put_error_cd(argv[1], "memory error"); 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); ret = cmd_cd_core(path, env);
ft_strdel(&path); ft_strdel(&path);
return (ret); return (ret);
} }
return (0); return (1);
} }

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/13 14:44:20 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */