Files
ft_minipowershell/src/exec/exec_split_cmd.c

137 lines
3.3 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_split_cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/01 14:55:06 by mmoussou #+# #+# */
/* Updated: 2024/06/21 09:58:27 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char *get_cmd_local_path(char *cmd, t_env *env)
{
char *path;
path = env_get_value("PWD", env);
if (!path)
return (NULL);
path = ft_strjoin_free_s1(path, "/");
if (!path)
return (NULL);
path = ft_strjoin_free_s1(path, cmd);
return (path);
}
int switch_cmd_path(t_cmd *cmd, t_env *env)
{
if (cmd->cmd[0] == '.' && cmd->cmd[1] == '/')
cmd->cmd = get_cmd_local_path(cmd->cmd, env);
else if (cmd->cmd[0] != '/')
cmd->cmd = get_path(env_get_value("PATH", env), cmd->cmd);
if (!(cmd->cmd))
return (-1);
return (0);
}
int exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t)
{
int fork_pid;
int status;
status = switch_cmd_path(cmd, env_t);
if (status == -1 || access(cmd->cmd, X_OK))
return (-1);
fork_pid = fork();
if (fork_pid == -1)
return (-1);
if (!fork_pid)
{
status = dup2(cmd->infile, STDIN_FILENO);
if (status == -1)
exit(-1);
if (cmd->outfile != STDOUT_FILENO)
status = dup2(STDOUT_FILENO, cmd->outfile);
else
status = dup2(STDOUT_FILENO, STDIN_FILENO);
if (status == -1)
exit(-1);
execve(cmd->cmd, cmd->argv, env);
exit(-1);
}
return (0);
}
int exec_last_cmd(t_cmd *cmd, char **env, t_env *env_t)
{
int fork_pid;
int status;
status = switch_cmd_path(cmd, env_t);
if (status == -1 || access(cmd->cmd, X_OK))
return (-1);
fork_pid = fork();
if (fork_pid == -1)
return (-1);
if (!fork_pid)
{
status = dup2(cmd->infile, STDIN_FILENO);
if (status == -1)
exit(-1);
status = dup2(STDOUT_FILENO, cmd->outfile);
if (status == -1)
exit(-1);
execve(cmd->cmd, cmd->argv, env);
exit(-1);
}
return (0);
}
int exec_split_cmd(t_list *list_cmd, t_env *env)
{
char **env_array;
int status;
int return_code;
int i;
env_array = env_get(env);
if (!env_array)
return (-1);
i = ft_lstsize(list_cmd);
while (list_cmd->next)
{
status = exec_single_cmd(list_cmd->content, env_array, env);
if (status == -1)
{
ft_free("a", &env_array);
return (-1);
}
list_cmd = list_cmd->next;
}
status = exec_last_cmd(list_cmd->content, env_array, env);
ft_free("a", &env_array);
if (status == -1)
return (-1);
while (i)
{
waitpid(-1, &return_code, 0);
i--;
}
if (WIFEXITED(return_code))
printf("minishell: %d\n", WEXITSTATUS(return_code));
else
{
if (WIFSIGNALED(return_code))
{
if (WCOREDUMP(return_code))
printf("minishell: %d\n", WTERMSIG(return_code));
else
printf("minishell: %d\n", WTERMSIG(return_code));
}
}
return (0);
}