42_minishell/execution.c

119 lines
2.2 KiB
C
Raw Normal View History

2023-02-09 12:47:05 -05:00
#include "libftx/libftx.h"
#include "minishell.h"
2023-02-14 07:38:40 -05:00
#include "utils/utils.h"
2023-02-09 12:47:05 -05:00
#include <unistd.h>
2023-02-14 07:38:40 -05:00
static char *ft_get_variable(char **env, char *variable)
2023-02-09 12:47:05 -05:00
{
2023-02-14 07:38:40 -05:00
size_t i;
2023-02-09 12:47:05 -05:00
2023-02-14 07:38:40 -05:00
i = 0;
2023-02-14 11:11:39 -05:00
while (env[i] != NULL)
2023-02-14 07:38:40 -05:00
{
2023-02-14 11:11:39 -05:00
if (ft_strncmp(variable, env[i], ft_strlen(variable)) == 0)
return (ft_strchr(env[i], '=') + 1);
i++;
2023-02-14 07:38:40 -05:00
}
return (NULL);
2023-02-09 12:47:05 -05:00
}
2023-02-14 07:38:40 -05:00
static char *ft_get_executable_path(char *executable_name, char **env)
{
char *path;
2023-02-14 11:11:39 -05:00
char *temp;
2023-02-14 07:38:40 -05:00
char **tab;
size_t i;
2023-02-14 11:11:39 -05:00
path = NULL;
2023-02-14 07:38:40 -05:00
if (executable_name[0] == '.' || executable_name[0] == '/')
2023-02-15 14:52:27 -05:00
{
2023-02-14 11:11:39 -05:00
path = ft_strdup(executable_name);
2023-02-15 14:52:27 -05:00
if (path == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (NULL);
}
}
2023-02-14 07:38:40 -05:00
else
{
tab = ft_split(ft_get_variable(env, "PATH"), ':');
if (tab == NULL)
return (NULL);
i = 0;
while (tab[i] != NULL)
{
2023-02-14 11:11:39 -05:00
temp = ft_strmerger(3, tab[i], "/", executable_name);
if (temp == NULL)
2023-02-14 07:38:40 -05:00
{
2023-02-14 11:11:39 -05:00
ft_freer_tab_ultimate(1, tab);
2023-02-14 07:38:40 -05:00
free(executable_name);
2023-02-14 11:11:39 -05:00
ft_eprintf("minishell: malloc failed\n");
}
if (access(temp, X_OK) == 0)
{
path = temp;
2023-02-14 07:38:40 -05:00
break ;
}
2023-02-14 11:11:39 -05:00
free(temp);
2023-02-14 07:38:40 -05:00
i++;
}
2023-02-14 11:11:39 -05:00
if (path == NULL)
{
ft_eprintf("%s: command not found\n", executable_name);
}
2023-02-14 07:38:40 -05:00
ft_freer_tab_ultimate(1, tab);
}
return (path);
}
2023-02-09 12:47:05 -05:00
2023-02-14 07:38:40 -05:00
static int ft_excutor(t_cmd *cmd, char **env)
2023-02-09 12:47:05 -05:00
{
int pid;
2023-02-14 07:38:40 -05:00
int return_value;
2023-02-09 12:47:05 -05:00
2023-02-15 14:52:27 -05:00
if (cmd->fd_in == -1 || cmd->fd_out == -1)
return (1);
2023-02-09 12:47:05 -05:00
pid = fork();
if (pid == -1)
return (1);
if (pid == 0)
{
2023-02-14 01:21:24 -05:00
dup2(cmd->fd_out, 1);
dup2(cmd->fd_in, 0);
2023-02-14 11:11:39 -05:00
execve(cmd->executable, cmd->args, env);
2023-02-09 12:47:05 -05:00
}
else
2023-02-14 07:38:40 -05:00
waitpid(pid, &return_value, 0);
return (return_value);
}
int ft_cmds_executor(t_list **cmds, char **env)
{
t_cmd *content;
t_list *current;
int fds[2];
current = *cmds;
2023-02-14 11:11:39 -05:00
while (current != NULL)
2023-02-14 07:38:40 -05:00
{
2023-02-14 11:11:39 -05:00
content = current->content;
if (current->next != NULL)
2023-02-14 07:38:40 -05:00
{
2023-02-14 11:11:39 -05:00
if (pipe(fds) == -1)
{
ft_eprintf("minishell: pipe failed\n");
return (1);
}
content->fd_out = fds[1];
((t_cmd *) current->next->content)->fd_in = fds[0];
2023-02-14 07:38:40 -05:00
}
2023-02-14 11:11:39 -05:00
content->executable = ft_get_executable_path(content->executable, env);
if (content->executable != NULL)
ft_excutor(content, env);
close(content->fd_in);
close(content->fd_out);
2023-02-14 07:38:40 -05:00
current = current->next;
}
return (0);
2023-02-09 12:47:05 -05:00
}