From c2d979012a7edcd8aa1e53f7c61f317579e4feed Mon Sep 17 00:00:00 2001 From: wescande Date: Tue, 7 Feb 2017 19:53:12 +0100 Subject: [PATCH] rectif du ./ dans le glob + optim du nb de result dans le cas de recherche cross-dir --- 42sh/includes/glob.h | 4 +++- 42sh/src/glob/dir_glob.c | 16 ++++++++-------- 42sh/src/glob/glob.c | 17 +++++++++-------- 42sh/src/glob/match_pattern.c | 9 ++++++--- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/42sh/includes/glob.h b/42sh/includes/glob.h index 651a6231..728ce648 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/02/07 16:04:57 by wescande ### ########.fr */ +/* Updated: 2017/02/07 19:40:59 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,6 +27,8 @@ typedef struct s_ld typedef struct s_glob { + short int found; + short int cur_dir; char *pat; unsigned char *esc; unsigned char *esc2; diff --git a/42sh/src/glob/dir_glob.c b/42sh/src/glob/dir_glob.c index 9c9ad8e3..8dc2d0da 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/02/07 13:06:07 by wescande ### ########.fr */ +/* Updated: 2017/02/07 19:48:12 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,13 +20,11 @@ int is_directory(const char *path) return (S_ISDIR(path_stat.st_mode)); } -static int dir_list_content(t_glob *gl, char **str, char *pat, +static void dir_list_content(t_glob *gl, char **str, char *pat, int recursive) { - int ret; char *path_tmp; - ret = 0; if (str[1][0] != '.') { if (*str[0] == '/' && !*(str[0] + 1)) @@ -36,12 +34,14 @@ static int dir_list_content(t_glob *gl, char **str, char *pat, 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 * + if (match_pattern(gl, str[1], path_tmp)) + { + gl->found = 1; + ft_ld_pushfront(&gl->match, ft_strdup(path_tmp + gl->cur_dir * 2 * (path_tmp[0] == '.' && path_tmp[1] == '/'))); + } ft_strdel(&path_tmp); } - return (ret); } int dir_research(t_glob *gl, char *p, @@ -56,7 +56,7 @@ int dir_research(t_glob *gl, char *p, { dir = opendir(p); while ((in = readdir(dir))) - ret += dir_list_content(gl, + dir_list_content(gl, (char *[2]){p, in->d_name}, pat, recursive); closedir(dir); } diff --git a/42sh/src/glob/glob.c b/42sh/src/glob/glob.c index d5bc8354..fc0c4d47 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/02/07 18:59:50 by wescande ### ########.fr */ +/* Updated: 2017/02/07 19:47:53 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,26 +37,27 @@ char **glob(char *pat, unsigned char *esc, unsigned char *esc2) { t_glob gl; - int ret; int len; len = ft_strlen(pat); - gl = (t_glob){ft_strdup(pat), dup_char_esc(esc, (len >> 3) + 1), + gl = (t_glob){0, 0, ft_strdup(pat), dup_char_esc(esc, (len >> 3) + 1), dup_char_esc(esc2, (len >> 3) +1), NULL, NULL}; expand_var(&gl); ft_memdel((void **)&gl.esc2); expand_brace(&gl); - while (gl.m_pat->next) + while (gl.m_pat->next && !(gl.found = 0)) { + gl.found = 0; + gl.cur_dir = 1; 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, 0); + dir_research(&gl, ".", gl.pat, 0); else - ret = dir_research(&gl, "/", gl.pat + 1, 0); - if (!ret) + dir_research(&gl, "/", gl.pat + 1, 0); + if (!gl.found) ft_ld_pushfront(&gl.match, - ft_strdup(((char **)gl.m_pat->content)[0])); + ft_strjoin(gl.cur_dir ? "" : "./", ((char **)gl.m_pat->content)[0])); gl.m_pat = gl.m_pat->next; } ft_ld_clear(&gl.m_pat, &ft_tabdel); diff --git a/42sh/src/glob/match_pattern.c b/42sh/src/glob/match_pattern.c index 47b77311..086d768f 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/02/07 13:06:58 by wescande ### ########.fr */ +/* Updated: 2017/02/07 19:45:54 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -85,17 +85,20 @@ static int match_star(t_glob *gl, char *str, char *full_word) return (0); } -char *manage_pat(char *pat, char *str) +static char *manage_pat(t_glob *gl, char *pat, char *str) { if (pat[0] == '.' && pat[1] == '/' && ((str[0] == '.' && str[1] != '/') || str[0] != '.')) + { + gl->cur_dir = 0; return (pat + 2); + } return (pat); } int match_pattern(t_glob *gl, char *str, char *full_word) { - gl->pat = manage_pat(gl->pat, str); + gl->pat = manage_pat(gl, gl->pat, str); while (*gl->pat) { if (is_char_esc(gl->esc, ((char **)gl->m_pat->content)[0], gl->pat))