forked from 42-shell/minishell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute_command_redir_undo.c
46 lines (42 loc) · 1.55 KB
/
execute_command_redir_undo.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* execute_command_redir_undo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jkong <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/10 15:18:12 by jkong #+# #+# */
/* Updated: 2022/06/24 20:59:01 by jkong ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include "safe_io.h"
#include <unistd.h>
void add_undo_redirects(t_shell *sh)
{
int fd;
fd = dup(STDIN_FILENO);
if (fd < 0)
exit_fail("dup");
sh->redir_undo[0] = fd;
fd = dup(STDOUT_FILENO);
if (fd < 0)
exit_fail("dup");
sh->redir_undo[1] = fd;
fd = dup(STDERR_FILENO);
if (fd < 0)
exit_fail("dup");
sh->redir_undo[2] = fd;
}
void cleanup_redirects(t_shell *sh)
{
if (dup2(sh->redir_undo[0], STDIN_FILENO) < 0)
exit_fail("dup");
close(sh->redir_undo[0]);
if (dup2(sh->redir_undo[1], STDOUT_FILENO) < 0)
exit_fail("dup");
close(sh->redir_undo[1]);
if (dup2(sh->redir_undo[2], STDERR_FILENO) < 0)
exit_fail("dup");
close(sh->redir_undo[2]);
}