Expansion: fix: algo was wrong for wildcard

Hopefully, this is a proper fix. This commit serves mainly to save
advancement.
This commit is contained in:
Jérôme Guélen 2025-03-15 15:44:23 +01:00
parent 1766e8d1ba
commit fc12d8e057
No known key found for this signature in database
5 changed files with 129 additions and 81 deletions

View file

@ -6,7 +6,7 @@
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/23 15:02:59 by jguelen #+# #+# */
/* Updated: 2025/03/14 14:30:30 by jguelen ### ########.fr */
/* Updated: 2025/03/15 14:45:11 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
@ -128,6 +128,41 @@ static t_wordlist *expand_star_core(t_worddesc *file_pattern)
return (wordlist_quicksort_full(file_wordlist));
}
/*
** Returns 1 if and only if filename is recognized by pattern, 0
** otherwise or -1 in case of error.
** Takes only into account the * wildcard or ?, those characters
** NOTE: for a pattern to accept '.' as the first character of a filename
** it must be explicitly matched (only for the first character though).
** Similarly, '/' is never to be matched except if given explicitly.
*/
char fits_pattern(char *str, t_worddesc *pattern)
{
char **pattern_check;
size_t str_len;
size_t pattern_len;
size_t i;
char ret;
pattern_len = ft_strlen(pattern->word);
str_len = ft_strlen(str);
pattern_check = ft_calloc(str_len + 1, sizeof(char *));
if (!pattern_check)
return (-1);
i = 0;
while (i <= str_len)
{
pattern_check[i] = ft_calloc(pattern_len + 1, sizeof(char));
if (!pattern_check[i])
return (destroy_pattern_check(pattern_check, str_len + 1), NULL);
i++;
}
build_pattern_checks(str, pattern, pattern_check);
ret = pattern_check[str_len][pattern_len];
destroy_pattern_check(pattern_check, str_len + 1);
return (ret);
}
/*
** TODO
** Pre-processes file_pattern to give it a proper marker chain and remove