From 784e71d719bf814d52f62ab64ea09a4df1fbb13b Mon Sep 17 00:00:00 2001 From: Tanguy MAZE Date: Sun, 27 Jan 2019 17:38:59 +0100 Subject: [PATCH] a bit of cleaning --- Makefile | 4 +-- includes/minishell.h | 4 ++- srcs/cmd_cd.c | 73 ++++++-------------------------------------- srcs/ft_realpath.c | 69 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 67 deletions(-) create mode 100644 srcs/ft_realpath.c diff --git a/Makefile b/Makefile index 5ec5d12..a894437 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: tmaze +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # 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 diff --git a/includes/minishell.h b/includes/minishell.h index 21d7ccc..7ba9f41 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 diff --git a/srcs/cmd_cd.c b/srcs/cmd_cd.c index c342976..c5536e3 100644 --- a/srcs/cmd_cd.c +++ b/srcs/cmd_cd.c @@ -6,7 +6,7 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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; @@ -156,7 +98,7 @@ char cd_getparams(char **argv, size_t *i) put_error_cd(&ret, "invalid option"); put_error_cd("usage", "cd [-L|-P] [dir]"); ret = '\0'; - break; + break ; } } return (ret); @@ -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) diff --git a/srcs/ft_realpath.c b/srcs/ft_realpath.c new file mode 100644 index 0000000..57383b2 --- /dev/null +++ b/srcs/ft_realpath.c @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_realpath.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +}