rectif du ./ dans le glob + optim du nb de result dans le cas de recherche cross-dir

This commit is contained in:
wescande 2017-02-07 19:53:12 +01:00
parent 90611cf046
commit c2d979012a
4 changed files with 26 additions and 20 deletions

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */ /* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/04 16:31:18 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 typedef struct s_glob
{ {
short int found;
short int cur_dir;
char *pat; char *pat;
unsigned char *esc; unsigned char *esc;
unsigned char *esc2; unsigned char *esc2;

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */ /* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/30 12:07:16 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)); 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 recursive)
{ {
int ret;
char *path_tmp; char *path_tmp;
ret = 0;
if (str[1][0] != '.') if (str[1][0] != '.')
{ {
if (*str[0] == '/' && !*(str[0] + 1)) if (*str[0] == '/' && !*(str[0] + 1))
@ -36,12 +34,14 @@ static int dir_list_content(t_glob *gl, char **str, char *pat,
if (recursive) if (recursive)
dir_research(gl, path_tmp, pat, recursive); dir_research(gl, path_tmp, pat, recursive);
gl->pat = pat; gl->pat = pat;
if (match_pattern(gl, str[1], path_tmp) && ++ret) if (match_pattern(gl, str[1], path_tmp))
ft_ld_pushfront(&gl->match, ft_strdup(path_tmp + 2 * {
gl->found = 1;
ft_ld_pushfront(&gl->match, ft_strdup(path_tmp + gl->cur_dir * 2 *
(path_tmp[0] == '.' && path_tmp[1] == '/'))); (path_tmp[0] == '.' && path_tmp[1] == '/')));
}
ft_strdel(&path_tmp); ft_strdel(&path_tmp);
} }
return (ret);
} }
int dir_research(t_glob *gl, char *p, int dir_research(t_glob *gl, char *p,
@ -56,7 +56,7 @@ int dir_research(t_glob *gl, char *p,
{ {
dir = opendir(p); dir = opendir(p);
while ((in = readdir(dir))) while ((in = readdir(dir)))
ret += dir_list_content(gl, dir_list_content(gl,
(char *[2]){p, in->d_name}, pat, recursive); (char *[2]){p, in->d_name}, pat, recursive);
closedir(dir); closedir(dir);
} }

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */ /* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/04 16:29:54 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) unsigned char *esc2)
{ {
t_glob gl; t_glob gl;
int ret;
int len; int len;
len = ft_strlen(pat); 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}; dup_char_esc(esc2, (len >> 3) +1), NULL, NULL};
expand_var(&gl); expand_var(&gl);
ft_memdel((void **)&gl.esc2); ft_memdel((void **)&gl.esc2);
expand_brace(&gl); 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.pat = ((char **)gl.m_pat->content)[0];
gl.esc = ((unsigned char **)gl.m_pat->content)[1]; gl.esc = ((unsigned char **)gl.m_pat->content)[1];
if (gl.pat[0] != '/') if (gl.pat[0] != '/')
ret = dir_research(&gl, ".", gl.pat, 0); dir_research(&gl, ".", gl.pat, 0);
else else
ret = dir_research(&gl, "/", gl.pat + 1, 0); dir_research(&gl, "/", gl.pat + 1, 0);
if (!ret) if (!gl.found)
ft_ld_pushfront(&gl.match, 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; gl.m_pat = gl.m_pat->next;
} }
ft_ld_clear(&gl.m_pat, &ft_tabdel); ft_ld_clear(&gl.m_pat, &ft_tabdel);

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */ /* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/24 17:30:23 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); 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] == '/' if (pat[0] == '.' && pat[1] == '/'
&& ((str[0] == '.' && str[1] != '/') || str[0] != '.')) && ((str[0] == '.' && str[1] != '/') || str[0] != '.'))
{
gl->cur_dir = 0;
return (pat + 2); return (pat + 2);
}
return (pat); return (pat);
} }
int match_pattern(t_glob *gl, char *str, char *full_word) 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) while (*gl->pat)
{ {
if (is_char_esc(gl->esc, ((char **)gl->m_pat->content)[0], gl->pat)) if (is_char_esc(gl->esc, ((char **)gl->m_pat->content)[0], gl->pat))