From: Lukas Jiriste Date: Thu, 22 Aug 2024 09:53:24 +0000 (+0200) Subject: Fix the wildcards' interaction with directories X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=f9f2fd391b9e301e51e26637c3be900dd710d61a;p=42%2Fminishell.git Fix the wildcards' interaction with directories --- diff --git a/src/wildcards.c b/src/wildcards.c index 703608a..010c36d 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/08 15:34:37 by ljiriste ### ########.fr */ +/* Updated: 2024/08/22 11:30:03 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -75,6 +75,11 @@ int add_conformant(t_vec *expanded, t_wildcard_info *info) ++info->current_expand_char; return (expand_further(expanded, *info)); } + if (*info->current_expand_char == '/' && info->current_entry_char == info->entry) + { + ++info->current_expand_char; + return (add_conformant(expanded, info)); + } if ((*info->current_expand_char == '?' && *info->current_entry_char != '\0') || *info->current_expand_char == *info->current_entry_char) { @@ -92,7 +97,10 @@ int expand_dir(t_vec *expanded, t_wildcard_info *info) DIR *dir; struct dirent *dir_entry; - dir = opendir(info->path); + if (info->path[0] == '\0') + dir = opendir("./"); + else + dir = opendir(info->path); if (!dir) return (0); dir_entry = readdir(dir); @@ -153,15 +161,25 @@ int expand_word(char **str, const t_execution_env *env) str[0][ft_strlen(*str) - 1] = '\0'; if (ft_vec_init(&matched, sizeof(char *)) != success) return (1); + info.to_expand = *str; if (str[0][0] == '/') info.path = ft_strdup("/"); - else if (str[0][0] == '~') - info.path = ft_strjoin(get_env_var_value(env, "HOME"), "/"); + else if (str[0][0] == '~' && (ft_isalpha(str[0][1]) || str[0][1] == '/' || str[0][1] == '~' || str[0][1] == '+' || str[0][1] == '-')) + { + 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("./"); + info.path = ft_strdup(""); if (!info.path) return (1); - info.to_expand = *str; + if (info.path[0] && ft_strchr("/~", info.path[0]) != NULL) + ++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);