-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_echo.c
113 lines (105 loc) · 2.53 KB
/
ft_echo.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_echo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wilhelmfermey <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/23 14:43:05 by wilhelmfermey #+# #+# */
/* Updated: 2022/05/23 14:43:28 by wilhelmfermey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void ft_echo_all_empty(t_seg **seg)
{
t_seg *tmp;
tmp = *seg;
if (tmp->flags)
{
if (tmp->flags[1] == 'n' && tmp->flags[2] == '\0')
return ;
else
printf("%s", tmp->flags + 1);
printf("\n");
}
else
printf("%s", "\n");
}
void ft_echo_no_redir(t_seg **seg)
{
int i;
t_seg *tmp;
i = 0;
tmp = *seg;
if (tmp->flags && tmp->flags[1] == 'n' && tmp->flags[2] == '\0'
&& tmp->tokens[1][0] == '-')
{
while (tmp->args[i])
{
ft_printf("%s", tmp->args[i++]);
if (tmp->args[i])
ft_printf(" ");
}
}
else
{
if (!tmp->flags)
{
while (tmp->args[i])
{
printf("%s", tmp->args[i++]);
if (tmp->args[i])
printf(" ");
}
printf("\n");
}
else
{
i = 1;
while (tmp->tokens[i])
{
if(tmp->tokens[i][0] != '>' && tmp->tokens[i][0] != '<'
&& !ft_is_not_inout(tmp->tokens[i], tmp))
{
if (i > 0)
printf(" ");
printf("%s", tmp->tokens[i++]);
}
else
i++;
}
printf("\n");
}
}
}
void ft_echo(t_seg **seg)
{
t_seg *tmp;
int i;
int new_file;
int saved_out;
tmp = *seg;
i = -1;
update_exit_code("0");
if (tmp->input[0] == NULL && tmp->output[0] == NULL
&& tmp->args[0] == NULL)
ft_echo_all_empty(seg);
else if (tmp->output[0] == NULL)
ft_echo_no_redir(seg);
else if (tmp->output[0])
{
while (tmp->output[++i])
{
saved_out = dup(1);
if (tmp->output[i][0] == '1')
new_file = open(tmp->output[i] + 1, O_RDWR | O_CREAT | O_TRUNC, 0644);
else
new_file = open(tmp->output[i] + 1, O_RDWR | O_APPEND| O_CREAT, 0644);
dup2(new_file, 1);
ft_echo_no_redir(seg);
close(new_file);
dup2(saved_out, 1);
close(saved_out);
}
}
}