-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetenv.c
88 lines (81 loc) · 1.59 KB
/
getenv.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
#include "shell.h"
/**
* **get_environ - function that returns array of string copy
* @info: pointer to struct
* Return: 0
*/
char **get_environ(info_t *info)
{
if (!info->environ || info->env_changed)
{
info->environ = list_to_strings(info->env);
info->env_changed = 0;
}
return (info->environ);
}
/**
* _unsetenv - function that remove an env variable
* @info: pointer to struct
* Return: 1 on delete, else 0
* @var: pointer to string env var
*/
int _unsetenv(info_t *info, char *var)
{
list_t *node = info->env;
size_t a = 0;
char *p;
if (node == NULL || var == NULL)
return (0);
while (node)
{
p = starts_with(node->str, var);
if (p && *p == '=')
{
info->env_changed = delete_node_at_index(&(info->env), a);
a = 0;
node = info->env;
continue;
}
node = node->next;
a++;
}
return (info->env_changed);
}
/**
* _setenv - function that initialize a new environment variable
* @info: pointer to struct
* @var: pointer to string
* @value: pointer to string value
* Return: Always 0
*/
int _setenv(info_t *info, char *var, char *value)
{
char *buff = NULL;
list_t *node;
char *p;
if (var == NULL || value == NULL)
return (0);
buff = malloc(_strlen(var) + _strlen(value) + 2);
if (!buff)
return (1);
_strcpy(buff, var);
_strcat(buff, "=");
_strcat(buff, value);
node = info->env;
while (node)
{
p = starts_with(node->str, var);
if (p && *p == '=')
{
free(node->str);
node->str = buff;
info->env_changed = 1;
return (0);
}
node = node->next;
}
add_node_end(&(info->env), buff, 0);
free(buff);
info->env_changed = 1;
return (0);
}