Files
42_INT_07_minishell/srcs/HUGO_WIP_execute.c
2021-11-07 04:41:17 +01:00

78 lines
1.5 KiB
C

#include "minishell.h"
void close_fd(t_cmd *cmd)
{
if (cmd->fd_in != 0)
close(cmd->fd_in);
if (cmd->fd_out != 1)
close(cmd->fd_out);
}
void execute_cmd(char **envp, t_cmd **cmd_arr)
{
pid_t pid;
pid_t wpid;
int status;
int i;
i = 0;
while(cmd_arr[i])
{
pid = fork();
if (pid == 0)
{
if (cmd_arr[i]->fd_in != 0)
dup2(cmd_arr[i]->fd_in, STDIN_FILENO);
if (cmd_arr[i]->fd_out != 1)
dup2(cmd_arr[i]->fd_out, STDOUT_FILENO);
//close_fd(cmd_arr[i]);
//Must close all fds, not just the two cmd_arr[i] fd
execve(cmd_arr[i]->argv[0], cmd_arr[i]->argv, envp);
}
else
close_fd(cmd_arr[i]); // Close here or after all execve() for simplicity ?
i++;
}
// waitpid pour la derniere commande (pour '$?')
while ((wpid = wait(&status)) > 0);
}
/*
int handle_fdd(char *input, int fdin, t_cmd *cmd)
{
int *pipes_fd;
int next_in;
char *tmp;
cmd->fd_in = fdin;
cmd->fd_out = 1;
next_in = 0;
if (input + 1)
{
pipes_fd = calloc(2, sizeof(int));
pipe(pipes_fd);
cmd->fd_out = pipes_fd[1];
next_in = pipes_fd[0];
}
tmp = ft_strchr(input, '>');
if (tmp)
{
tmp[0] = '\0';
tmp = ft_strtrim(tmp + 2, " "); // +2 for "> "
if (cmd->fd_out != 1)
close(cmd->fd_out);
cmd->fd_out = open(tmp, O_RDWR | O_TRUNC);
}
tmp = ft_strchr(input, '<');
if (tmp)
{
tmp[0] = '\0';
tmp = ft_strtrim(tmp + 2, " "); // +2 for "> "
if (cmd->fd_in != 0)
close(cmd->fd_in);
cmd->fd_in = open(tmp, O_RDONLY);
}
printf(" handle_fd: %s\n", input);
return (next_in);
} */