diff --git a/42sh/includes/glob.h b/42sh/includes/glob.h index 50038348..6b8d2475 100644 --- a/42sh/includes/glob.h +++ b/42sh/includes/glob.h @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/04 16:31:18 by wescande #+# #+# */ -/* Updated: 2017/01/24 19:27:53 by wescande ### ########.fr */ +/* Updated: 2017/01/24 21:22:58 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,8 +25,10 @@ typedef struct s_ld char **glob(const char *str, char **env); t_ld *expand_brace(const char *pat); void glob_print(t_list *token, t_data *data); -int match_pattern(const char *pat, char *str, char *full_word, t_ld **match); +int match_pattern(const char *pat, char *str, + char *full_word, t_ld **match); void dir_research(const char *pat, char *path, t_ld **match); +void dir_research_recursive(const char *pat, char *p, t_ld **match); bool is_directory(const char *path); /* diff --git a/42sh/srcs/glob/dir_glob.c b/42sh/srcs/glob/dir_glob.c index 6d8b0b27..c4bdb128 100644 --- a/42sh/srcs/glob/dir_glob.c +++ b/42sh/srcs/glob/dir_glob.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/04 16:29:54 by wescande #+# #+# */ -/* Updated: 2017/01/24 19:10:52 by wescande ### ########.fr */ +/* Updated: 2017/01/24 21:16:42 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,3 +46,32 @@ void dir_research(const char *pat, char *p, t_ld **match) } } } + +void dir_research_recursive(const char *pat, char *p, t_ld **match) +{ + DIR *dir; + struct dirent *in; + char *path_tmp; + + if (ft_strlen(p) <= 1 || p[ft_strlen(p) - 1] != '.') + { + if (!(dir = opendir(p))) + return ; + while ((in = readdir(dir))) + { + if (ft_strcmp(in->d_name, ".") && ft_strcmp(in->d_name, "..")) + { + if (*p == '/' && !*(p + 1)) + path_tmp = ft_strjoin(p, in->d_name); + else + path_tmp = ft_strjoinf(ft_strjoin(p, "/"), in->d_name, 1); + if (is_directory(path_tmp)) + dir_research_recursive(pat, path_tmp, match); + if (match_pattern(pat, in->d_name, path_tmp, match)) + ft_ld_pushfront(match, ft_strdup(path_tmp + 2 * + (path_tmp[0] == '.' && path_tmp[1] == '/'))); + ft_strdel(&path_tmp); + } + } + } +} diff --git a/42sh/srcs/glob/match_pattern.c b/42sh/srcs/glob/match_pattern.c index 84b7c107..5e95b55b 100644 --- a/42sh/srcs/glob/match_pattern.c +++ b/42sh/srcs/glob/match_pattern.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/24 17:30:23 by wescande #+# #+# */ -/* Updated: 2017/01/24 20:42:30 by wescande ### ########.fr */ +/* Updated: 2017/01/24 20:58:47 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -72,6 +72,8 @@ static int match_star(const char *pat, char *str, { char *fix; + if (pat[1] == '*') + dir_research_recursive(pat, full_word, match); if (!pat[1]) return (1); fix = str + ft_strlen(str);