-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_ext.c
50 lines (44 loc) · 1.56 KB
/
cmd_ext.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmd_ext.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abdsalah <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/31 08:53:53 by abdsalah #+# #+# */
/* Updated: 2024/10/31 10:51:35 by abdsalah ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
void error_exit(const char *msg, int *fds, int fd_count)
{
int i;
i = -1;
while (++i < fd_count)
{
if (fds[i] != -1)
close(fds[i]);
}
perror(msg);
exit(EXIT_FAILURE);
}
void execute_command(char *cmd, char **envp)
{
char *args[4];
args[0] = "/bin/sh";
args[1] = "-c";
args[2] = cmd;
args[3] = NULL;
execve("/bin/sh", args, envp);
perror("execve failed");
exit(EXIT_FAILURE);
}
void handle_command(int input_fd, int output_fd, char *cmd, char **envp)
{
if (dup2(input_fd, STDIN_FILENO) == -1 || dup2(output_fd, STDOUT_FILENO)
== -1)
error_exit("dup2 failed", (int []){input_fd, output_fd}, 2);
close(input_fd);
close(output_fd);
execute_command(cmd, envp);
}