forked from 42-shell/minishell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin_echo.c
65 lines (60 loc) · 1.74 KB
/
builtin_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin_echo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jkong <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/23 23:11:25 by jkong #+# #+# */
/* Updated: 2022/06/25 12:04:28 by jkong ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtins.h"
#include "safe_io.h"
#include "string_buffer.h"
#include "string_vector.h"
static char **_process_option(char **argv, int *display_return)
{
char *str;
argv++;
*display_return = 1;
while (*argv)
{
str = *argv;
if (*str++ == '-' && *str != '\0')
{
while (*str == 'n')
str++;
if (*str != '\0')
break ;
*display_return = 0;
}
else
break ;
argv++;
}
return (argv);
}
t_builtin_res ft_echo(t_builtin_argv argv, t_builtin_envp envp)
{
int display_return;
char **value;
t_str_buf *buf;
char *str;
(void)envp;
value = _process_option(argv, &display_return);
buf = NULL;
while (*value)
{
buf = str_append(buf, *value++);
if (*value)
buf = str_append(buf, " ");
}
if (display_return)
buf = str_append(buf, "\n");
str = str_dispose(buf);
if (str)
putstr_safe(str);
free(str);
return (EXIT_SUCCESS);
}