Repository navigation
Expand file tree
/
Copy pathredirections2.c
More file actions
99 lines (92 loc) · 3.37 KB
/
Copy pathredirections2.c
File metadata and controls
99 lines (92 loc) · 3.37 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* redirections2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: iarbaiza <iarbaiza@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/15 11:47:02 by auskola- #+# #+# */
/* Updated: 2024/02/02 17:03:28 by iarbaiza ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int check_redirect( char **argv, int i)
{
if (strncmp(argv[i], "<<", 2) == 0)
return (HERE_DOCUMENT);
else if (strncmp(argv[i], "<", 1) == 0)
return (DATA_INPUT);
else if (strncmp(argv[i], ">", 1) == 0)
return (WRITE);
else if (strncmp(argv[i], ">>", 2) == 0)
return (APPEND);
else
return (0);
}
void close_redirection(t_mini_data *data)
{
if (data->file_id >= 0)
{
close(data->file_id);
data->file_id = -1;
}
}
/* if (data->pipe[line_number].tokens[i].token[0] == '"')
return (ft_strdup_trim(data->pipe[line_number].tokens[i].token));
printf("tokena: %s\n", data->pipe[line_number].tokens[i].token);
if (strncmp(data->pipe[line_number].tokens[i].token, "\'", 1) == 0)
return (printf("casi\n"), \
ft_strdup_trim(data->pipe[line_number].tokens[i].token));
else if (strncmp(data->pipe[line_number].tokens[i].token, "\"", 1) == 0)
return ("\"");
else if (strncmp(data->pipe[line_number].tokens[i].token, "\'" , 2) == 0)
return (ft_strdup_trim(data->pipe[line_number].tokens[i].token));
else
*/
// if (strncmp(data->pipe[line_number].tokens[i].token, "\'", 1) == 0)
// return (ft_strdup_trim(data->pipe[line_number].tokens[i].token));
char *clean_text(t_mini_data *data, int line_number, int i)
{
if (data->pipe[line_number].tokens[i].token[0] == '"')
return (ft_strdup_trim(data->pipe[line_number].tokens[i].token));
if (strncmp(data->pipe[line_number].tokens[i].token, "\"", 1) == 0)
return ("\"");
else if (strncmp(data->pipe[line_number].tokens[i].token, "\'", 2) == 0)
return (ft_strdup_trim(data->pipe[line_number].tokens[i].token));
else
return (ft_strdup(data->pipe[line_number].tokens[i].token));
}
void update_redirections(t_mini_data *data, int pipe, \
int i, int data_type)
{
data->pipe[pipe].num_redirections++;
data->pipe[pipe].tokens[i].redirection = data_type;
data->pipe[pipe].tokens[i + 1].redirection = FILE_NAME;
}
void open_redirection(t_mini_data *data, int redirection)
{
if (redirection == WRITE)
data->out = open(data->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (redirection == APPEND)
data->out = open(data->file_name, O_WRONLY | O_CREAT | O_APPEND, 0644);
if (redirection == WRITE || redirection == APPEND)
{
if (data->out < 0)
{
perror("Error open file ");
exit(EXIT_FAILURE);
}
dup2(data->out, STDOUT_FILENO);
close(data->out);
}
if (redirection == DATA_INPUT)
{
if (data->in < 0)
{
perror("Error opening file Heredoc");
exit(EXIT_FAILURE);
}
dup2(data->in, STDIN_FILENO);
close(data->in);
}
}