-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfd_handling.c
58 lines (52 loc) · 1.83 KB
/
fd_handling.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
57
58
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fd_handling.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cdomet-d <cdomet-d@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/07 22:39:05 by jauseff #+# #+# */
/* Updated: 2024/05/23 11:24:28 by cdomet-d ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
void init_fds(t_fd *fd, t_input *in)
{
if (in)
fd->pnb = count_pipes(in);
fd->pfd[R] = -1;
fd->pfd[W] = -1;
fd->hfd = -1;
fd->ffd = -1;
fd ->tmpin = -1;
fd->pid = -1;
}
void close_pipe_read(t_fd *fd)
{
if (fd->pfd[R] != -1)
if (close(fd->pfd[R]) == -1)
print_error(errno, "close_read (pfd[R])");
}
void close_pipe_write(t_fd *fd)
{
if (fd->pfd[W] != -1)
if (close(fd->pfd[W]) == -1)
print_error(errno, "close_write (pfd[W])");
}
void close_pfd(t_fd *fd)
{
close_pipe_read(fd);
close_pipe_write(fd);
init_fds(fd, NULL);
}
void reset_stds(int tmpstdin, int tmpstdout)
{
if (dup2(tmpstdin, STDIN_FILENO) == -1)
print_error(errno, "reset_stds (reopening STDIN)");
if (dup2(tmpstdout, STDOUT_FILENO) == -1)
print_error(errno, "reset_stds (reopening STDOUT)");
if (close(tmpstdin) == -1)
print_error(errno, "reset_stds (closing tmpstdin)");
if (close(tmpstdout) == -1)
print_error(errno, "reset_stds (closing tmpstdin)");
}