-
Notifications
You must be signed in to change notification settings - Fork 0
/
redirection.c
149 lines (136 loc) · 3.77 KB
/
redirection.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
145
146
147
148
149
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* redirection.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mleclair <mleclair@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/13 12:54:31 by aridolfi #+# #+# */
/* Updated: 2017/05/19 17:48:03 by mleclair ### ########.fr */
/* */
/* ************************************************************************** */
#include "chell.h"
/*
** Redirecting Output: command [n]> output.txt
** command [n]>| output.txt
*/
int verredir(t_env *env)
{
int i;
i = 0;
while (env->input[i])
{
if (env->input[i] == '>')
if (env->input[i + 1] && env->input[i + 1] == '>')
if (env->input[i + 2] && env->input[i + 2] == '>')
{
error(-15, NULL, NULL);
return (-1);
}
i++;
}
return (0);
}
void rd_output(t_env *env, int fd, int n, pid_t child)
{
char **s;
int status;
if (ft_isdigit(*(env->redir)))
n = *(env->redir) - 48;
if ((s = ft_strsplitquote(env->redir + (n == -1 ? 1 : 2), ' ', 1)) && !s[0])
return (free(s));
free_swap(&env->redir, ft_strdup(s[0]));
free_double_array(s);
if (env->redir[0] == '&')
return (rd_dupoutput(env, n));
if ((fd = open(env->redir, O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1)
error(-17, NULL, NULL);
child = fork();
if ((int)child == -1)
error(-16, NULL, NULL);
else if ((int)child == 0)
{
dup2(fd, (n == -1 ? STDOUT_FILENO : (int)n));
parse(env, &env->input, 1);
exit(env->lastret);
}
close(fd);
wait(&status);
retvalue_into_loc(env, WEXITSTATUS(status));
}
/*
** Appending Redirected Output: command [n]>> output.txt
*/
void rd_output_apd(t_env *env, int fd, pid_t child)
{
char n;
char **s;
int status;
n = -1;
if (ft_isdigit(*(env->redir)))
n = *(env->redir) - 48;
s = ft_strsplitquote(env->redir + (n == -1 ? 2 : 3), ' ', 1);
free_swap(&env->redir, ft_strdup(s[0]));
free_double_array(s);
if ((fd = open(env->redir, O_WRONLY | O_CREAT | O_APPEND, 0644)) == -1)
error(-17, NULL, NULL);
child = fork();
if ((int)child == -1)
error(-16, NULL, NULL);
else if ((int)child == 0)
{
dup2(fd, (n == -1 ? STDOUT_FILENO : (int)n));
parse(env, &env->input, 1);
exit(env->lastret);
}
close(fd);
wait(&status);
retvalue_into_loc(env, WEXITSTATUS(status));
}
/*
** Redirecting Input: command [n]< output.txt
*/
int rd_input2(t_env *env, char *n, int *fd)
{
if (ft_isdigit(env->inp1[ft_strlen(env->inp1) > 0
? ft_strlen(env->inp1) - 1 : 0]))
if ((*n = 0) && ft_strlen(env->inp1) > 1)
*n = (env->inp1[ft_strlen(env->inp1) - 2] == '\\' ? -1
: env->inp1[ft_strlen(env->inp1) - 1] - 48);
if (*n != -1)
env->inp1[ft_strlen(env->inp1) - 1] = '\0';
if ((*fd = open(env->inp2, O_RDONLY)) == -1)
{
error(-17, NULL, NULL);
close(*fd);
return (-1);
}
return (0);
}
void rd_input(t_env *env, int fd)
{
pid_t child;
char n;
int status;
char **s;
child = -1;
n = -1;
if ((s = ft_strsplitquote(env->inp2, ' ', 1)) && !s[0])
return (free(s));
free_swap(&env->inp2, ft_strdup(s[0]));
free_double_array(s);
if (rd_input2(env, &n, &fd) == -1)
return ;
child = fork();
if ((int)child == -1)
error(-16, NULL, NULL);
else if ((int)child == 0)
{
dup2(fd, (n == -1 ? STDIN_FILENO : (int)n));
parse(env, &env->inp1, 1);
exit(env->lastret);
}
close(fd);
wait(&status);
retvalue_into_loc(env, WEXITSTATUS(status));
}