From 4238485a4e26a9d15541708bfc38bfede9bbe7d2 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Wed, 9 Sep 2020 15:52:34 +0200 Subject: Moving utils_directory_iter back to path.c, abstraction not needed without globs --- include/minishell.h | 10 +--------- libft | 2 +- minishell_test | 2 +- src/path.c | 28 ++++++++++++++++++---------- src/utils.c | 23 +---------------------- 5 files changed, 22 insertions(+), 43 deletions(-) diff --git a/include/minishell.h b/include/minishell.h index 573ac29..085f082 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/26 15:33:51 by cacharle #+# #+# */ -/* Updated: 2020/09/09 14:04:00 by charles ### ########.fr */ +/* Updated: 2020/09/09 15:42:03 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -125,14 +125,6 @@ void signal_sigterm(int signum); ** utils.c */ -typedef int (*t_directory_iter_func)(char*, struct dirent*, void*); - -int utils_directory_iter( - char *dirname, - void *param, - t_directory_iter_func f -); - size_t utils_var_end(char *name); bool utils_valid_identifier(char *name); bool utils_start_with_valid_identifier(char *name); diff --git a/libft b/libft index d3cf16e..50876fe 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit d3cf16e078e92ab85e5ad2f30f2c07ce382fd98e +Subproject commit 50876fe6b9e369d6b51bac9fa62b790ef5bda9d7 diff --git a/minishell_test b/minishell_test index fb3df18..f571ffc 160000 --- a/minishell_test +++ b/minishell_test @@ -1 +1 @@ -Subproject commit fb3df181b71d3bed23f43ea4a6fcd8111d4c8690 +Subproject commit f571ffc6beb34cf0d4a171e5b5aa70a1d2b83658 diff --git a/src/path.c b/src/path.c index 1234998..0de0bad 100644 --- a/src/path.c +++ b/src/path.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/27 15:51:01 by cacharle #+# #+# */ -/* Updated: 2020/07/17 10:48:31 by charles ### ########.fr */ +/* Updated: 2020/09/09 15:49:50 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,19 +28,27 @@ ** in the directory named `dirname`. ** \param path Path hash table ** \param dirname directory name -** \return Same path or NULL on error +** \return -1 on error, 0 or -2 otherwise */ -static int st_add_file(char *dirname, struct dirent *entry, void *path) +static int st_add_directory(t_path path, char *dirname) { - char *filepath; + DIR *dir; + struct dirent *entry; - if ((filepath = ft_strjoin3(dirname, "/", entry->d_name)) == NULL || - ft_htset((t_path)path, entry->d_name, filepath, free) == NULL) + if ((dir = opendir(dirname)) == NULL) + return (-2); + while ((entry = readdir(dir)) != NULL) { - free(filepath); - return (-1); + if (ft_htset_safe(path, entry->d_name, + ft_strjoin3(dirname, "/", entry->d_name), free) == NULL) + { + closedir(dir); + return (-1); + } } + if (closedir(dir) == -1) + return (-1); return (0); } @@ -66,8 +74,8 @@ t_path path_update(t_path path, char *path_var) if ((dirs = ft_split(path_var, ':')) == NULL) return (NULL); i = -1; - while (dirs[++i] != NULL) // carefull with non existant dir error - if (utils_directory_iter(dirs[i], path, st_add_file) == -1) + while (dirs[++i] != NULL) + if (st_add_directory(path, dirs[i]) == -1) return (ft_split_destroy(dirs)); ft_split_destroy(dirs); return (path); diff --git a/src/utils.c b/src/utils.c index d511195..55c190b 100644 --- a/src/utils.c +++ b/src/utils.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:56:31 by cacharle #+# #+# */ -/* Updated: 2020/09/09 14:05:53 by charles ### ########.fr */ +/* Updated: 2020/09/09 15:41:04 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,27 +17,6 @@ #include "minishell.h" -int utils_directory_iter( - char *dirname, - void *param, - int (*f)(char*, struct dirent*, void*) -) -{ - DIR *dir; - struct dirent *entry; - - if ((dir = opendir(dirname)) == NULL) // EACCES and Not a directory with glob are ignored by bash - return (-2); - while ((entry = readdir(dir)) != NULL) - if (f(dirname, entry, param) == -1) - { - closedir(dir); - return (-1); - } - if (closedir(dir) == -1) - return (-1); - return (0); -} /* bool utils_is_var_name(char *name) */ /* { */ -- cgit