42_minishell/execution.c
Camille Chauvet 5bfc613126 fix: exit
2023-03-06 14:22:30 +01:00

184 lines
4.5 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* execution.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/21 12:45:16 by cchauvet #+# #+# */
/* Updated: 2023/02/28 14:56:05 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftx/libftx.h"
#include "minishell.h"
#include <unistd.h>
static char *ft_get_executable_path(t_data *data, char *executable_name)
{
char *path;
char *temp;
char **tab;
size_t i;
if (executable_name == NULL)
return (NULL);
path = NULL;
if (executable_name[0] == '.' || executable_name[0] == '/')
{
path = ft_strdup(executable_name);
if (path == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (NULL);
}
if (access(path, X_OK) == 0)
{
ft_eprintf("minishell: %s: permission denied\n", path);
return (NULL);
}
}
else
{
tab = ft_split(get_value_by_key("PATH", data->env), ':');
if (tab == NULL)
return (NULL);
i = 0;
while (tab[i] != NULL)
{
temp = ft_strmerger(3, tab[i], "/", executable_name);
if (temp == NULL)
{
ft_freer_tab_ultimate(1, tab);
free(executable_name);
ft_eprintf("minishell: malloc failed\n");
}
if (access(temp, X_OK) == 0)
{
path = temp;
break ;
}
free(temp);
i++;
}
if (path == NULL)
{
ft_eprintf("%s: command not found\n", executable_name);
data->exit_code = 127;
}
ft_freer_tab_ultimate(1, tab);
}
return (path);
}
static int ft_executor(t_data *data, t_cmd *cmd)
{
int pid;
int return_value;
char **tab;
if (cmd->fd_in == -1 || cmd->fd_out == -1)
return (1);
pid = fork();
if (pid == -1)
return (1);
if (pid == 0)
{
tab = env_to_strs(data->env);
if (tab == NULL)
return (1);
dup2(cmd->fd_out, 1);
dup2(cmd->fd_in, 0);
if (cmd->fd_out > 2)
close(cmd->fd_out);
if (cmd->fd_in > 2)
close(cmd->fd_in);
execve(cmd->executable, cmd->args, tab);
}
else
waitpid(pid, &return_value, 0);
data->exit_code = return_value;
return (return_value);
}
static int ft_own_cmd(t_data *data, t_cmd *cmd)
{
int return_code;
int exit_code;
return_code = -1;
if (ft_strcmp(cmd->executable, "pwd") == 0)
return_code = pwd(cmd->fd_out);
else if (ft_strcmp(cmd->executable, "env") == 0)
return_code = print_env(data->env, cmd->fd_out);
else if (ft_strcmp(cmd->executable, "export") == 0)
return_code = (export(data->env,cmd->args + 1, cmd->fd_out));
else if (ft_strcmp(cmd->executable, "cd") == 0)
return_code = (move_folder(cmd->args + 1, cmd->fd_out));
if (ft_strcmp(cmd->executable, "unset") == 0)
return_code = (unset(data->env, cmd->args, cmd->fd_out));
else if (ft_strcmp(cmd->executable, "echo") == 0)
return_code = (echo(cmd->fd_out, cmd->args + 1));
else if (ft_strcmp(cmd->executable, "exit") == 0)
{
exit_code = ft_exit(cmd->args + 1);
if (exit_code > -1)
{
data->exit_code = exit_code;
return_code = -2;
}
else
{
data->exit_code = 1;
return_code = -3;
}
}
if (return_code >= 0)
data->exit_code = return_code;
if (return_code != -1)
cmd->executable = NULL;
return (return_code);
}
int ft_cmds_executor(t_data *data, t_list **cmds)
{
t_cmd *content;
t_list *current;
int exit_code;
int fds[2];
current = *cmds;
while (current != NULL)
{
content = current->content;
if (current->next != NULL)
{
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];
}
exit_code = ft_own_cmd(data, content);
if (exit_code == -1)
{
content->executable = ft_get_executable_path(data,
content->executable);
if (content->executable != NULL)
exit_code = ft_executor(data, content);
}
else if (exit_code == -2)
return (1);
if (ft_gen_exit_code_var(data))
return (1);
if (content->fd_in > 2)
close(content->fd_in);
if (content->fd_out > 2)
close(content->fd_out);
current = current->next;
}
return (0);
}