mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
Expansion: post-rebasing
This commit is contained in:
parent
dd4f109d38
commit
ce24304e34
3 changed files with 175 additions and 11 deletions
65
src/subst/simple_filename_exp.c
Normal file
65
src/subst/simple_filename_exp.c
Normal file
|
|
@ -0,0 +1,65 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* simple_filename_exp.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/03/02 13:40:10 by jguelen #+# #+# */
|
||||||
|
/* Updated: 2025/03/05 10:58:44 by jguelen ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
#include "libft.h"
|
||||||
|
#include "ft_errno.h"
|
||||||
|
|
||||||
|
char *alloc_path(char *path_dir, char *name)
|
||||||
|
{
|
||||||
|
char *path;
|
||||||
|
size_t dir_len;
|
||||||
|
size_t name_len;
|
||||||
|
|
||||||
|
dir_len = ft_strlen(path_dir);
|
||||||
|
name_len = ft_strlen(name);
|
||||||
|
path = malloc((dir_len + name_len + 2) * sizeof(char));
|
||||||
|
if (!path)
|
||||||
|
return (NULL);
|
||||||
|
ft_memcpy(path, path_dir, dir_len);
|
||||||
|
path[dir_len] = '/';
|
||||||
|
ft_memcpy(path + dir_len + 1, name, name_len);
|
||||||
|
path[dir_len + name_len + 1] = '\0';
|
||||||
|
return (path);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char **get_paths_array(t_env *env)
|
||||||
|
{
|
||||||
|
char **path_array;
|
||||||
|
char *path_val;
|
||||||
|
|
||||||
|
path_val = env_get_val(env, "PATH");
|
||||||
|
if (path_val == NULL)
|
||||||
|
return (NULL);
|
||||||
|
path_array = minishell_split(path_val, ':');
|
||||||
|
return (path_array);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *filepath_from_env(char *filename, t_minishell *app)
|
||||||
|
{
|
||||||
|
char *filepath;
|
||||||
|
char **path;
|
||||||
|
|
||||||
|
path = get_paths_array(app->env);
|
||||||
|
if (path == NULL)
|
||||||
|
return (NULL);
|
||||||
|
while (path[i])
|
||||||
|
{
|
||||||
|
filepath = alloc_path(path[i], filename);
|
||||||
|
if (access(filepath, F_OK) == 0)
|
||||||
|
return (filepath);
|
||||||
|
free(filepath);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
destroy_split_array(path);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
@ -5,8 +5,8 @@
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/06 13:06/10 by khais #+# #+# */
|
/* Created: 2025/02/23 15:01:40 by jguelen #+# #+# */
|
||||||
/* Updated: 2025/03/06 13:06:10 by khais ### ########.fr */
|
/* Updated: 2025/03/06 17:41:07 by jguelen ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -15,6 +15,8 @@
|
||||||
|
|
||||||
# include "../parser/wordlist/wordlist.h"
|
# include "../parser/wordlist/wordlist.h"
|
||||||
# include "../minishell.h"
|
# include "../minishell.h"
|
||||||
|
# include <dirent.h>
|
||||||
|
# include <unistd.h>
|
||||||
|
|
||||||
char *expand_question_mark(t_minishell *app);
|
char *expand_question_mark(t_minishell *app);
|
||||||
t_wordlist *wordlist_var_expansion(t_wordlist *list, t_minishell *app);
|
t_wordlist *wordlist_var_expansion(t_wordlist *list, t_minishell *app);
|
||||||
|
|
|
||||||
|
|
@ -5,30 +5,127 @@
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/06 13:02/36 by khais #+# #+# */
|
/* Created: 2025/02/23 15:02:59 by jguelen #+# #+# */
|
||||||
/* Updated: 2025/03/06 13:02:36 by khais ### ########.fr */
|
/* Updated: 2025/03/06 17:40:42 by jguelen ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "ft_printf.h"
|
#include "ft_printf.h"
|
||||||
#include "subst.h"
|
#include "subst.h"
|
||||||
#include "unistd.h"
|
#include "unistd.h"
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
/* NOTE: Use of errno and the setting of it was OKed by Alexandru in this */
|
||||||
|
/* context. */
|
||||||
|
/******************************************************************************/
|
||||||
/*
|
/*
|
||||||
** TODO
|
** Returns a directory stream corresponding to the current directory.
|
||||||
|
**
|
||||||
*/
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** A function designed to present all possible * filename expansions for the
|
**
|
||||||
** current directory.
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** 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).
|
||||||
** Does not take into account any other wildcard and does only search the
|
** Does not take into account any other wildcard and does only search the
|
||||||
** current working directory.
|
** current working directory.
|
||||||
** @PARAM A C compliant character string representing a pattern for a filename.
|
** @PARAM A C compliant character string representing a pattern for a filename.
|
||||||
** @RETURN A NULL-terminated pointer to a wordlist
|
** @RETURN
|
||||||
*/
|
*/
|
||||||
t_wordlist *expand_star(char *file_pattern)
|
t_wordlist *expand_star(char *file_pattern)
|
||||||
{
|
{
|
||||||
(void)file_pattern;
|
struct dirent *new;
|
||||||
ft_dprintf(STDERR_FILENO, "Not implemented: expand_star\n");
|
DIR *current_dir;
|
||||||
return (NULL);
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue