forked from xhelp00/minishell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.c
135 lines (123 loc) · 3.33 KB
/
get.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* execute.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: phelebra <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/10 13:58:52 by fvonsovs #+# #+# */
/* Updated: 2023/08/01 16:09:21 by phelebra ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
extern int g_status;
char *get_here_str(char *str[2], size_t len, char *limit, char *warn)
{
char *temp;
while (g_status != 130 && (!str[0] || ft_strncmp(str[0], limit, len) \
|| (size_t)ft_strlen(limit) != len))
{
temp = str[1];
str[1] = ft_strjoin(str[1], str[0]);
free(temp);
free(str[0]);
str[0] = readline("HERE_DOC > ");
if (!str[0])
{
printf("%s (wanted `%s\')\n", warn, limit);
break ;
}
temp = str[0];
str[0] = ft_strjoin(str[0], "\n");
free(temp);
len = ft_strlen(str[0]) - 1;
}
free(str[0]);
return (str[1]);
}
int get_here_doc(char *str[2], char *aux[2])
{
int fd[2];
g_status = 0;
if (pipe(fd) == -1)
you_fucked_up("pipe error", 1);
str[1] = get_here_str(str, 0, aux[0], aux[1]);
write(fd[WRITE_END], str[1], ft_strlen(str[1]));
free(str[1]);
close(fd[WRITE_END]);
if (g_status == 130)
{
close(fd[READ_END]);
return (-1);
}
return (fd[READ_END]);
}
int get_fd(int oldfd, char *path, int flags[2])
{
int fd;
if (oldfd > 2)
close(oldfd);
if (!path)
return (-1);
if (access(path, F_OK) == -1 && !flags[0])
you_fucked_up("No such file or directory", 127);
else if (!flags[0] && access(path, R_OK) == -1)
you_fucked_up("Permission denied", 126);
else if (flags[0] && access(path, W_OK) == -1 && access(path, F_OK) == 0)
you_fucked_up("Permission denied", 126);
if (flags[0] && flags[1])
fd = open(path, O_CREAT | O_WRONLY | O_APPEND, 0666);
else if (flags[0] && !flags[1])
fd = open(path, O_CREAT | O_WRONLY | O_TRUNC, 0666);
else if (!flags[0] && oldfd != -1)
fd = open(path, O_RDONLY);
else
fd = oldfd;
return (fd);
}
t_parsed *get_outfile1(t_parsed *node, char **args, int *i)
{
char *nl;
int flags[2];
flags[0] = 1;
flags[1] = 0;
nl = "syntax error near unexpected token `newline'";
(*i)++;
if (args[*i])
node->outfile = get_fd(node->outfile, args[*i], flags);
if (!args[*i] || node->outfile == -1)
{
*i = -1;
if (node->outfile != -1)
{
ft_putendl_fd(nl, 2);
g_status = 2;
}
else
g_status = 1;
}
return (node);
}
t_parsed *get_outfile2(t_parsed *node, char **args, int *i)
{
char *nl;
int flags[2];
flags[0] = 1;
flags[1] = 1;
nl = "minishell: syntax error near unexpected token `newline'";
(*i)++;
if (args[++(*i)])
node->outfile = get_fd(node->outfile, args[*i], flags);
if (!args[*i] || node->outfile == -1)
{
*i = -1;
if (node->outfile != -1)
{
ft_putendl_fd(nl, 2);
g_status = 2;
}
else
g_status = 1;
}
return (node);
}