diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-10-03 17:06:53 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-10-03 17:06:53 +0200 |
commit | 4cf209d8b88da70b6f8d4be096a2ef6140cb81a9 (patch) | |
tree | 96808f82e989035927ee6e1c58976f47993600f2 /src/u_path.c | |
parent | Normed u_parse (diff) | |
download | 42-minishell-4cf209d8b88da70b6f8d4be096a2ef6140cb81a9.tar.gz 42-minishell-4cf209d8b88da70b6f8d4be096a2ef6140cb81a9.tar.bz2 42-minishell-4cf209d8b88da70b6f8d4be096a2ef6140cb81a9.tar.xz 42-minishell-4cf209d8b88da70b6f8d4be096a2ef6140cb81a9.tar.zst 42-minishell-4cf209d8b88da70b6f8d4be096a2ef6140cb81a9.zip |
Normed u_path
Diffstat (limited to 'src/u_path.c')
-rw-r--r-- | src/u_path.c | 62 |
1 files changed, 39 insertions, 23 deletions
diff --git a/src/u_path.c b/src/u_path.c index b43946f..5e756a5 100644 --- a/src/u_path.c +++ b/src/u_path.c @@ -16,12 +16,13 @@ #include <limits.h> #include "s_struct.h" +#include "u_path.h" #include "u_vars.h" -static void u_get_fullpath(char fullpath[], - const char p_path[], - const char d_name[], - size_t dstsize) +static void u_get_fullpath(char fullpath[], + const char p_path[], + const char d_name[], + size_t dstsize) { const size_t path_len = ft_strlen(p_path); const size_t name_len = ft_strlen(d_name); @@ -29,44 +30,59 @@ static void u_get_fullpath(char fullpath[], fullpath[0] = C_NUL; if (path_len + name_len < dstsize) { - (void)ft_memcpy(fullpath, p_path, path_len); + (void)ft_memcpy(fullpath, p_path, path_len * sizeof(char)); *(fullpath + (path_len)) = '/'; - ft_memcpy(fullpath + path_len + 1, d_name, name_len); + (void)ft_memcpy(fullpath + path_len + 1, d_name, + name_len * sizeof(char)); *(fullpath + (path_len + name_len + 1)) = '\0'; } } +static int8_t u_read_dir(DIR *dir, + struct s_path s, + const char com[], + char fullpath[]) +{ + struct dirent *ent; + + while ((ent = readdir(dir)) != NULL) + { + if (ft_strncmp(com, ent->d_name, ft_strlen(com) + 1) == 0) + { + u_get_fullpath(fullpath, s.tok_path, ent->d_name, s.dstsize); + closedir(dir); + if (fullpath[0] == C_NUL) + return (1); + return (0); + } + } + return (-1); +} + uint8_t u_search_in_path(char fullpath[], const char com[], size_t dstsize, t_msh *msh) { - struct dirent *ent; char tmp[ARG_MAX]; - char *tok_path; + struct s_path s; DIR *dir; + int8_t ret; if (u_get_var_value(tmp, "$PATH", ARG_MAX, msh) != 0) return (1); - tok_path = ft_strtok(tmp, ":"); - while (tok_path != NULL) + s.dstsize = dstsize; + s.tok_path = ft_strtok(tmp, ":"); + while (s.tok_path != NULL) { - if ((dir = opendir(tok_path)) != NULL) + if ((dir = opendir(s.tok_path)) != NULL) { - while ((ent = readdir(dir)) != NULL) - { - if (ft_strncmp(com, ent->d_name, ft_strlen(com) + 1) == 0) - { - u_get_fullpath(fullpath, tok_path, ent->d_name, dstsize); - closedir(dir); - if (fullpath[0] == C_NUL) - return (1); - return (0); - } - } + ret = u_read_dir(dir, s, com, fullpath); + if (ret == 0 || ret == 1) + return (ret); closedir(dir); } - tok_path = ft_strtok(NULL, ":"); + s.tok_path = ft_strtok(NULL, ":"); } return (1); } |