-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenviroment_func2.c
92 lines (82 loc) · 1.88 KB
/
enviroment_func2.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
#include "s_shell.h"
/**
* copyEnvironToStringArray - returns the string array copy of our environment
* @info: Struct
* Return: 0
*/
char **copyEnvironToStringArray(info_t *info)
{
if (!info->environ || info->environmentChanged)
{
info->environ = listToString(info->environment);
info->environmentChanged = 0;
}
return (info->environ);
}
/**
* removeEnvironmentVariable - Remove an environment variable
* @info: Structure
* @variable: the string environment variable property
* Return: 1 on delete, 0 otherwise
*/
int removeEnvironmentVariable(info_t *info, char *variable)
{
str_list_t *node = info->environment;
size_t index = 0;
char *p;
if (!node || !variable)
return (0);
while (node)
{
p = _starts_with(node->str, variable);
if (p && *p == '=')
{
info->environmentChanged = deleteNodeAtIndex(&(info->environment), index);
index = 0;
node = info->environment;
continue;
}
node = node->next;
index++;
}
return (info->environmentChanged);
}
/**
* setEnvironmentVariable - Initialize a new environment variable,
* or modify an existing one
* @info: Structure
* @variable: the string environment variable property
* @value: the string environment variable value
* Return: Always 0
*/
int setEnvironmentVariable(info_t *info, char *variable, char *value)
{
char *buffer = NULL;
str_list_t *node;
char *p;
if (!variable || !value)
return (0);
buffer = malloc(_strlen(variable) + _strlen(value) + 2);
if (!buffer)
return (1);
_strcpy(buffer, variable);
_strcat(buffer, "=");
_strcat(buffer, value);
node = info->environment;
while (node)
{
p = _starts_with(node->str, variable);
if (p && *p == '=')
{
free(node->str);
node->str = buffer;
info->environmentChanged = 1;
return (0);
}
node = node->next;
}
addNodeEnd(&(info->environment), buffer, 0);
free(buffer);
info->environmentChanged = 1;
return (0);
}