minishell/srcs/ft_realpath.c
Tanguy MAZE 9a1bc7b613 WIP cd builtin
need to implement -P and -L (default) options
2019-01-28 17:00:16 +01:00

70 lines
1.9 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_realpath.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/27 14:42:35 by tmaze #+# #+# */
/* Updated: 2019/01/28 15:32:44 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char *ft_tabtopath(char **tab_path)
{
size_t total_size;
size_t i;
char *ret;
i = 0;
total_size = 0;
while (tab_path[i])
total_size += ft_strlen(tab_path[i++]);
if ((ret = ft_strnew(total_size + i)) == NULL)
return (NULL);
i = 0;
while (tab_path[i])
{
ft_strcat(ret, "/");
ft_strcat(ret, tab_path[i++]);
}
return (ret);
}
char *ft_realpath(char *r_path, char **a_path)
{
char **tab_path;
size_t i;
size_t j;
int strcmp;
i = 0;
if ((tab_path = ft_strsplit(r_path, '/')) == NULL)
return (NULL);
while (tab_path[i])
{
if ((strcmp = ft_strcmp(tab_path[i], "..")) == 0
|| ft_strcmp(tab_path[i], ".") == 0)
{
j = 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 - ((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_words_tables(&tab_path);
return (*a_path);
}