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
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* redir.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/15 11:05:34 by charles #+# #+# */
/* Updated: 2020/09/16 16:17:09 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include "eval.h"
static int st_open_replace(char *filename, int *fd, int oflag)
{
if (fd == NULL)
return (EVAL_FATAL);
if (*fd != FD_NONE)
close(*fd);
if (oflag & O_CREAT)
*fd = open(filename, oflag, 0644);
else
*fd = open(filename, oflag);
if (*fd == -1)
{
errorf("%s: %s\n", filename, strerror(errno));
free(filename);
return (1);
}
return (0);
}
static int st_open_replace_dispatch(char *filename, int fds[2], enum e_tok tag)
{
int *fd;
int oflag;
fd = NULL;
oflag = 0;
if (tag == TAG_REDIR_IN)
{
fd = &fds[FD_READ];
oflag = O_RDONLY;
}
else if (tag == TAG_REDIR_OUT)
{
fd = &fds[FD_WRITE];
oflag = O_WRONLY | O_CREAT | O_TRUNC;
}
else if (tag == TAG_REDIR_APPEND)
{
fd = &fds[FD_WRITE];
oflag = O_WRONLY | O_CREAT | O_APPEND;
}
return (st_open_replace(filename, fd, oflag));
}
int redir_extract(t_tok_lst **redirs, t_env env, int fds[2])
{
t_tok_lst *after;
t_tok_lst *curr;
char *filename;
int status;
if (*redirs == NULL)
return (0);
if (!((*redirs)->tag & TAG_IS_REDIR) || (*redirs)->next == NULL
|| !((*redirs)->next->tag & TAG_IS_STR))
return (EVAL_FATAL);
curr = (*redirs)->next;
after = NULL;
while (curr != NULL && curr->tag & TAG_IS_STR)
{
if (curr->next == NULL || curr->next->tag & TAG_IS_REDIR)
{
after = curr->next;
curr->next = NULL;
}
curr = curr->next;
}
if ((status = preprocess_filename(&(*redirs)->next, env, &filename)))
{
tok_lst_destroy(redirs, free);
tok_lst_destroy(&after, free);
return (status);
}
if ((status = st_open_replace_dispatch(filename, fds, (*redirs)->tag)) != 0)
{
tok_lst_destroy(redirs, free);
tok_lst_destroy(&after, free);
return (status);
}
tok_lst_destroy(redirs, free);
free(filename);
return (redir_extract(&after, env, fds));
}
|