-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_echo.c
More file actions
73 lines (66 loc) · 1.95 KB
/
ft_echo.c
File metadata and controls
73 lines (66 loc) · 1.95 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_echo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mamoussa <mamoussa@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/10 12:03:41 by mamoussa #+# #+# */
/* Updated: 2020/12/10 12:24:08 by mamoussa ### ########.fr */
/* */
/* ************************************************************************** */
#include "shell.h"
size_t check_for_n(void)
{
t_cmd *current;
current = g_cmd_head;
while (current && (current->type != semicolumn) &&
(current->type != pipee))
{
if (!ft_strncmp(g_cmd_head->string, "-n", ft_strlen("-n")))
return (1);
current = current->next;
}
return (0);
}
void input_out_red(void)
{
if (g_is_out)
{
dup2(g_fd_out, 1);
close(g_fd_out);
}
}
void ft_echo_helper(void)
{
size_t is_n;
g_cmd_head = g_cmd_head->next;
input_out_red();
imp_pipes();
if (!g_cmd_head || (g_cmd_head->type == semicolumn) ||
(g_cmd_head->type == pipee))
{
write(1, "\n", 1);
exit(0);
}
if ((is_n = check_for_n()))
g_cmd_head = g_cmd_head->next;
while (g_cmd_head && (g_cmd_head->type != semicolumn) &&
(g_cmd_head->type != pipee))
{
write(1, g_cmd_head->string, ft_strlen(g_cmd_head->string));
g_cmd_head = g_cmd_head->next;
if (g_cmd_head && (g_cmd_head->type != semicolumn) &&
(g_cmd_head->type != pipee))
write(1, " ", 1);
}
if (!is_n)
write(1, "\n", 1);
exit(0);
}
void ft_echo(void)
{
if (echo_error_checker())
return ;
ft_echo_helper();
}