-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute_utils_3.c
75 lines (68 loc) · 1.89 KB
/
execute_utils_3.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* execute_utils_3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abuzdin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/08 15:53:41 by abuzdin #+# #+# */
/* Updated: 2022/08/11 10:07:32 by abuzdin ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int hd_estatus(int *i, int out)
{
char *tmp;
if (g_status > 255)
g_status = (g_status >> 8) & 0xff;
tmp = ft_itoa(g_status);
write(out, tmp, ft_strlen(tmp));
free(tmp);
++(*i);
return (0);
}
static int hd_expansion(t_input *data, char *str, int out, int *i)
{
int start;
t_env *tmp;
start = *i;
if (str[start] == '?')
return (hd_estatus(i, out));
while (str[*i] && ft_isalnum(str[*i]))
++(*i);
if (start == *i)
return (1);
data->tmp = ms_strndup(str + start, *i - start, data);
tmp = data->envp_n;
while (tmp)
{
if (tmp->value && !ft_strcmp(tmp->type, data->tmp))
{
write(out, tmp->value, ft_strlen(tmp->value));
break ;
}
tmp = tmp->next;
}
free(data->tmp);
return (0);
}
void hd_write(t_input *data, char *str, int out)
{
int i;
i = 0;
while (str[i])
{
if (str[i] == '$')
{
++i;
if (hd_expansion(data, str, out, &i))
write(out, "$", 1);
}
if (str[i] && str[i] != '$')
{
write(out, &str[i], 1);
++i;
}
}
write(out, "\n", 1);
}