wildcards (mieux)

This commit is contained in:
2024-04-16 09:35:04 +02:00
parent dd581557de
commit 483d6579d6

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */ /* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/15 12:53:29 by tomoron #+# #+# */ /* Created: 2024/04/15 12:53:29 by tomoron #+# #+# */
/* Updated: 2024/04/15 18:54:15 by tomoron ### ########.fr */ /* Updated: 2024/04/16 09:34:47 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
@ -15,11 +15,11 @@ int filename_corresponds(char *wildcard, char *value)
{ {
if(*value == '.' && *wildcard != '.') if(*value == '.' && *wildcard != '.')
return(0); return(0);
while(*wildcard && *value && (*wildcard == '*' || *wildcard == *value)) while(*wildcard && (*wildcard == '*' || *wildcard == *value))
{ {
if(*wildcard == '*') if(*wildcard == '*')
{ {
while(*wildcard && *value && *value != wildcard[1]) while(*wildcard && *value && *value != wildcard[1] && *wildcard != wildcard[1])
value++; value++;
wildcard++; wildcard++;
} }
@ -29,7 +29,7 @@ int filename_corresponds(char *wildcard, char *value)
value++; value++;
} }
} }
return(!*wildcard); return(!*wildcard && !*value);
} }
t_token *expand_wildcards(t_token *res, char *value) t_token *expand_wildcards(t_token *res, char *value)
@ -37,7 +37,9 @@ t_token *expand_wildcards(t_token *res, char *value)
DIR *dir; DIR *dir;
char *cwd; char *cwd;
struct dirent *content; struct dirent *content;
int found;
found = 0;
if(!ft_strchr(value, '*')) if(!ft_strchr(value, '*'))
return(token_add_back(res, value)); return(token_add_back(res, value));
cwd = getcwd(NULL, 100000); cwd = getcwd(NULL, 100000);
@ -51,11 +53,14 @@ t_token *expand_wildcards(t_token *res, char *value)
while(content) while(content)
{ {
if(filename_corresponds(value, content->d_name)) if(filename_corresponds(value, content->d_name))
{
res = token_add_back(res,ft_strdup(content->d_name)); res = token_add_back(res,ft_strdup(content->d_name));
found = 1;
}
content = readdir(dir); content = readdir(dir);
} }
closedir(dir); closedir(dir);
if(!res) if(!found)
return(token_add_back(res, value)); return(token_add_back(res, value));
free(value); free(value);
return(res); return(res);