-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathenv_table.c
86 lines (79 loc) · 2.12 KB
/
env_table.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
76
77
78
79
80
81
82
83
84
85
86
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env_table.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmachado <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/14 15:56:13 by bmachado #+# #+# */
/* Updated: 2022/02/23 13:00:10 by bmachado ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int get_position_env(t_shell *table, char *key)
{
int i;
int check_var;
t_vars *current;
i = 0;
check_var = is_var_repeated(table, key);
if (check_var != 0)
return (check_var);
while (i < table->size)
{
current = table->env_vars[i];
if (current && ft_strcmp(current->key, key) == 0)
break ;
if (!current)
break ;
i++;
}
return (i);
}
void hash_insert_env(t_shell *table, char *key, char *value)
{
int i;
t_vars *current_item;
i = get_position_env(table, key);
current_item = table->env_vars[i];
if (current_item)
{
if (ft_strcmp(current_item->key, key) == 0)
{
free(current_item->value);
current_item->value = ft_strdup(value);
return ;
}
}
if (!current_item)
{
current_item = create_vars(key, value);
table->env_vars[i] = current_item;
table->env_count++;
}
}
void envtable(t_shell *table, char **env)
{
int i;
int p;
char *tmpkey;
char *tmpvalue;
i = -1;
table->env_count = 0;
while (++i < ft_arrlen(env))
{
p = 0;
while (env[i][p])
{
if (env[i][p] == '=')
{
tmpkey = ft_substr(env[i], 0, p);
tmpvalue = ft_substr(env[i], p + 1, ft_strlen(env[i]));
hash_insert_env(table, tmpkey, tmpvalue);
free(tmpkey);
free(tmpvalue);
}
p++;
}
}
}