-
Notifications
You must be signed in to change notification settings - Fork 0
/
redirections_utils.c
103 lines (95 loc) · 2.39 KB
/
redirections_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
96
97
98
99
100
101
102
103
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* redirections_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hchorfi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/06 14:34:17 by hchorfi #+# #+# */
/* Updated: 2021/05/22 11:01:37 by hchorfi ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int ft_check_in2(char *tmp_in)
{
int j;
int cot;
j = 0;
if (tmp_in[j] == '\"' || tmp_in[j] == '\'')
{
cot = j++;
while (tmp_in[j] != tmp_in[cot] && tmp_in[j] != '\0')
j++;
}
else
{
while (tmp_in[j] != ' ' && tmp_in[j] != '\0')
j++;
}
return (j);
}
int ft_check_out(char **tmp_out)
{
int j;
int cot;
j = 0;
if ((*tmp_out)[0] == '>')
{
(*tmp_out)++;
while ((*tmp_out)[j] == ' ' && (*tmp_out)[j] != '\0')
j++;
g_data.append = 1;
}
if (((*tmp_out)[j] == '\"' || (*tmp_out)[j] == '\''))
{
cot = j++;
while ((*tmp_out)[j] != (*tmp_out)[cot] && (*tmp_out)[j] != '\0')
{
j++;
}
}
else
{
while ((*tmp_out)[j] != ' ' && (*tmp_out)[j] != '\0')
j++;
}
return (j);
}
void ft_close_get_out(int out)
{
if (g_data.command->output_file > 1)
close(g_data.command->output_file);
g_data.command->output_file = out;
}
void ft_out_red_file2(char *file, char c, int out, int priority)
{
struct stat path_stat;
char *tmp_free;
if (c != '\'')
{
tmp_free = file;
file = get_other_variables(file);
if (*file == '\0')
{
ft_putstrs_er("minishell: ", tmp_free, ": ambiguous redirect\n", NULL);
g_data.ret = 1;
}
free(tmp_free);
}
tmp_free = file;
file = remove_all_quotes(file);
free(tmp_free);
if (g_data.append == 0)
{
priority = 1;
out = open(file, O_RDWR | O_CREAT | O_TRUNC, 0777);
free(file);
}
else
{
if (priority == 0 || stat(file, &path_stat))
out = open(file, O_RDWR | O_CREAT | O_APPEND, 0777);
free(file);
}
ft_close_get_out(out);
}