* Restart from scratch branched out on dev removed all previous source files started test on new env2lst function * norm: splitted functions in main into appropriate file Moved lstdelenvelem & env2lst to ms_env.c Normed out files * Feat(WIP): Added possibility to cmd_env to execute commands with altered env. Added dummy function for cd, echo, setenv & unsetenv Started work on env copy feature * feature: switched env from ft_list to specific list * feature: added execution of commands from path * feature: added env, setenv & unsetenv builtins added -i option & command execution to env added setenv & unsetenv builtins * Clean-up: normed out files * Changed comportment on error for check_path functions * feature: added completion for ~ and ${} WIP leaks detected on ${} completion * fix leak on ext need test * feature: added echo cmd changed printf ref to ft_printf * feature: add cd built-in * Simple norm-out * moved env functions to libft * fixed out-of-memory access in extension * Resolved infinite loop in extension revolving WIP norming in cd * Normedout cmd_cd.c * Normed out functions in cmd_cd * Normed out funtions sorting out needed * removed -fsanitize from Makefile * corrected env -i crash & SHLVL * Delete norm.txt * added put_error_cd2 in put_error_cd.c * added inline environment
78 lines
2.1 KiB
C
78 lines
2.1 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_realpath.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2019/01/27 14:42:35 by tmaze #+# #+# */
|
|
/* Updated: 2019/01/29 18:16:22 by tmaze ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "libft.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++]);
|
|
total_size = (total_size == 0) ? 1 : total_size;
|
|
if ((ret = ft_strnew(total_size + i)) == NULL)
|
|
return (NULL);
|
|
i = 0;
|
|
ft_strcat(ret, "/");
|
|
while (tab_path[i])
|
|
{
|
|
if (i > 0)
|
|
ft_strcat(ret, "/");
|
|
ft_strcat(ret, tab_path[i++]);
|
|
}
|
|
return (ret);
|
|
}
|
|
|
|
void ft_reducepath(char ***tab)
|
|
{
|
|
size_t i;
|
|
size_t j;
|
|
int strcmp;
|
|
|
|
i = 0;
|
|
while ((*tab)[i])
|
|
{
|
|
if ((strcmp = ft_strcmp((*tab)[i], "..")) == 0
|
|
|| ft_strcmp((*tab)[i], ".") == 0)
|
|
{
|
|
ft_strdel(&((*tab)[i]));
|
|
if ((j = i) == i && strcmp == 0 && i != 0)
|
|
ft_strdel(&((*tab)[i - 1]));
|
|
while ((*tab)[++j])
|
|
(*tab)[j - ((strcmp == 0 && i != 0) ? 2 : 1)] = (*tab)[j];
|
|
if (strcmp == 0 && i != 0)
|
|
(*tab)[j - 2] = NULL;
|
|
(*tab)[j - 1] = NULL;
|
|
if (strcmp == 0 && i != 0)
|
|
i--;
|
|
}
|
|
else
|
|
i++;
|
|
}
|
|
}
|
|
|
|
char *ft_realpath(char *r_path, char **a_path)
|
|
{
|
|
char **tab_path;
|
|
|
|
if ((tab_path = ft_strsplit(r_path, '/')) == NULL)
|
|
return (NULL);
|
|
ft_reducepath(&tab_path);
|
|
*a_path = ft_tabtopath(tab_path);
|
|
ft_del_words_tables(&tab_path);
|
|
return (*a_path);
|
|
}
|