-
Notifications
You must be signed in to change notification settings - Fork 2
/
redirect.c
192 lines (181 loc) · 3.92 KB
/
redirect.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "shell.h"
/**
* parse_left_redirect - parses redirect directives from input
* @info: parameter struct
* Return: void
*/
void parse_left_redirect(info_t *info)
{
char *p = _strchrlast(info->arg, '<'), *file;
int fd;
if (!p) /* no redirect */
return;
file = p + 1;
if (p > info->arg && *(p - 1) == '<')
{
info->left_append = 1;
p--;
}
if (p > info->arg) /* if not start of string */
{
/* check if previous char is digit */
if (*(p - 1) >= '0' && *(p - 1) <= '9')
{
p--;
info->left_redirect_from_fd = *p - '0';
}
}
fd = open_redirect(info, file, 1);
if (fd == -1)
{
/* TODO: print message, set status, etc? */
return;
}
info->left_redirect_from_fd = fd;
*p = 0; /* insert null char to cut string b4 > */
}
/**
* parse_right_redirect - parses redirect directives from input
* @info: parameter struct
* Return: void
*/
void parse_right_redirect(info_t *info)
{
char *p = _strchrlast(info->arg, '>'), *file;
int fd;
if (!p) /* no redirect */
return;
file = p + 1;
if (p > info->arg && *(p - 1) == '>')
{
info->right_append = 1;
p--;
}
if (p > info->arg) /* if not start of string */
{
/* check if previous char is digit */
if (*(p - 1) >= '0' && *(p - 1) <= '9')
{
p--;
info->right_redirect_from_fd = *p - '0';
}
}
fd = open_redirect(info, file, 0);
if (fd == -1)
{
/* TODO: print message, set status, etc? */
return;
}
info->right_redirect_to_fd = fd;
*p = 0; /* insert null char to cut string b4 > */
}
/**
* open_redirect - opens a fd for redirect writing
* @info: parameter struct
* @file: name of file
* @left: 1 if left, 0 if right
* Return: integer fd or -1
*/
int open_redirect(info_t *info, char *file, int left)
{
int fd;
char buf[256], *action;
while (*file == ' ' || *file == '\t')
file++;
if (!*file)
{
print_error_noarg(info, "Syntax error: newline unexpected\n");
return (-1);
}
if (left)
{
if (info->left_append)
{
info->heredoc = _strdup(file);
if (!info->heredoc)
exit(1);
return (HEREDOC_FD);
}
fd = open(file, O_RDONLY);
}
else
fd = open(file, O_CREAT | O_WRONLY |
(info->right_append ? O_APPEND : O_TRUNC), 0666);
if (fd == -1)
{
buf[0] = 0;
info->line_count++;
info->err_num = info->status = 2;
action = left ? "open" : "create";
if (errno == EACCES)
sprintf(buf, "cannot %s %s: Permission denied\n", action, file);
else if (errno == ENOENT)
sprintf(buf, "cannot %s %s: No such file\n", action, file);
print_error_noarg(info, buf);
}
return (fd);
}
/**
* parse_heredoc - parses line of HEREDOC input into buffer
* @info: the parameter struct
* @buf: the getline buffer
* @r: number of bytes read into getline buffer
* Return: length of heredoc_cmd if end of heredoc else r
*/
size_t parse_heredoc(info_t *info, char **buf, size_t r)
{
static char *heredoc_buf;
static size_t heredoc_i, heredoc_len;
size_t len;
r++;
if (!_strcmp(info->heredoc, *buf))
{
bfree((void **)buf);
*buf = info->heredoc_cmd;
info->heredoc_cmd = NULL;
len = _strlen(*buf);
info->heredoc_txt = heredoc_buf;
heredoc_buf = NULL;
heredoc_i = heredoc_len = 0;
bfree((void **)&info->heredoc);
return (len);
}
while (heredoc_len <= r + heredoc_i)
{
heredoc_buf = _realloc(heredoc_buf, heredoc_len,
heredoc_len ? heredoc_len * 2 : STARTING_ARR_SIZE);
if (!heredoc_buf)
exit(1);
heredoc_len <<= 1;
if (!heredoc_len)
{
_memset(heredoc_buf, 0, STARTING_ARR_SIZE);
heredoc_len = STARTING_ARR_SIZE;
}
}
_strcat(heredoc_buf, *buf);
_strcat(heredoc_buf, "\n");
heredoc_i += r;
return (r);
}
/**
* restore_stdfd - restores stdin/out after redirect
* @info: the parameter struct
*/
void restore_stdfd(info_t *info)
{
_putchar(BUF_FLUSH);
_eputchar(BUF_FLUSH);
if (info->dup_stdin)
{
dup2(info->dup_stdin, STDIN_FILENO);
close(info->dup_stdin);
info->dup_stdin = 0;
}
if (info->dup_stdout)
{
dup2(info->dup_stdout, STDOUT_FILENO);
close(info->dup_stdout);
info->dup_stdout = 0;
}
}