-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell_env.c
94 lines (82 loc) · 1.56 KB
/
shell_env.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
87
88
89
90
91
92
93
94
#include "shell.h"
/* rayesyounes - spoutnikrs */
/**
* return_environ - return arr copy of environ
* @info: struct of params
* Return: 0 (Success)
*/
char **return_environ(info_t *info)
{
if (!info->environ || info->env_changed)
{
info->environ = listto_str(info->env);
info->env_changed = 0;
}
return (info->environ);
}
/**
* remove_environ - remove env variable
* @info: struct of params
* @var: string env property
* Return: changed value
*/
int remove_environ(info_t *info, char *var)
{
list_t *node = info->env;
size_t i = 0;
char *p;
if (!node || !var)
return (0);
while (node)
{
p = start_with(node->str, var);
if (p && *p == '=')
{
info->env_changed = del_node_at(&(info->env), i);
i = 0;
node = info->env;
continue;
}
node = node->next;
i++;
}
return (info->env_changed);
}
/**
* setnew_environ - set new env variable
* @info: struct of params
* @var: string env property
* @value: value
* Return: 0 (Success)
*/
int setnew_environ(info_t *info, char *var, char *value)
{
char *buffer = NULL;
list_t *node;
char *p;
if (!var || !value)
return (0);
buffer = malloc(str_length(var) + str_length(value) + 2);
if (!buffer)
return (1);
str_copy(buffer, var);
str_concat(buffer, "=");
str_concat(buffer, value);
node = info->env;
while (node)
{
p = start_with(node->str, var);
if (p && *p == '=')
{
free(node->str);
node->str = buffer;
info->env_changed = 1;
return (0);
}
node = node->next;
}
add_node_end(&(info->env), buffer, 0);
free(buffer);
info->env_changed = 1;
return (0);
}