-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_redir.c
144 lines (134 loc) · 3.58 KB
/
parse_redir.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_redir.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aoizel <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 11:05:18 by aoizel #+# #+# */
/* Updated: 2024/01/23 11:31:12 by aoizel ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int open_redir_files(t_wordlst *wordlst, t_processlst *process)
{
int open_flag;
if ((wordlst->type == REDIR_OUT || wordlst->type == REDIR_APPEND))
{
if (process->fd_out > 1)
close(process->fd_out);
if (wordlst->type == REDIR_OUT)
open_flag = O_CREAT | O_WRONLY | O_TRUNC;
if (wordlst->type == REDIR_APPEND)
open_flag = O_CREAT | O_WRONLY | O_APPEND;
process->fd_out = open(wordlst->next->word, open_flag, 0644);
if (process->fd_out < 0)
return (perror(wordlst->next->word), 0);
}
if (wordlst->type == REDIR_IN)
{
if (process->fd_in > 1)
close(process->fd_in);
process->fd_in = open(wordlst->next->word, O_RDONLY);
if (process->fd_in < 0)
return (perror(wordlst->next->word), 0);
}
return (0);
}
char *tmp_heredoc_name(void)
{
static int nb;
int basepower;
int tmp_nb;
int i;
static char name[26];
i = 0;
nb++;
tmp_nb = nb;
basepower = 1000000000;
ft_bzero(name, 26);
while (i < 15)
{
name[i] = "/tmp/ms_heredoc"[i];
i++;
}
while (basepower)
{
name[i] = '0' + tmp_nb / basepower;
tmp_nb %= basepower;
basepower /= 10;
i++;
}
return (name);
}
int write_heredoc(t_processlst *process, char *delimiter)
{
char *line;
line = NULL;
if (heredoc_signal_handler() == -1)
return (-1);
while (!line || ft_strcmp(line, delimiter))
{
if (line)
ft_putendl_fd(line, process->fd_in);
free(line);
line = readline(">");
if (g_last_sig == SIGINT)
{
g_last_sig = 0;
process->status = 130;
return (free(line), close(process->fd_in), -1);
}
if (!line)
{
ft_putendl_fd("Warning : Heredoc ended by EOF", 2);
break ;
}
}
free(line);
return (0);
}
int open_heredoc(t_wordlst *wordlst, t_processlst *process)
{
char *heredoc_name;
heredoc_name = tmp_heredoc_name();
while (access(heredoc_name, F_OK) == 0)
heredoc_name = tmp_heredoc_name();
if (!heredoc_name)
return (-1);
if (process->fd_in != 0)
close(process->fd_in);
process->fd_in = open(heredoc_name, O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (process->fd_in == -1)
return (-1);
if (write_heredoc(process, wordlst->next->word) == -1)
return (-1);
close(process->fd_in);
process->fd_in = open(heredoc_name, O_RDONLY, 0644);
if (process->fd_in == -1)
return (-1);
unlink(heredoc_name);
return (0);
}
int get_redir_files(t_wordlst *wordlst, t_processlst *process)
{
while (wordlst && wordlst->type != PIPE)
{
if (wordlst->type == REDIR_OUT || wordlst->type == REDIR_IN
|| wordlst->type == REDIR_APPEND)
{
if (open_redir_files(wordlst, process) == -1)
{
process->status = 1;
return (-1);
}
}
if (wordlst->type == REDIR_HEREDOC)
{
if (open_heredoc(wordlst, process) == -1)
return (-1);
}
wordlst = wordlst->next;
}
return (0);
}