-
Notifications
You must be signed in to change notification settings - Fork 0
/
s_shlvl.c
57 lines (52 loc) · 1.7 KB
/
s_shlvl.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* s_shlvl.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ebalgruu <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/27 01:29:33 by ebalgruu #+# #+# */
/* Updated: 2021/11/27 14:11:42 by jkassand ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int ft_if_shlvl(t_env *tmp, t_arg *arg)
{
int value;
value = 1;
while (tmp)
{
if (!ft_strcmp(tmp->key, "SHLVL") && !tmp->value[0])
{
free(tmp->sep);
tmp->sep = ft_strdup_pars("=", arg);
free(tmp->value);
tmp->value = ft_strdup_pars("1", arg);
return (1);
}
else if (!ft_strcmp(tmp->key, "SHLVL") && tmp->value[0])
{
value += ft_atoi(tmp->value);
free(tmp->value);
tmp->value = ft_itoa_pars(value, arg);
return (1);
}
tmp = tmp->next;
}
return (0);
}
void ft_shlvl_check(t_arg *arg)
{
t_env *tmp;
tmp = arg->envp;
if (!ft_if_shlvl(tmp, arg))
{
tmp = malloc(sizeof(t_env));
if (!tmp)
ft_exit(12, "malloc", arg);
tmp->key = "SHLVL";
tmp->sep = ft_strdup_pars("=", arg);
tmp->value = ft_strdup_pars("1", arg);
ft_env_lst_add_back(&arg->envp, tmp);
}
}