wildcard sort

This commit is contained in:
2024-04-17 10:49:51 +02:00
parent 483d6579d6
commit 3dcb4e55d7
5 changed files with 91 additions and 18 deletions

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

55
srcs/sort_wildcard_list.c Normal file
View File

@ -0,0 +1,55 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_wildcard_list.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}
}

View File

@ -1 +0,0 @@
:

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}