From 5ab0c197cebef701b3a7a5310b9f185369108a5b Mon Sep 17 00:00:00 2001 From: tomoron Date: Mon, 15 Apr 2024 18:55:52 +0200 Subject: [PATCH] wildcards --- Makefile | 3 ++- srcs/minishell.h | 4 +++- srcs/parsing.c | 4 ++-- srcs/wildcards.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 srcs/wildcards.c diff --git a/Makefile b/Makefile index a83daed..24cc128 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: marde-vr +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/07/28 00:35:01 by tomoron #+# #+# # -# Updated: 2024/04/13 12:56:12 by tomoron ### ########.fr # +# Updated: 2024/04/15 16:41:54 by tomoron ### ########.fr # # # # **************************************************************************** # @@ -32,6 +32,7 @@ SRCS_RAW = main.c\ input_redirections.c\ output_redirections.c\ builtins.c\ + wildcards.c\ commands.c\ pipe.c\ utils.c\ diff --git a/srcs/minishell.h b/srcs/minishell.h index a69bfb9..b631a5a 100755 --- a/srcs/minishell.h +++ b/srcs/minishell.h @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */ -/* Updated: 2024/04/13 13:31:40 by babonnet ### ########.fr */ +/* Updated: 2024/04/15 16:42:28 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,6 +24,7 @@ # include # include # include +# include typedef enum e_cmd_type { @@ -100,6 +101,7 @@ t_token *parsing_syntax_error(t_token *res); t_cmd *parsing_bonus(char *cmd); int file_access(t_msh *msh, int *found); void remove_command_from_msh(t_msh *msh); +t_token *expand_wildcards(t_token *res, char *value); void ft_exit(t_msh *msh, int error_code); void signal_handler_command(int signum); void ft_exit(t_msh *msh, int exit_code); diff --git a/srcs/parsing.c b/srcs/parsing.c index 78096c1..7d17cf1 100755 --- a/srcs/parsing.c +++ b/srcs/parsing.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/09 15:26:01 by tomoron #+# #+# */ -/* Updated: 2024/04/02 00:42:36 by tomoron ### ########.fr */ +/* Updated: 2024/04/15 15:56:17 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" @@ -73,7 +73,7 @@ t_token *parse_command(char *command, t_env *env) value = get_token(&command, &in_quote, &in_dquote, env); if (!value) return (free_token(res)); - res = token_add_back(res, value); + res = expand_wildcards(res, value); while (ft_isspace(*command)) command++; } diff --git a/srcs/wildcards.c b/srcs/wildcards.c new file mode 100644 index 0000000..2062cb4 --- /dev/null +++ b/srcs/wildcards.c @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* wildcards.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/15 12:53:29 by tomoron #+# #+# */ +/* Updated: 2024/04/15 18:54:15 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "minishell.h" + +int filename_corresponds(char *wildcard, char *value) +{ + if(*value == '.' && *wildcard != '.') + return(0); + while(*wildcard && *value && (*wildcard == '*' || *wildcard == *value)) + { + if(*wildcard == '*') + { + while(*wildcard && *value && *value != wildcard[1]) + value++; + wildcard++; + } + while(*wildcard && *value && *value == *wildcard && *wildcard != '*') + { + wildcard++; + value++; + } + } + return(!*wildcard); +} + +t_token *expand_wildcards(t_token *res, char *value) +{ + DIR *dir; + char *cwd; + struct dirent *content; + + if(!ft_strchr(value, '*')) + return(token_add_back(res, value)); + cwd = getcwd(NULL, 100000); + if(!cwd) + return(token_add_back(res, value)); + dir = opendir(cwd); + free(cwd); + if(!dir) + return(token_add_back(res, value)); + content = readdir(dir); + while(content) + { + if(filename_corresponds(value, content->d_name)) + res = token_add_back(res,ft_strdup(content->d_name)); + content = readdir(dir); + } + closedir(dir); + if(!res) + return(token_add_back(res, value)); + free(value); + return(res); +}