-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho_env.c
More file actions
97 lines (88 loc) · 2.37 KB
/
echo_env.c
File metadata and controls
97 lines (88 loc) · 2.37 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* echo_env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adurusoy <adurusoy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/20 22:13:16 by adurusoy #+# #+# */
/* Updated: 2023/12/28 15:11:35 by adurusoy ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include <unistd.h>
void run_echo(t_parse *data, int *i)
{
int x;
int j;
x = *i;
while (data->text[x])
{
j = 0;
while (data->text[x][j])
write(data->outfile, &data->text[x][j++], 1);
if (data->text[x][j] == '\0' && data->text[x + 1] != NULL)
write(data->outfile, " ", 1);
x++;
*i += 1;
}
}
static int all_n_chars(const char *str)
{
while (*str != '\0')
{
if (*str != 'n')
return (0);
str++;
}
return (1);
}
void exec_echo(t_parse *data, t_shell *m_shell)
{
int i;
i = 0;
if (!data->text || !data->text[0])
write(data->outfile, "\n", 1);
else
{
if (data->text[0] && data->text[0][0] == '-' && data->text[0][1] == 'n'
&& all_n_chars(data->text[0] + 2))
{
while (data->text[i] && data->text[i][0] == '-' && \
data->text[i][1] == 'n' && all_n_chars(data->text[i] + 2))
i++;
if (data->text[i])
run_echo(data, &i);
}
else
{
run_echo(data, &i);
if (!data->text[i])
write(data->outfile, "\n", 1);
}
}
m_shell->exec_status = 0;
}
void print_list(void *data, t_shell *m_shell)
{
t_parse *str;
t_env *new;
int i;
i = 0;
str = m_shell->parse;
new = (t_env *)data;
if (!new->value)
return ;
while (new->key[i])
write(str->outfile, &new->key[i++], 1);
write(str->outfile, "=", 1);
i = 0;
while (new->value && new->value[i])
write(str->outfile, &new->value[i++], 1);
write(str->outfile, "\n", 1);
}
void exec_env(t_shell *m_shell)
{
ft_newlstiter(m_shell->env, print_list, m_shell);
m_shell->exec_status = 0;
}