Expansion: Most changes dealt with

This commit is contained in:
Jérôme Guélen 2025-03-21 18:54:10 +01:00
parent 532d71efb0
commit 2a61706f1a
No known key found for this signature in database
6 changed files with 64 additions and 35 deletions

View file

@ -6,7 +6,7 @@
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/20 15:01:38 by jguelen #+# #+# */
/* Updated: 2025/03/20 16:53:31 by jguelen ### ########.fr */
/* Updated: 2025/03/21 18:42:04 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
@ -50,18 +50,30 @@ static DIR *open_current_dir(void)
return (dir);
}
/*
** Adds filename to the end of the wordlist *list by creating a worddesc whose
** word is filename and marker makes filename be considered fully single
** quoted.
** NOTE: In case of error, this function destroys *list in a similar fashion
** as worddesc_create destroys its first parameter in case of failure.
*/
static t_wordlist *add_file_to_list(t_wordlist **list, char *filename)
{
t_worddesc *file_desc;
char *copy;
char *marker;
copy = ft_strdup(filename);
if (!copy)
return (wordlist_destroy(*list), NULL);
file_desc = worddesc_create(copy, '\0', NULL);
marker = construct_repeting_char_string('\'', ft_strlen(copy));
if (!marker)
return (wordlist_destroy(*list), free(copy), NULL);
file_desc = worddesc_create(copy, '\0', marker);
if (!file_desc)
{
wordlist_destroy(*list);
free(marker);
return (NULL);
}
*list = wordlist_push(*list, file_desc);
@ -117,7 +129,10 @@ static t_wordlist *expand_star_core(t_worddesc *file_pattern)
** 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.
** Similarly, '/' is never to be matched except if given explicitly as per
** bash requirement. This is a note in case of future expansion as in our case
** here we do not have to deal with that since we concern ourselves only with
** the current working directory.
*/
char fits_pattern(char *str, t_worddesc *pattern)
{
@ -156,25 +171,6 @@ char fits_pattern(char *str, t_worddesc *pattern)
*/
t_wordlist *expand_star(t_worddesc *file_pattern)
{
size_t i;
char *tmp;
i = 0;
while (file_pattern->word[i])
{
if (file_pattern->marker[i] != '\'' && file_pattern->marker[i] != '"'
&& file_pattern->marker[i] != '&' && file_pattern->marker[i] != '$'
&& (file_pattern->word[i] == '\'' || file_pattern->word[i] == '"'))
{
tmp = replace_in_str(file_pattern->word, i, i, NULL);
free(file_pattern->word);
file_pattern->word = tmp;
tmp = replace_in_str(file_pattern->marker, i, i, NULL);
free(file_pattern->marker);
file_pattern->marker = tmp;
continue ;
}
i++;
}
clean_pattern(file_pattern);
return (expand_star_core(file_pattern));
}