-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre_open.c
71 lines (64 loc) · 1.87 KB
/
pre_open.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pre_open.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jkassand <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/27 01:30:08 by jkassand #+# #+# */
/* Updated: 2021/11/27 01:30:10 by jkassand ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int err_open(int errnum, char *text, t_arg *arg)
{
char *errmsg;
errmsg = strerror(errnum);
write(2, text, ft_strlen(text));
write(2, ": ", 2);
write(2, errmsg, ft_strlen(errmsg));
write(2, "\n", 1);
arg->errnum = errnum;
return (1);
}
static int open_file(t_red *tmp, t_arg *arg)
{
int fd;
if (!tmp->in && tmp->two)
{
fd = open(tmp->name, O_WRONLY | O_CREAT | O_APPEND, 0644);
if (fd == -1)
return (err_open(errno, tmp->name, arg));
}
else if (!tmp->in && !tmp->two)
{
fd = open(tmp->name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1)
return (err_open(errno, tmp->name, arg));
}
else
{
fd = open(tmp->name, O_RDONLY);
if (fd == -1)
return (err_open(errno, tmp->name, arg));
}
close(fd);
return (0);
}
int pre_open(t_arg *arg)
{
t_red *tmp;
tmp = arg->red;
while (tmp)
{
if (!tmp->in || !tmp->two)
{
if (open_file(tmp, arg))
return (1);
}
else
heredoc(tmp->name, tmp->limiter, arg);
tmp = tmp->next;
}
return (0);
}