core: rebuild of parsing and the execution

This commit is contained in:
Camille Chauvet
2023-03-10 12:32:39 +01:00
parent a18c4cae82
commit d36d9c92f5
54 changed files with 708 additions and 618 deletions

17
utils/fd.c Normal file
View File

@ -0,0 +1,17 @@
#include "./utils.h"
void ft_closer(int fds[2])
{
if (fds[0] > 2)
close(fds[0]);
if (fds[1] > 2)
close(fds[1]);
}
void ft_add_fd(int fds[2], int fd)
{
if (fds[0] == -1)
fds[0] = fd;
else
fds[1] = fd;
}

74
utils/ft_get_executable.c Normal file
View File

@ -0,0 +1,74 @@
#include "./utils.h"
#include <unistd.h>
char *ft_get_executable_with_path(const char *name)
{
char *path;
if (access(name, F_OK) == 0)
{
ft_eprintf("minishell: %s bash: No such file or directery\n");
return (NULL);
}
if (access(name, X_OK) != 0)
{
ft_eprintf("minishell: %s: permission denied\n", name);
return (NULL);
}
path = ft_strdup(name);
if (path == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (NULL);
}
return (path);
}
char *ft_get_executable_without_path(t_list **env, const char *name)
{
char **tab;
char *paths;
char *path;
size_t i;
path = NULL;
paths = get_value_by_key("PATH", env);
if (paths == NULL)
path = "";
tab = ft_split(paths, ':');
if (tab == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (NULL);
}
i = 0;
while (tab[i] != NULL)
{
path = ft_strmerger(3, tab[i], "/", name);
if (path == NULL)
{
ft_eprintf("minishell: malloc failed\n");
break;
}
if (access(path, X_OK))
break;
free(path);
path = NULL;
i++;
}
ft_freer_tab_ultimate(1, tab);
if (path == NULL)
ft_eprintf("minishell: %s: command not found\n", name);
return (path);
}
char *ft_get_executable(t_list **env, const char *name)
{
char *path;
if (name[0] == '.' || name[0] == '/')
path = ft_get_executable_with_path(name);
else
path = ft_get_executable_without_path(env, name);
return (path);
}

Binary file not shown.

Binary file not shown.

View File

@ -14,6 +14,8 @@
# define UTILS_H
# include <stdlib.h>
# include "../libftx/libftx.h"
# include "../data/data.h"
# include "../env/env.h"
size_t ft_strncpy(char *dst, const char *src, size_t n);
int ft_is_in_quote(const char *str, size_t n);
@ -26,5 +28,9 @@ char **ft_split_quoted(const char *s, char c);
void ft_strshift(char *str, int shift);
char *ft_quote_remover(char *str);
int ft_atoi_check(const char *nptr);
char *ft_get_executable(t_list **env, const char *name);
void ft_closer(int fds[2]);
void ft_add_fd(int fds[2], int fd);
#endif