fix(wildcard): error detection in wordlist_expand_star

Will help in closing #167
This commit is contained in:
Khaïs COLIN 2025-04-30 14:49:49 +02:00
parent 3ae29cbdf7
commit 04eabf096d
3 changed files with 24 additions and 13 deletions

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* wordlist_utils.c :+: :+: :+: */ /* wordlist_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/10 09:51:34 by jguelen #+# #+# */ /* Created: 2025/03/10 09:51:34 by jguelen #+# #+# */
/* Updated: 2025/03/20 14:43:17 by jguelen ### ########.fr */ /* Updated: 2025/04/30 14:52:32 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,6 +14,7 @@
#include "../worddesc/worddesc.h" #include "../worddesc/worddesc.h"
#include <stdlib.h> #include <stdlib.h>
#include "../../../libft/libft.h" #include "../../../libft/libft.h"
#include "../../ft_errno.h"
/* /*
** Creates a new wordlist composed of only one element. Its next field is NULL ** Creates a new wordlist composed of only one element. Its next field is NULL
@ -27,18 +28,18 @@ t_wordlist *wordlist_independant_create(t_worddesc *word)
new = ft_calloc(1, sizeof(t_wordlist)); new = ft_calloc(1, sizeof(t_wordlist));
if (!new) if (!new)
return (NULL); return (ft_errno(FT_ENOMEM), NULL);
desc_copy = ft_calloc(1, sizeof(t_worddesc)); desc_copy = ft_calloc(1, sizeof(t_worddesc));
if (!desc_copy) if (!desc_copy)
return (free(new), NULL); return (free(new), ft_errno(FT_ENOMEM), NULL);
desc_copy->word = ft_strdup(word->word); desc_copy->word = ft_strdup(word->word);
if (!desc_copy->word) if (!desc_copy->word)
return (free(desc_copy), free(new), NULL); return (free(desc_copy), free(new), ft_errno(FT_ENOMEM), NULL);
desc_copy->flags = word->flags; desc_copy->flags = word->flags;
desc_copy->marker = ft_strdup(word->marker); desc_copy->marker = ft_strdup(word->marker);
new->word = desc_copy; new->word = desc_copy;
if (!desc_copy->marker && word->marker) if (!desc_copy->marker && word->marker)
return (wordlist_destroy(new), NULL); return (wordlist_destroy(new), ft_errno(FT_ENOMEM), NULL);
return (new); return (new);
} }

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/03 19:28:28 by kcolin #+# #+# */ /* Created: 2025/04/03 19:28:28 by kcolin #+# #+# */
/* Updated: 2025/04/21 14:42:41 by kcolin ### ########.fr */ /* Updated: 2025/04/30 14:51:12 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -60,6 +60,9 @@ static t_redirect *redirect_expand_wildcards(t_redirect *in_list)
return (out_list); return (out_list);
} }
/*
** returns null on error
*/
static t_wordlist *wordlist_expand_wildcards(t_wordlist *inlist) static t_wordlist *wordlist_expand_wildcards(t_wordlist *inlist)
{ {
t_wordlist *outlist; t_wordlist *outlist;
@ -71,15 +74,20 @@ static t_wordlist *wordlist_expand_wildcards(t_wordlist *inlist)
{ {
current = wordlist_pop(&inlist); current = wordlist_pop(&inlist);
expansion_result = expand_star(current); expansion_result = expand_star(current);
if (expansion_result == NULL) if (expansion_result == NULL && ft_errno_get() == FT_ESUCCESS)
outlist = wordlist_push(outlist, current); outlist = wordlist_push(outlist, current);
else else if (expansion_result != NULL)
{ {
while (expansion_result != NULL) while (expansion_result != NULL)
outlist = wordlist_push(outlist, outlist = wordlist_push(outlist,
wordlist_pop(&expansion_result)); wordlist_pop(&expansion_result));
worddesc_destroy(current); worddesc_destroy(current);
} }
else
{
ft_dprintf(STDERR_FILENO, "minishell: wildcard: expansion fail\n");
return (wordlist_destroy(outlist), worddesc_destroy(current), NULL);
}
} }
return (outlist); return (outlist);
} }
@ -87,6 +95,8 @@ static t_wordlist *wordlist_expand_wildcards(t_wordlist *inlist)
t_simple_cmd *simple_cmd_expand_wildcards(t_simple_cmd *cmd) t_simple_cmd *simple_cmd_expand_wildcards(t_simple_cmd *cmd)
{ {
cmd->words = wordlist_expand_wildcards(cmd->words); cmd->words = wordlist_expand_wildcards(cmd->words);
if (cmd->words == NULL && ft_errno_get() != FT_ESUCCESS)
return (NULL);
ft_errno(FT_ESUCCESS); ft_errno(FT_ESUCCESS);
if (redirect_expand_wildcards(cmd->redirections) == NULL if (redirect_expand_wildcards(cmd->redirections) == NULL
&& ft_errno_get() != FT_ESUCCESS) && ft_errno_get() != FT_ESUCCESS)

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* wildcard_exp.c :+: :+: :+: */ /* wildcard_exp.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/20 15:01:38 by jguelen #+# #+# */ /* Created: 2025/03/20 15:01:38 by jguelen #+# #+# */
/* Updated: 2025/04/30 14:07:14 by jguelen ### ########.fr */ /* Updated: 2025/04/30 14:53:30 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -103,7 +103,7 @@ static t_wordlist *expand_star_core(t_worddesc *file_pattern)
current_dir = open_current_dir(); current_dir = open_current_dir();
if (current_dir == NULL) if (current_dir == NULL)
return (NULL); return (ft_errno(FT_EERRNO), NULL);
file_wordlist = NULL; file_wordlist = NULL;
new = readdir(current_dir); new = readdir(current_dir);
while (new) while (new)
@ -166,7 +166,7 @@ t_wordlist *expand_star(t_worddesc *file_pattern)
t_wordlist *expanded; t_wordlist *expanded;
if (!file_pattern || !file_pattern->word) if (!file_pattern || !file_pattern->word)
return (NULL); return (ft_errno(FT_EERRNO), NULL);
pattern_copy = deal_with_potential_pattern_marker(file_pattern); pattern_copy = deal_with_potential_pattern_marker(file_pattern);
if (!ispattern(pattern_copy)) if (!ispattern(pattern_copy))
return (worddesc_destroy(pattern_copy), NULL); return (worddesc_destroy(pattern_copy), NULL);