-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuiltin_echo.c
More file actions
61 lines (55 loc) · 1.71 KB
/
builtin_echo.c
File metadata and controls
61 lines (55 loc) · 1.71 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin_echo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: phelebra <xhelp00@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/02 14:01:36 by phelebra #+# #+# */
/* Updated: 2023/08/02 14:16:27 by phelebra ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void handle_outfile_redirection(t_parsed *parsed_cmd)
{
int outfile_fd;
outfile_fd = parsed_cmd->outfile;
if (outfile_fd != STDOUT_FILENO)
{
dup2(outfile_fd, STDOUT_FILENO);
close(outfile_fd);
}
}
void print_arguments(char **args, int index, bool n_opt)
{
while (args[index] != NULL)
{
printf("%s", args[index]);
if (args[index + 1] != NULL)
printf(" ");
index++;
}
if (!n_opt)
printf("\n");
}
int builtin_echo(t_parsed *parsed_cmd)
{
bool n_opt;
int i;
char **args;
int save_stdout;
n_opt = false;
i = 1;
args = parsed_cmd->args;
save_stdout = dup(STDOUT_FILENO);
handle_outfile_redirection(parsed_cmd);
if (args[i] != NULL && strcmp(args[i], "-n") == 0)
{
n_opt = true;
i++;
}
print_arguments(args, i, n_opt);
dup2(save_stdout, STDOUT_FILENO);
close(save_stdout);
return (1);
}