From: Lukas Jiriste Date: Thu, 22 Aug 2024 11:05:21 +0000 (+0200) Subject: Make wildcard expansion quoted X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=bd441d7d6c00c1ba98bc23bbb9947bb2a387090c;p=42%2Fminishell.git Make wildcard expansion quoted This is done for the expansion not to be expanded further for variable names. This also prevents files with spae in their name to be split to multiple tokens. --- diff --git a/src/wildcards.c b/src/wildcards.c index 62edf86..8274f49 100644 --- a/src/wildcards.c +++ b/src/wildcards.c @@ -133,16 +133,46 @@ int replace_str_by_joint_split(char **str, char **expanded_split) while (expanded_split[i]) len += ft_strlen(expanded_split[i++]) + 1; *str = malloc(len + 1 + 1); + if (!str) + { + *str = tmp; + return (1); + } str[0][0] = '\0'; + i = 0; + while (expanded_split[i]) + { + ft_strlcat(*str, expanded_split[i++], len + 1 + 1); + ft_strlcat(*str, " ", len + 1 + 1); + } + free(tmp); + return (0); +} + +int replace_str_by_joint_quoted_split(char **str, char **expanded_split) +{ + char *tmp; + size_t len; + size_t i; + + tmp = *str; + len = 0; + i = 0; + while (expanded_split[i]) + len += ft_strlen(expanded_split[i++]) + 2 + 1; + *str = malloc(len + 1 + 1); if (!str) { *str = tmp; return (1); } + str[0][0] = '\0'; i = 0; while (expanded_split[i]) { + ft_strlcat(*str, "'", len + 1 + 1); ft_strlcat(*str, expanded_split[i++], len + 1 + 1); + ft_strlcat(*str, "'", len + 1 + 1); ft_strlcat(*str, " ", len + 1 + 1); } free(tmp); @@ -179,7 +209,7 @@ int expand_word(char **str, const t_execution_env *env) ++info.to_expand; res = expand_dir(&matched, &info); res = res || (ft_vec_append(&matched, &nullptr) != success); - res = res || replace_str_by_joint_split(str, matched.vec); + res = res || replace_str_by_joint_quoted_split(str, matched.vec); ft_vec_free(&matched, free_str); free((char *)info.path); if (last_char == '\n')