diff --git a/Makefile b/Makefile index c1ffe4f..5f049b6 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: marde-vr +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/07/28 00:35:01 by tomoron #+# #+# # -# Updated: 2024/04/15 18:58:21 by tomoron ### ########.fr # +# Updated: 2024/04/17 10:13:40 by tomoron ### ########.fr # # # # **************************************************************************** # @@ -30,6 +30,7 @@ SRCS_RAW = main.c\ here_doc_utils.c\ export.c\ input_redirections.c\ + sort_wildcard_list.c\ output_redirections.c\ builtins.c\ wildcards.c\ diff --git a/srcs/minishell.h b/srcs/minishell.h index b631a5a..146f205 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/15 16:42:28 by tomoron ### ########.fr */ +/* Updated: 2024/04/17 10:13:08 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -134,6 +134,7 @@ int is_output_type(t_cmd *cmd); int is_cmd_type(t_cmd *cmd); int is_operand_type(t_cmd *cmd); int set_echoctl(int value); +void sort_wildcards_token(t_token *list); int print_env(t_env *env); t_cmd *free_cmd(t_cmd *cmd); void print_syntax_error_bonus(t_cmd *cmd); diff --git a/srcs/sort_wildcard_list.c b/srcs/sort_wildcard_list.c new file mode 100644 index 0000000..ac0ac2d --- /dev/null +++ b/srcs/sort_wildcard_list.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sort_wildcard_list.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/17 09:12:33 by tomoron #+# #+# */ +/* Updated: 2024/04/17 10:42:39 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int wildcard_cmp(char *s1, char *s2, char *order) +{ + while(*s1 && *s2) + { + while(*s1 && !ft_isalnum(*s1)) + s1++; + while(*s2 && !ft_isalnum(*s2)) + s2++; + if(*s1 != *s2) + return ((ft_strchr(order, *s2) - order) - (ft_strchr(order, *s1)\ + - order)); + s1++; + s2++; + } + return ((ft_strchr(order, *s2) - order) - (ft_strchr(order, *s1) - order)); +} + +void sort_wildcards_token(t_token *list) +{ + t_token *tmp; + t_token *start; + char *swap; + + tmp = list; + start = list; + while(tmp) + { + list = start; + while(list->next) + { + if(wildcard_cmp(list->value, list->next->value, "zZyYxXwWvVuUtTsSrRqQpPoOnNmMlLkKjJiIhHgGfFeEdDcCbBaA9876543210") > 0) + { + swap = list->value; + list->value = list->next->value; + list->next->value = swap; + } + list = list->next; + } + tmp = tmp->next; + } +} diff --git a/srcs/vim b/srcs/vim deleted file mode 100644 index 397db75..0000000 --- a/srcs/vim +++ /dev/null @@ -1 +0,0 @@ -: diff --git a/srcs/wildcards.c b/srcs/wildcards.c index 015fa4d..e50b6af 100644 --- a/srcs/wildcards.c +++ b/srcs/wildcards.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/15 12:53:29 by tomoron #+# #+# */ -/* Updated: 2024/04/16 09:34:47 by tomoron ### ########.fr */ +/* Updated: 2024/04/17 10:16:33 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" @@ -32,14 +32,38 @@ int filename_corresponds(char *wildcard, char *value) return(!*wildcard && !*value); } +t_token *get_all_files(DIR *dir, char *wildcard) +{ + struct dirent *content; + t_token *res; + + res = 0; + content = readdir(dir); + while(content) + { + if(filename_corresponds(wildcard, content->d_name)) + res = token_add_back(res,ft_strdup(content->d_name)); + content = readdir(dir); + } + return(res); +} + +t_token *wildcards_add_back(t_token *res, t_token *next) +{ + if(!res) + return(next); + while(res->next) + res = res->next; + res->next = next; + return(res); +} + t_token *expand_wildcards(t_token *res, char *value) { DIR *dir; char *cwd; - struct dirent *content; - int found; + t_token *new; - found = 0; if(!ft_strchr(value, '*')) return(token_add_back(res, value)); cwd = getcwd(NULL, 100000); @@ -49,19 +73,12 @@ t_token *expand_wildcards(t_token *res, char *value) 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)); - found = 1; - } - content = readdir(dir); - } + new = get_all_files(dir, value); closedir(dir); - if(!found) + if(!new) return(token_add_back(res, value)); free(value); + sort_wildcards_token(new); + res = wildcards_add_back(res, new); return(res); }