a bit of cleaning

This commit is contained in:
Tanguy MAZE 2019-01-27 17:38:59 +01:00
parent 98807eb6f8
commit 784e71d719
4 changed files with 83 additions and 67 deletions

View File

@ -6,7 +6,7 @@
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2018/11/22 14:43:33 by tmaze #+# #+# #
# Updated: 2019/01/15 15:11:04 by tmaze ### ########.fr #
# Updated: 2019/01/27 15:09:17 by tmaze ### ########.fr #
# #
#******************************************************************************#
@ -16,7 +16,7 @@ CCSTD :=
NAME := minishell
SRCS := main.c exec.c cmd_echo.c cmd_cd.c cmd_env.c cmd_setenv.c cmd_unsetenv.c ms_env.c
SRCS := main.c exec.c cmd_echo.c cmd_cd.c cmd_env.c cmd_setenv.c cmd_unsetenv.c ms_env.c ft_realpath.c
OBJS_DIR := objs
OBJS := $(SRCS:.c=.o)
INCLS := -Iincludes -Ilibft

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/18 13:12:34 by tmaze #+# #+# */
/* Updated: 2019/01/25 16:25:35 by tmaze ### ########.fr */
/* Updated: 2019/01/27 14:43:56 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
@ -47,4 +47,6 @@ int ft_isin(char *str, char c);
char *check_path_slash(char *exec);
char *check_path_dot(char *exec, t_list *env);
char *ft_realpath(char *r_path, char **a_path);
#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/26 16:58:39 by tmaze ### ########.fr */
/* Updated: 2019/01/27 16:32:01 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
@ -49,64 +49,6 @@ char *check_path_slash_cd(char *exec)
return (NULL);
}
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;
*a_path = NULL;
if ((tab_path = ft_strsplit(r_path, '/')) == NULL)
return (*a_path);
while (tab_path[i])
{
if (ft_strcmp(tab_path[i], ".") == 0
|| (strcmp = ft_strcmp(tab_path[i], "..")) == 0
|| (strcmp == 0 && 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);
}
int cmd_cd_core(char *path, t_list **env)
{
t_envelem *pwd;
@ -166,7 +108,7 @@ int ft_isfstcmp(char *path, char *cmp)
{
size_t i;
i == 0;
i = 0;
while (path[i] == '/')
i++;
return (ft_strncmp(&path[i], cmp, ft_strlen(cmp)));
@ -181,13 +123,14 @@ int cmd_cd2(char **argv, t_list **env)
size_t i;
i = 0;
if ((opt = cf_getparams(argv, &i)) == '\0')
path = NULL;
if ((opt = cd_getparams(argv, &i)) == '\0')
return (1);
if (!argv[i] && (elem = env_getelemfromkey("HOME", *env)) != NULL && elem->val != NULL)
tmp = elem->val;
else if (argv[i] && argv[i][0] != ft_isfstcmp(argv[i]) == 0 && (elem = env_getelemfromkey("PWD", *env)) != NULL && elem->val != NULL)
else if (argv[i] && argv[i][0] == '.' && (elem = env_getelemfromkey("PWD", *env)) != NULL && elem->val != NULL)
{
if ((tmp = ft_strnew(ft_strlen(argv[i]) + ft_strelen(elem->val) + 1)) == NULL)
if ((tmp = ft_strnew(ft_strlen(argv[i]) + ft_strlen(elem->val) + 1)) == NULL)
put_error_cd(argv[i], "memory error");
if (tmp == NULL)
return (1);
@ -195,6 +138,8 @@ int cmd_cd2(char **argv, t_list **env)
ft_strcat(tmp, "/");
ft_strcat(tmp, argv[i]);
}
else if ()
return (0);
}
int cmd_cd(char **argv, t_list **env)

69
srcs/ft_realpath.c Normal file
View File

@ -0,0 +1,69 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_realpath.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/27 14:42:35 by tmaze #+# #+# */
/* Updated: 2019/01/27 15:35:06 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 || (strcmp == 0 && 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);
}