-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipex_bonus.c
More file actions
119 lines (111 loc) · 3.77 KB
/
pipex_bonus.c
File metadata and controls
119 lines (111 loc) · 3.77 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amacarul <amacarul@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/27 13:29:37 by amacarul #+# #+# */
/* Updated: 2024/11/12 15:19:13 by amacarul ### ########.fr */
/* */
/* ************************************************************************** */
#include "libpipex.h"
/*Execute a command by splitting its arguments and checking their validity.
Calls get_path to find the command path, and then executes the command*/
void execute(char *cmd, char **envp, int is_first_cmd)
{
char *path;
char **cmd_args;
cmd_args = ft_split(cmd, ' ');
if (!cmd_args || !cmd_args[0])
handle_cmd_error(cmd, cmd_args, is_first_cmd);
path = get_path(cmd, envp);
if (!path || access(path, F_OK | X_OK) == -1)
{
if (path)
free(path);
handle_cmd_error(cmd, cmd_args, is_first_cmd);
exit(EXIT_FAILURE);
}
if (execve(path, cmd_args, envp) == -1)
{
handle_error("cannot execute command", cmd_args[0]);
free(path);
free_array(cmd_args);
exit (EXIT_FAILURE);
}
}
/*Set up and execute the first command in a pipeline.*/
void first_process(char *file1, char *cmd1, int *pipefd, char **envp)
{
int fd;
if (access(file1, F_OK) == -1 || access(file1, R_OK) == -1)
{
if (access(file1, F_OK) == -1)
handle_error("no such file or directory", file1);
else if (access(file1, R_OK) == -1)
handle_error("permission denied", file1);
close_pipefd(pipefd);
exit (EXIT_FAILURE);
}
fd = open(file1, O_RDONLY);
if (fd == -1)
{
handle_error("error opening file", file1);
close_pipefd(pipefd);
exit (EXIT_FAILURE);
}
close (pipefd[0]);
dup2 (fd, 0);
dup2 (pipefd[1], 1);
close (fd);
close (pipefd[1]);
execute(cmd1, envp, 1);
}
/*Set up and execute the last command in a pipeline.*/
void last_process(int argc, char **argv, int *pipefd, char **envp)
{
int fd;
char *cmd;
cmd = argv[argc - 2];
waitpid(-1, NULL, 0);
fd = open(argv[argc - 1], O_CREAT | O_RDWR | O_TRUNC, 0644);
if (fd == -1 || access(argv[argc - 1], W_OK) == -1)
{
if (fd == -1)
handle_error("error opening file", argv[argc - 1]);
else if (access(argv[argc - 1], W_OK) == -1)
handle_error("permission denied", argv[argc - 1]);
close_pipefd(pipefd);
exit (EXIT_FAILURE);
}
close(pipefd[1]);
dup2 (pipefd[0], 0);
dup2 (fd, 1);
close(pipefd[0]);
close(fd);
execute(cmd, envp, 0);
}
/*Set up and execute an intermediate command in a pipeline.
- waitpid(): waits for any child process to finish
- closes fds it the read end of the current pipe (pipefd[0])
and the write end of previous pipe (prev_pipefd[1])
as there are not used here
- dup2() to redirect stdin to prev_pipefd[0] (read end of previous pipe)
- dup2() to redirect stdout to pipefd[1] (write end of current pipe).
This redirects the cmds output to the next process in the pipeline
- close the original fds after redirection
- call execute() to run the cmd. The cmd reads from the redirected
stdin (prev_pipefd[0]) and writes its output to the redirected stdout
(pipefd[1])*/
void inter_process(char *cmd, int *prev_pipefd, int *pipefd, char **envp)
{
waitpid(-1, NULL, 0);
close(pipefd[0]);
close(prev_pipefd[1]);
dup2(prev_pipefd[0], 0);
dup2(pipefd[1], 1);
close(prev_pipefd[0]);
close(pipefd[1]);
execute(cmd, envp, 0);
}