-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_red_in_utils.c
95 lines (87 loc) · 2.74 KB
/
ft_red_in_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_red_in_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ikgonzal <ikgonzal@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/02 21:37:47 by jsolinis #+# #+# */
/* Updated: 2022/04/15 17:41:28 by ikgonzal ### ########.fr */
/* */
/* ************************************************************************** */
#include <fcntl.h>
#include "minishell.h"
void ft_set_red_read(int fd)
{
dup2(fd, 0);
close(fd);
}
char *ft_infile_red(char *arg, int j)
{
int i;
char *ret;
i = 0;
ret = malloc (ft_strlen(arg) - j);
while (arg[++j])
ret[i++] = arg[j];
ret[i] = '\0';
return (ret);
}
void ft_set_red_in(t_proc *proc, int i)
{
proc->infile = 0;
if ((*proc->lst)->infd)
close((*proc->lst)->infd);
(*proc->lst)->infd = open((*proc->lst)->args[i + 1], O_RDONLY);
if ((*proc->lst)->infd < 0)
{
if (access((*proc->lst)->args[i + 1], F_OK) != 0)
proc->infile = print_error(": No such file or directory", 1,
(*proc->lst)->args[i + 1], 0);
else
printf("Error de acceso");
(*proc->lst)->infd = 0;
}
}
void ft_set_red_in_del_fd(t_proc *proc)
{
if ((*proc->lst)->infd)
close((*proc->lst)->infd);
(*proc->lst)->infd = open("test", O_RDONLY);
if ((*proc->lst)->infd < 0)
{
if (access("test", F_OK) != 0)
proc->infile = print_error(": No such file or directory",
1, "test", 0);
else
printf("Error de acceso");
(*proc->lst)->infd = 0;
}
unlink("test");
free ((*proc->lst)->heredoc_line);
}
void ft_set_red_in_del(t_proc *proc, int i)
{
int index;
index = 2;
if ((*proc->lst)->args[i][0] == 60 && (*proc->lst)->args[i][1] == 60)
index = 1;
if ((*proc->lst)->infd)
close((*proc->lst)->infd);
(*proc->lst)->infd = open("test", O_WRONLY | O_TRUNC | O_CREAT, 0777);
while (ft_strncmp_len((*proc->lst)->heredoc_line,
(*proc->lst)->args[i + index],
ft_strlen((*proc->lst)->args[i + index])) != 0)
{
(*proc->lst)->heredoc_line = readline("> ");
if (ft_strncmp_len((*proc->lst)->heredoc_line,
(*proc->lst)->args[i + index],
ft_strlen((*proc->lst)->args[i + index])) != 0)
{
write((*proc->lst)->infd, (*proc->lst)->heredoc_line,
ft_strlen((*proc->lst)->heredoc_line));
write((*proc->lst)->infd, "\n", 1);
}
}
ft_set_red_in_del_fd(proc);
}