diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-09-09 15:52:34 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-09-09 15:52:34 +0200 |
| commit | 4238485a4e26a9d15541708bfc38bfede9bbe7d2 (patch) | |
| tree | 8bbc2d73a459cf6f076a7f22a59805e17b4d23f5 /src | |
| parent | ea2199c8805b5db5b31c46d0c5feb37f607a8394 (diff) | |
| download | minishell-4238485a4e26a9d15541708bfc38bfede9bbe7d2.tar.gz minishell-4238485a4e26a9d15541708bfc38bfede9bbe7d2.tar.bz2 minishell-4238485a4e26a9d15541708bfc38bfede9bbe7d2.zip | |
Moving utils_directory_iter back to path.c, abstraction not needed without globs
Diffstat (limited to 'src')
| -rw-r--r-- | src/path.c | 28 | ||||
| -rw-r--r-- | src/utils.c | 23 |
2 files changed, 19 insertions, 32 deletions
@@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 <cacharle@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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) */ /* { */ |
