From bd441d7d6c00c1ba98bc23bbb9947bb2a387090c Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Thu, 22 Aug 2024 13:05:21 +0200 Subject: [PATCH] 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. --- src/wildcards.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) 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') -- 2.30.2