aboutsummaryrefslogtreecommitdiff
path: root/src/path.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-03-28 12:10:13 +0100
committerCharles <sircharlesaze@gmail.com>2020-03-28 12:10:13 +0100
commit73a7027a9d58c6ca71817170bff23ac71edac9d8 (patch)
treebb992bb24bf4e0f576f4c37eb9cc5dea04cdbdd3 /src/path.c
parent3b319748a024067b4ce0b70ae2dad364977d6365 (diff)
downloadminishell-73a7027a9d58c6ca71817170bff23ac71edac9d8.tar.gz
minishell-73a7027a9d58c6ca71817170bff23ac71edac9d8.tar.bz2
minishell-73a7027a9d58c6ca71817170bff23ac71edac9d8.zip
Cleaning env and path, Removed state struct
Diffstat (limited to 'src/path.c')
-rw-r--r--src/path.c63
1 files changed, 15 insertions, 48 deletions
diff --git a/src/path.c b/src/path.c
index d4f4418..3ac12d9 100644
--- a/src/path.c
+++ b/src/path.c
@@ -12,15 +12,13 @@
#include "minishell.h"
-static int st_in_dirs(char **dirs, char *dir)
-{
- while (*dirs != NULL)
- if (ft_strcmp(*dirs++, dir) == 0)
- return (0);
- return (-1);
-}
+#define MS_PATH_HT_SIZE 4096
+
+/*
+** Update `path` with all files in the directory named `dirname`.
+*/
-static t_path *st_path_commands_update(t_path *path, char *dirname)
+static t_path st_path_dir_update(t_path path, char *dirname)
{
DIR *dir;
struct dirent *entry;
@@ -30,9 +28,9 @@ static t_path *st_path_commands_update(t_path *path, char *dirname)
return (NULL);
while ((entry = readdir(dir)) != NULL)
{
- if ((tmp = ft_strjoin(dirname, entry->d_name)) == NULL)
+ if ((tmp = ft_strjoin3(dirname, "/", entry->d_name)) == NULL)
return (NULL);
- if (ft_htset(path->commands, entry->d_name, tmp, ms_ht_del_str_entry) == NULL)
+ if (ft_htset(path, entry->d_name, tmp, ms_ht_del_str_entry) == NULL)
return (NULL);
}
if (closedir(dir) == -1)
@@ -41,56 +39,25 @@ static t_path *st_path_commands_update(t_path *path, char *dirname)
}
/*
-** Update path list with `dirname`
-*/
-
-static t_path *st_path_dir_update(t_path *path, char *dirname)
-{
- t_ftlst *front;
-
- if (ft_lstlfind(path->dirs, (int (*)(const void*, const void*))ft_strcmp, dirname) == NULL)
- {
- if ((dirname = ft_strdup(dirname)) == NULL)
- return (NULL);
- if ((front = ft_lstnew(dirname)) == NULL)
- {
- free(dirname);
- return (NULL);
- }
- ft_lstadd_front(&path->dirs, front);
- }
- return (st_path_commands_update(path, dirname));
-}
-
-/*
** Update (or create if `path` is NULL) `path` according to `path_str`
** (each directory is separated by a ':').
*/
-t_path *ms_path_update(t_path *path, const char *path_str)
+t_path ms_path_update(t_path path, char *path_var)
{
int i;
char **dirs;
- if (path == NULL)
- if ((path = malloc(sizeof(t_path))) == NULL)
- return (NULL);
- if ((dirs = ft_split(path_str, ':')) == NULL)
+ if (path_var == NULL)
+ return (NULL);
+ if (path == NULL && (path = ft_htnew(MS_PATH_HT_SIZE)) == NULL)
+ return (NULL);
+ if ((dirs = ft_split(path_var, ':')) == NULL)
return (NULL);
- ft_lstremove_if(&path->dirs, (int (*)(const void*, const void*))st_in_dirs, free, (void*)dirs);
i = -1;
- while (dirs[i] != NULL)
+ while (dirs[++i] != NULL)
if (st_path_dir_update(path, dirs[i]) == NULL)
return (ft_split_destroy(dirs));
ft_split_destroy(dirs);
return (path);
}
-
-void ms_path_destroy(t_path *path)
-{
- if (path == NULL)
- return ;
- ft_lstclear(&path->dirs, free);
- ft_htdestroy(path->commands, ms_ht_del_str_entry);
- free(path);
-}