42_minishell/execution.c

177 lines
4.4 KiB
C
Raw Normal View History

2023-02-21 08:37:41 -05:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* execution.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/21 12:45:16 by cchauvet #+# #+# */
2023-02-28 08:56:28 -05:00
/* Updated: 2023/02/28 14:56:05 by erey-bet ### ########.fr */
2023-02-21 08:37:41 -05:00
/* */
/* ************************************************************************** */
2023-02-09 12:47:05 -05:00
#include "libftx/libftx.h"
#include "minishell.h"
2023-02-21 16:38:00 -05:00
#include <unistd.h>
2023-02-09 12:47:05 -05:00
2023-02-24 14:30:01 -05:00
static char *ft_get_executable_path(t_data *data, char *executable_name)
2023-02-14 07:38:40 -05:00
{
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-21 09:32:22 -05:00
if (executable_name == NULL)
return (NULL);
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);
}
if (access(path, X_OK) == 0)
{
ft_eprintf("minishell: %s: permission denied\n", path);
return (NULL);
}
2023-02-15 14:52:27 -05:00
}
2023-02-14 07:38:40 -05:00
else
{
2023-02-24 14:30:01 -05:00
tab = ft_split(get_value_by_key("PATH", data->env), ':');
2023-02-14 07:38:40 -05:00
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-24 14:30:01 -05:00
data->exit_code = 127;
2023-02-14 11:11:39 -05:00
}
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-24 14:30:01 -05:00
static int ft_executor(t_data *data, t_cmd *cmd)
2023-02-09 12:47:05 -05:00
{
int pid;
2023-02-14 07:38:40 -05:00
int return_value;
char **tab;
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-24 14:30:01 -05:00
tab = env_to_strs(data->env);
2023-02-17 07:27:40 -05:00
if (tab == NULL)
return (1);
2023-02-14 01:21:24 -05:00
dup2(cmd->fd_out, 1);
dup2(cmd->fd_in, 0);
execve(cmd->executable, cmd->args, tab);
2023-02-09 12:47:05 -05:00
}
else
2023-02-14 07:38:40 -05:00
waitpid(pid, &return_value, 0);
2023-02-24 14:30:01 -05:00
data->exit_code = return_value;
2023-02-14 07:38:40 -05:00
return (return_value);
}
2023-02-24 07:13:11 -05:00
static int ft_own_cmd(t_data *data, t_cmd *cmd)
2023-02-21 08:37:41 -05:00
{
2023-02-21 09:07:14 -05:00
int return_code;
2023-02-24 07:13:11 -05:00
int exit_code;
2023-02-21 09:07:14 -05:00
return_code = -1;
if (ft_strcmp(cmd->executable, "pwd") == 0)
return_code = pwd(cmd->fd_out);
else if (ft_strcmp(cmd->executable, "env") == 0)
2023-02-24 07:13:11 -05:00
return_code = print_env(data->env, cmd->fd_out);
2023-02-21 09:07:14 -05:00
else if (ft_strcmp(cmd->executable, "export") == 0)
2023-02-24 07:13:11 -05:00
return_code = (export(data->env,cmd->args + 1, cmd->fd_out));
2023-02-21 09:07:14 -05:00
else if (ft_strcmp(cmd->executable, "cd") == 0)
2023-02-28 07:17:26 -05:00
return_code = (move_folder(cmd->args + 1, cmd->fd_out));
2023-02-24 07:13:11 -05:00
if (ft_strcmp(cmd->executable, "unset") == 0)
return_code = (unset(data->env, cmd->args, cmd->fd_out));
2023-02-21 09:46:21 -05:00
else if (ft_strcmp(cmd->executable, "echo") == 0)
return_code = (echo(cmd->fd_out, cmd->args + 1));
2023-02-21 09:07:14 -05:00
else if (ft_strcmp(cmd->executable, "exit") == 0)
2023-02-24 07:13:11 -05:00
{
2023-02-28 08:56:28 -05:00
exit_code = ft_exit(cmd->args + 1);
2023-02-24 07:13:11 -05:00
if (exit_code != -1)
{
data->exit_code = exit_code;
return_code = -2;
}
2023-02-28 08:57:16 -05:00
else
return_code = -3;
2023-02-24 07:13:11 -05:00
}
2023-02-28 08:57:16 -05:00
if (return_code >= 0)
2023-02-24 14:30:01 -05:00
data->exit_code = return_code;
2023-02-28 08:57:16 -05:00
if (return_code != -1)
cmd->executable = NULL;
2023-02-21 09:07:14 -05:00
return (return_code);
2023-02-21 08:37:41 -05:00
}
2023-02-21 16:12:23 -05:00
int ft_cmds_executor(t_data *data, t_list **cmds)
2023-02-14 07:38:40 -05:00
{
t_cmd *content;
t_list *current;
2023-02-24 14:30:01 -05:00
int exit_code;
2023-02-14 07:38:40 -05:00
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-28 07:10:02 -05:00
exit_code = ft_own_cmd(data, content);
if (exit_code == -1)
2023-02-21 08:37:41 -05:00
{
2023-02-24 14:30:01 -05:00
content->executable = ft_get_executable_path(data,
content->executable);
2023-02-21 08:37:41 -05:00
if (content->executable != NULL)
2023-02-24 14:30:01 -05:00
exit_code = ft_executor(data, content);
2023-02-21 08:37:41 -05:00
}
2023-02-28 07:10:02 -05:00
else if (exit_code == -2)
return (1);
2023-02-24 14:30:01 -05:00
if (ft_gen_exit_code_var(data))
2023-02-21 08:37:41 -05:00
return (1);
2023-02-21 16:38:00 -05:00
if (content->fd_in > 2)
close(content->fd_in);
if (content->fd_out > 2)
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
}