-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipex_redirect_utils.c
129 lines (120 loc) · 3.44 KB
/
pipex_redirect_utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex_redirect_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bsyvasal <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/03 23:28:16 by bsyvasal #+# #+# */
/* Updated: 2024/03/01 11:03:55 by bsyvasal ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void free_filenames(char *infilename, char *outfilename)
{
if (infilename)
free(infilename);
if (outfilename)
free(outfilename);
}
void redirect_error(char *errmsg, int *fd, t_pipe *data)
{
data->status = 1;
ft_putstr_fd("bvsh: ", 2);
if (fd[0] == -3 || fd[1] == -3)
{
ft_putstr_fd("syntax error near unexpected token `", 2);
data->status = 258;
}
ft_putstr_fd(errmsg, 2);
free(errmsg);
if (fd[1] == -2 || fd[2] == -2)
{
ft_putstr_fd(": malloc error\n", 2);
clean_exit(data, NULL, 1);
}
else if ((fd[1] != -3 && fd[0] != -3) && errno)
{
ft_putstr_fd(": ", 2);
ft_putstr_fd(strerror(errno), 2);
}
ft_putstr_fd("\n", 2);
}
static int buffer_interpret_pipe(char *buffer, int *heredoc_fd, t_pipe *data)
{
char *interpreted_str;
while (*buffer)
{
interpreted_str = interpret(buffer, data);
if (!interpreted_str)
{
close(heredoc_fd[1]);
close(heredoc_fd[0]);
return (-2);
}
write(heredoc_fd[1], interpreted_str, ft_strlen(interpreted_str));
free(interpreted_str);
buffer += len_next_meta_char(buffer, "", 1);
while (ft_isspace(*buffer))
write(heredoc_fd[1], buffer++, 1);
}
write(heredoc_fd[1], "\n", 1);
return (0);
}
static int here_doc(char *delimiter, t_pipe *data)
{
int heredoc_fd[2];
char *buffer;
if (!delimiter || ft_strlen(delimiter) == 0)
return (-3);
if (pipe(heredoc_fd) < 0)
return (-1);
while (1)
{
buffer = readline("> ");
if (!buffer)
break ;
if (ft_strncmp(buffer, delimiter, ft_strlen(delimiter) + 1) == 0)
break ;
if (buffer_interpret_pipe(buffer, heredoc_fd, data) == -2)
return (free_return (buffer, -2));
free(buffer);
}
free(buffer);
close(heredoc_fd[1]);
return (heredoc_fd[0]);
}
/**
* Checks permission of the file and stop redirect if there is no permission
* Creates file for each filename in redirect
*
* @return -1 if file exist but has no read permission, 0 if file exist and
* and can be read or was created if it did not exist before.
*/
int openfile(char *filename, char symbol, int type, t_pipe *data)
{
int fd;
if (!filename)
return (-2);
if (!*filename)
return (-3);
fd = 0;
if (symbol == '<' && type == 1)
{
if (access(filename, R_OK) == -1)
return (-1);
fd = open(filename, O_RDONLY);
}
else if (symbol == '<' && type == 2)
fd = here_doc(filename, data);
else if (symbol == '>')
{
if (access(filename, F_OK) == 0 && access(filename, W_OK) == -1)
return (-1);
if (type == 1)
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (type == 2)
fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, 0644);
}
return (fd);
}