aboutsummaryrefslogtreecommitdiff
path: root/src/path.c
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-09-09 15:52:34 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-09-09 15:52:34 +0200
commit4238485a4e26a9d15541708bfc38bfede9bbe7d2 (patch)
tree8bbc2d73a459cf6f076a7f22a59805e17b4d23f5 /src/path.c
parentea2199c8805b5db5b31c46d0c5feb37f607a8394 (diff)
downloadminishell-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/path.c')
-rw-r--r--src/path.c28
1 files changed, 18 insertions, 10 deletions
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 <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);