-
Notifications
You must be signed in to change notification settings - Fork 3
/
get_environ.c
93 lines (85 loc) · 1.86 KB
/
get_environ.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
#include "shell.h"
/**
* get_environ - function that returns the string array copy of the environ
* @info: Structure tht has potential arguments. maintain
* constant function prototype.
* Return: Always 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 environment variables
* @info: Structure that has arguments. maintain
* constant function prototype.
* Return: 1 for delete, 0 otherwise
* @vari: the string env var property
*/
int _unsetenv(info_t *info, char *vari)
{
list_t *node = info->env;
size_t j = 0;
char *q;
if (!node || !vari)
return (0);
while (node)
{
q = starts_with(node->str, vari);
if (q && *q == '=')
{
info->env_changed = delete_node_at_index(&(info->env), j);
j = 0;
node = info->env;
continue;
}
node = node->next;
j++;
}
return (info->env_changed);
}
/**
* _setenv - function tht Initialize new environ var,
* or modify existing ones
* @info: Structure that has arguments. maintain
* constant function prototype.
* @var: string environent variable property
* @value: string environment variable value
* Return: Always 0
*/
int _setenv(info_t *info, char *var, char *value)
{
char *buff = NULL;
list_t *node;
char *q;
if (!var || !value)
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)
{
q = starts_with(node->str, var);
if (q && *q == '=')
{
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);
}