diff --git a/42sh/includes/glob.h b/42sh/includes/glob.h index f6beebb8..96cf40f2 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/31 23:20:22 by wescande ### ########.fr */ +/* Updated: 2017/02/01 19:50:07 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -69,8 +69,7 @@ void modify_esc_split(unsigned char *esc_dest, unsigned char *esc_src, int start, int len); void expand_brace(t_glob *tglob); int match_pattern(t_glob *tglob, char *str, char *full_word); -int dir_research(t_glob *tglob, char *p, const char *pat); -int dir_research_recursive(t_glob *tglob, char *p, const char *pat); +int dir_research(t_glob *tglob, char *p, const char *pat, int rec); char **ft_strsplit_spe(const char *str, const unsigned char *esc, char c); unsigned char **ft_strsplit_esc(const char *str, diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 6a6924fc..0cbba450 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */ -/* Updated: 2017/01/31 20:19:48 by wescande ### ########.fr */ +/* Updated: 2017/02/01 19:29:27 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/glob/dir_glob.c b/42sh/src/glob/dir_glob.c index a16dcc38..3ed4a660 100644 --- a/42sh/src/glob/dir_glob.c +++ b/42sh/src/glob/dir_glob.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/30 12:07:16 by wescande #+# #+# */ -/* Updated: 2017/01/31 18:00:16 by wescande ### ########.fr */ +/* Updated: 2017/02/01 19:49:44 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,65 +14,51 @@ int is_directory(const char *path) { - struct stat path_stat; + struct stat path_stat; stat(path, &path_stat); return (S_ISDIR(path_stat.st_mode)); } -int dir_research(t_glob *gl, char *p, const char *pat) +static int dir_list_content(t_glob *gl, char **str, const char *pat, + int recursive) { - DIR *dir; - struct dirent *in; - char *path_tmp; - int ret; + int ret; + char *path_tmp; ret = 0; - if ((ft_strlen(p) <= 1 || p[ft_strlen(p) - 1] != '.') - && is_directory(p) && (dir = opendir(p))) - 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); - gl->pat = pat; - if (match_pattern(gl, in->d_name, path_tmp) && ++ret) - ft_ld_pushfront(&gl->match, ft_strdup(path_tmp + 2 * - (path_tmp[0] == '.' && path_tmp[1] == '/'))); - ft_strdel(&path_tmp); - } - } + if (ft_strcmp(str[1], ".") && ft_strcmp(str[1], "..")) + { + if (*str[0] == '/' && !*(str[0] + 1)) + path_tmp = ft_strjoin(str[0], str[1]); + else + path_tmp = ft_strjoinf(ft_strjoin(str[0], "/"), str[1], 1); + if (recursive) + dir_research(gl, path_tmp, pat, recursive); + gl->pat = pat; + if (match_pattern(gl, str[1], path_tmp) && ++ret) + ft_ld_pushfront(&gl->match, ft_strdup(path_tmp + 2 * + (path_tmp[0] == '.' && path_tmp[1] == '/'))); + ft_strdel(&path_tmp); + } return (ret); } -int dir_research_recursive(t_glob *gl, char *p, const char *pat) +int dir_research(t_glob *gl, char *p, + const char *pat, int recursive) { DIR *dir; struct dirent *in; - char *path_tmp; int ret; ret = 0; - if ((ft_strlen(p) <= 1 || p[ft_strlen(p) - 1] != '.') - && is_directory(p) && (dir = opendir(p))) + if ((ft_strlen(p) <= 1 || p[ft_strlen(p) - 1] != '.') && is_directory(p)) + { + dir = opendir(p); 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); - dir_research_recursive(gl, path_tmp, pat); - gl->pat = pat; - if (match_pattern(gl, in->d_name, path_tmp) && ++ret) - ft_ld_pushfront(&gl->match, ft_strdup(path_tmp + 2 * - (path_tmp[0] == '.' && path_tmp[1] == '/'))); - ft_strdel(&path_tmp); - } - } + ret += dir_list_content(gl, + (char *[2]){p, in->d_name}, pat, recursive); + closedir(dir); + } return (ret); } diff --git a/42sh/src/glob/glob.c b/42sh/src/glob/glob.c index 4e294d5a..1ec41786 100644 --- a/42sh/src/glob/glob.c +++ b/42sh/src/glob/glob.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/04 16:29:54 by wescande #+# #+# */ -/* Updated: 2017/01/31 23:15:27 by wescande ### ########.fr */ +/* Updated: 2017/02/01 19:46:22 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,9 +45,9 @@ char **glob(const char *pat, const unsigned char *esc) gl.pat = ((char **)gl.m_pat->content)[0]; gl.esc = ((unsigned char **)gl.m_pat->content)[1]; if (gl.pat[0] != '/') - ret = dir_research(&gl, ".", gl.pat); + ret = dir_research(&gl, ".", gl.pat, 0); else - ret = dir_research(&gl, "/", gl.pat + 1); + ret = dir_research(&gl, "/", gl.pat + 1, 0); if (!ret) ft_ld_pushfront(&gl.match, ft_strdup(((char **)gl.m_pat->content)[0])); diff --git a/42sh/src/glob/match_pattern.c b/42sh/src/glob/match_pattern.c index 8707c3c4..b2688603 100644 --- a/42sh/src/glob/match_pattern.c +++ b/42sh/src/glob/match_pattern.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/24 17:30:23 by wescande #+# #+# */ -/* Updated: 2017/01/27 23:45:39 by wescande ### ########.fr */ +/* Updated: 2017/02/01 19:46:43 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -72,7 +72,7 @@ static int match_star(t_glob *gl, char *str, char *full_word) if (gl->pat[1] == '*' && !is_char_esc(gl->esc, ((char **)gl->m_pat->content)[0], gl->pat + 1)) - dir_research_recursive(gl, full_word, gl->pat + 1); + dir_research(gl, full_word, gl->pat + 1, 1); if (!*++gl->pat) return (1); fix = str + ft_strlen(str); @@ -113,7 +113,7 @@ int match_pattern(t_glob *gl, char *str, char *full_word) else if (*gl->pat == '*') return (match_star(gl, str, full_word)); else if (*gl->pat == '/' && !*str && is_directory(full_word)) - dir_research(gl, full_word, gl->pat + 1); + dir_research(gl, full_word, gl->pat + 1, 0); else if (*gl->pat != *str) return (0); ++str;