wildcard sort
This commit is contained in:
3
Makefile
3
Makefile
@ -6,7 +6,7 @@
|
|||||||
# By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ #
|
# By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2023/07/28 00:35:01 by tomoron #+# #+# #
|
# 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\
|
here_doc_utils.c\
|
||||||
export.c\
|
export.c\
|
||||||
input_redirections.c\
|
input_redirections.c\
|
||||||
|
sort_wildcard_list.c\
|
||||||
output_redirections.c\
|
output_redirections.c\
|
||||||
builtins.c\
|
builtins.c\
|
||||||
wildcards.c\
|
wildcards.c\
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
|
/* 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_cmd_type(t_cmd *cmd);
|
||||||
int is_operand_type(t_cmd *cmd);
|
int is_operand_type(t_cmd *cmd);
|
||||||
int set_echoctl(int value);
|
int set_echoctl(int value);
|
||||||
|
void sort_wildcards_token(t_token *list);
|
||||||
int print_env(t_env *env);
|
int print_env(t_env *env);
|
||||||
t_cmd *free_cmd(t_cmd *cmd);
|
t_cmd *free_cmd(t_cmd *cmd);
|
||||||
void print_syntax_error_bonus(t_cmd *cmd);
|
void print_syntax_error_bonus(t_cmd *cmd);
|
||||||
|
55
srcs/sort_wildcard_list.c
Normal file
55
srcs/sort_wildcard_list.c
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -6,7 +6,7 @@
|
|||||||
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/04/15 12:53:29 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"
|
#include "minishell.h"
|
||||||
@ -32,14 +32,38 @@ int filename_corresponds(char *wildcard, char *value)
|
|||||||
return(!*wildcard && !*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)
|
t_token *expand_wildcards(t_token *res, char *value)
|
||||||
{
|
{
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
char *cwd;
|
char *cwd;
|
||||||
struct dirent *content;
|
t_token *new;
|
||||||
int found;
|
|
||||||
|
|
||||||
found = 0;
|
|
||||||
if(!ft_strchr(value, '*'))
|
if(!ft_strchr(value, '*'))
|
||||||
return(token_add_back(res, value));
|
return(token_add_back(res, value));
|
||||||
cwd = getcwd(NULL, 100000);
|
cwd = getcwd(NULL, 100000);
|
||||||
@ -49,19 +73,12 @@ t_token *expand_wildcards(t_token *res, char *value)
|
|||||||
free(cwd);
|
free(cwd);
|
||||||
if(!dir)
|
if(!dir)
|
||||||
return(token_add_back(res, value));
|
return(token_add_back(res, value));
|
||||||
content = readdir(dir);
|
new = get_all_files(dir, value);
|
||||||
while(content)
|
|
||||||
{
|
|
||||||
if(filename_corresponds(value, content->d_name))
|
|
||||||
{
|
|
||||||
res = token_add_back(res,ft_strdup(content->d_name));
|
|
||||||
found = 1;
|
|
||||||
}
|
|
||||||
content = readdir(dir);
|
|
||||||
}
|
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
if(!found)
|
if(!new)
|
||||||
return(token_add_back(res, value));
|
return(token_add_back(res, value));
|
||||||
free(value);
|
free(value);
|
||||||
|
sort_wildcards_token(new);
|
||||||
|
res = wildcards_add_back(res, new);
|
||||||
return(res);
|
return(res);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user