2025-02-28 18:48:30 +01:00
|
|
|
/* ************************************************************************** */
|
|
|
|
|
/* */
|
|
|
|
|
/* ::: :::::::: */
|
|
|
|
|
/* wildcard_exp.c :+: :+: :+: */
|
|
|
|
|
/* +:+ +:+ +:+ */
|
2025-03-06 12:45:05 +01:00
|
|
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
2025-02-28 18:48:30 +01:00
|
|
|
/* +#+#+#+#+#+ +#+ */
|
2025-03-06 17:37:08 +01:00
|
|
|
/* Created: 2025/02/23 15:02:59 by jguelen #+# #+# */
|
|
|
|
|
/* Updated: 2025/03/06 17:40:42 by jguelen ### ########.fr */
|
2025-02-28 18:48:30 +01:00
|
|
|
/* */
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
2025-03-06 12:45:05 +01:00
|
|
|
#include "ft_printf.h"
|
2025-02-28 18:48:30 +01:00
|
|
|
#include "subst.h"
|
2025-03-06 12:45:05 +01:00
|
|
|
#include "unistd.h"
|
2025-03-06 17:37:08 +01:00
|
|
|
#include "minishell.h"
|
2025-02-28 18:48:30 +01:00
|
|
|
|
2025-03-06 17:37:08 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
/* NOTE: Use of errno and the setting of it was OKed by Alexandru in this */
|
|
|
|
|
/* context. */
|
|
|
|
|
/******************************************************************************/
|
2025-02-28 18:48:30 +01:00
|
|
|
/*
|
2025-03-06 17:37:08 +01:00
|
|
|
** Returns a directory stream corresponding to the current directory.
|
|
|
|
|
**
|
2025-02-28 18:48:30 +01:00
|
|
|
*/
|
2025-03-06 17:37:08 +01:00
|
|
|
DIR *open_current_dir(void)
|
|
|
|
|
{
|
|
|
|
|
char *cur_dir_path;
|
|
|
|
|
size_t size;
|
|
|
|
|
DIR *dir;
|
|
|
|
|
|
|
|
|
|
size = PATH_SIZE_INIT;
|
|
|
|
|
cur_dir_path = malloc(size * sizeof(char));
|
|
|
|
|
if (!cur_dir_path)
|
|
|
|
|
return (NULL);
|
|
|
|
|
while (getcwd(cur_dir_path, size) == NULL)
|
|
|
|
|
{
|
|
|
|
|
if (errno != ERANGE)
|
|
|
|
|
return (NULL);
|
|
|
|
|
free(cur_dir_path);
|
|
|
|
|
size *= 2;
|
|
|
|
|
cur_dir_path = malloc(size * sizeof(char));
|
|
|
|
|
if (!cur_dir_path)
|
|
|
|
|
return (NULL);
|
|
|
|
|
}
|
|
|
|
|
errno = 0;
|
|
|
|
|
dir = opendir(cur_dir_path);
|
|
|
|
|
free(cur_dir_path);
|
|
|
|
|
return (dir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
**
|
|
|
|
|
*/
|
|
|
|
|
static t_wordlist *add_file_to_list(t_wordlist **list, char *filename)
|
|
|
|
|
{
|
|
|
|
|
t_worddesc *file_desc;
|
|
|
|
|
char *copy;
|
|
|
|
|
|
|
|
|
|
copy = ft_strdup(filename);
|
|
|
|
|
if (!copy)
|
|
|
|
|
return (wordlist_destroy(*list), NULL);
|
|
|
|
|
file_desc = worddesc_create(copy, '\0');
|
|
|
|
|
if (!file_desc)
|
|
|
|
|
{
|
|
|
|
|
wordlist_destroy(*list);
|
|
|
|
|
return (NULL);
|
|
|
|
|
}
|
|
|
|
|
*list = wordlist_push(*list, file_desc);
|
|
|
|
|
return (*list);
|
|
|
|
|
}
|
2025-02-28 18:48:30 +01:00
|
|
|
|
|
|
|
|
/*
|
2025-03-06 17:37:08 +01:00
|
|
|
** Returns true if and only if filename is recognized by pattern, false
|
|
|
|
|
** otherwise.
|
|
|
|
|
** Takes only into account the * wildcard or ?, those characters
|
|
|
|
|
** NOTE: for a pattern to accept '.' as the first character of a filename
|
|
|
|
|
** it must be explicitly matched (only for the first character though).
|
|
|
|
|
** Similarly, '/' is never to be matched except if given explicitly.
|
|
|
|
|
*/
|
|
|
|
|
bool fits_pattern(char *string, char *pattern)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** TODO Check if return value correct regarding the manual specifacally the
|
|
|
|
|
** following:
|
|
|
|
|
** cf Bash scans each word for the character '*'.
|
|
|
|
|
**
|
|
|
|
|
** If one of these characters appears, and is not quoted, then the word is
|
|
|
|
|
** regarded as a PATTERN, and replaced with an alphabetically sorted list
|
|
|
|
|
** of filenames matching the pattern (see: Pattern Matching). If no matching
|
|
|
|
|
** filenames are found, the word is left unchanged.
|
|
|
|
|
*/
|
|
|
|
|
/*
|
|
|
|
|
** A function designed to present all possible * or ? filename expansions
|
|
|
|
|
** for the current directory. (? is not asked by the subject).
|
2025-02-28 18:48:30 +01:00
|
|
|
** Does not take into account any other wildcard and does only search the
|
|
|
|
|
** current working directory.
|
|
|
|
|
** @PARAM A C compliant character string representing a pattern for a filename.
|
2025-03-06 17:37:08 +01:00
|
|
|
** @RETURN
|
2025-02-28 18:48:30 +01:00
|
|
|
*/
|
|
|
|
|
t_wordlist *expand_star(char *file_pattern)
|
|
|
|
|
{
|
2025-03-06 17:37:08 +01:00
|
|
|
struct dirent *new;
|
|
|
|
|
DIR *current_dir;
|
|
|
|
|
t_wordlist *file_wordlist;
|
|
|
|
|
|
|
|
|
|
current_dir = open_current_dir();
|
|
|
|
|
if (current_dir == NULL)
|
|
|
|
|
return (NULL);
|
|
|
|
|
errno = 0;
|
|
|
|
|
file_wordlist = NULL;
|
|
|
|
|
new = readdir(current_dir);
|
|
|
|
|
while (new)
|
|
|
|
|
{
|
|
|
|
|
if (fits_pattern(new->d_name, file_pattern))
|
|
|
|
|
{
|
|
|
|
|
if (add_file_to_list(&file_wordlist, new->d_name) == NULL)
|
|
|
|
|
return (NULL);
|
|
|
|
|
}
|
|
|
|
|
errno = 0;
|
|
|
|
|
new = readdir(current_dir);
|
|
|
|
|
}
|
|
|
|
|
if (errno)
|
|
|
|
|
return (wordlist_destroy(file_wordlist), NULL);
|
|
|
|
|
if (!file_wordlist)
|
|
|
|
|
add_file_to list(&file_wordlist, file_pattern);
|
|
|
|
|
return (file_wordlist);
|
2025-02-28 18:48:30 +01:00
|
|
|
}
|