-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_env.c
More file actions
63 lines (59 loc) · 1.86 KB
/
Copy pathinit_env.c
File metadata and controls
63 lines (59 loc) · 1.86 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init_env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abiru <abiru@student.42abudhabi.ae> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/02 09:08:17 by abiru #+# #+# */
/* Updated: 2023/04/25 12:22:16 by abiru ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int check_shl_val(t_list *envp[2], t_dict *dict, unsigned long long n)
{
if (get_val(&(envp)[0], "SHLVL"))
{
n = custom_atoi(get_val(&(envp)[0], "SHLVL"));
if (n >= 9999 && n <= 2147483646)
{
n++;
ft_putstr_fd("Yash: warning: shell level (", 2);
ft_putnbr_fd(n, 2);
ft_putendl_fd(") too high, resetting to 1", 2);
n = 0;
}
else if (n > 2147483646)
n = -1;
n++;
}
else
n = 1;
dict->value = ft_itoa(n);
update_env(dict, &(envp)[0]);
update_env(dict, &(envp)[1]);
free_dict(dict, 0);
return (0);
}
int update_shell(t_list *envp[2])
{
unsigned long long n;
t_dict *dict;
dict = (t_dict *)malloc(sizeof(t_dict));
if (!dict)
{
perror("malloc");
return (1);
}
dict->key = ft_strdup("SHLVL");
dict->flag = 1;
n = 0;
if (key_exists("SHLVL", &(envp)[0]))
return (check_shl_val(envp, dict, n));
n = 1;
dict->value = ft_itoa(n);
update_env(dict, &(envp)[0]);
update_env(dict, &(envp)[1]);
free_dict(dict, 0);
return (0);
}