wildcards

This commit is contained in:
2024-04-15 18:55:52 +02:00
parent 2862f6bab5
commit 5ab0c197ce
4 changed files with 69 additions and 4 deletions

View File

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

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/13 13:31:40 by babonnet ### ########.fr */
/* Updated: 2024/04/15 16:42:28 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,6 +24,7 @@
# include <signal.h>
# include <stdint.h>
# include <termios.h>
# include <dirent.h>
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);

View File

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

62
srcs/wildcards.c Normal file
View File

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