-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo.c
85 lines (76 loc) · 1.73 KB
/
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* echo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akhalid <akhalid@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/14 22:41:56 by aelkhalo #+# #+# */
/* Updated: 2022/03/03 00:38:41 by akhalid ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "minishell.h"
int is_option(char *args)
{
int i;
i = 0;
if (args && args[0] == '-' && args[1])
{
i++;
while (args[i])
{
if (args[i] != 'n')
return (0);
i++;
}
}
return (i);
}
int get_index(char **args)
{
int i;
i = 1;
if (is_option(args[1]))
{
while (args[i])
{
if (!is_option(args[i]))
return (i);
i++;
}
}
return (i);
}
static void ft_putstr_fd(char *s, int fd)
{
int i;
i = 0;
if (s)
{
while (s[i])
{
write(fd, &s[i], 1);
i++;
}
}
}
void ft_echo(char **args)
{
int i;
int option;
option = is_option(args[1]);
i = get_index(args);
while (args[i])
{
ft_putstr_fd(args[i], 1);
if (args[i + 1] && args[0] != '\0')
write(1, " ", 1);
i++;
}
if (n_args(args) == 1 || option == 0)
write(1, "\n", 1);
g_all.exit_status = 0;
}