-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd_exec_command.c
56 lines (53 loc) · 1.55 KB
/
cmd_exec_command.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <unistd.h>
#include "execution.h"
#include "minishell.h"
#include "builtin.h"
#include "libft/libft.h"
/*
* Connect pipe to STDIN_FILENO and STDOUT_FILE_NO.
*
* replace STDIN_FILENO and STDOUT_FILE_NO with pipe by dup2().
*/
static void replace_stdio_with_pipe(t_command_invocation *command,
int pipe_prev_fd[2], int pipe_fd[2])
{
if (pipe_prev_fd[1] >= 0)
{
close(pipe_prev_fd[1]);
if (dup2(pipe_prev_fd[0], STDIN_FILENO) == -1)
put_err_msg_and_exit("error child dup2(in)");
close(pipe_prev_fd[0]);
}
if (command->piped_command)
{
close(pipe_fd[0]);
if (dup2(pipe_fd[1], STDOUT_FILENO) == -1)
put_err_msg_and_exit("error child dup2(out)");
close(pipe_fd[1]);
}
}
/*
* Execute a command.
* This function is supposed to be called in child process.
*
* command: command
* pipe_prev_fd[2]: A pipe that connects the previous and current process.
* pipe_fd[2]: A pipe that connects the current and next process.
*/
void cmd_exec_command(t_command_invocation *command,
int pipe_prev_fd[2], int pipe_fd[2], t_fd_reds_list *in_fd_reds_list)
{
t_builtin_cmd *builtin_func;
set_sighandlers(SIG_DFL);
replace_stdio_with_pipe(command, pipe_prev_fd, pipe_fd);
if (cmd_set_input_file(in_fd_reds_list) == ERROR
|| cmd_set_output_file(command) == ERROR)
exit(EXIT_FAILURE);
if (!command->exec_and_args)
exit(0);
builtin_func = get_builtin_func((char *)command->exec_and_args[0]);
if (builtin_func)
exit(builtin_func((char **)command->exec_and_args));
cmd_execvp((char *)command->exec_and_args[0],
(char **) command->exec_and_args);
}