From 496058d7f8d49e6c4b51b73d29bf7db5da336b54 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Thu, 22 Aug 2024 13:04:08 +0200 Subject: [PATCH] Improve wildcard recognition --- src/wildcards.c | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/src/wildcards.c b/src/wildcards.c index 010c36d..62edf86 100644 --- a/src/wildcards.c +++ b/src/wildcards.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/08 10:50:26 by ljiriste #+# #+# */ -/* Updated: 2024/08/22 11:30:03 by ljiriste ### ########.fr */ +/* Updated: 2024/08/22 13:02:23 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -169,10 +169,7 @@ int expand_word(char **str, const t_execution_env *env) if (str[0][1] == '/' || str[0][1] == '\0') info.path = ft_strdup(get_env_var_value(env, "HOME")); else - { - ft_printf("Advanced tilde expansion not implemented.\n"); return (1); - } } else info.path = ft_strdup(""); @@ -193,6 +190,36 @@ int expand_word(char **str, const t_execution_env *env) return (res); } +int should_be_expanded(const char *word) +{ + size_t i; + char quote_char; + + quote_char = '\0'; + i = 0; + while (word[i]) + { + if ((word[i] == '*' || word[i] == '?') && quote_char == '\0') + return (1); + if (word[i] == '"') + { + if (quote_char == '\0') + quote_char = '"'; + else if (quote_char == '"') + quote_char = '\0'; + } + if (word[i] == '\'') + { + if (quote_char == '\0') + quote_char = '\''; + else if (quote_char == '\'') + quote_char = '\0'; + } + ++i; + } + return (0); +} + int expand_wildcards(char **input, const t_execution_env *env) { char **split; @@ -205,7 +232,7 @@ int expand_wildcards(char **input, const t_execution_env *env) i = 0; while (split[i]) { - if (split[i][0] != '"' && split[i][0] != '\'' && (ft_strchr(split[i], '*') != NULL || ft_strchr(split[i], '?') != NULL)) + if (should_be_expanded(split[i])) if (expand_word(split + i, env)) { ft_free_split(split); -- 2.30.2