-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_management.c
131 lines (104 loc) · 2.23 KB
/
env_management.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "shell.h"
/**
* env_get_key - gets the value of an environment variable
*
* @key: the environment variable of interest
*
* @data: struct of the program's data
*
* Return: pointer value variable or NULL
*/
char *env_get_key(char *key, data_of_program *data)
{
int i, key_length = 0;
if (key == NULL || data->env == NULL)
return (NULL);
key_length = str_length(key);
for (i = 0; data->env[i]; i++)
{
if (str_compare(key, data->env[i], key_length) &&
data->env[i][key_length] == '=')
{
return (data->env[i] + key_length + 1);
}
}
return (NULL);
}
/**
* env_set_key - overwrite the value of the environment variable
*
* @key: name of the variable to set
* @value: new value
* @data: struct of the program's data
* Return: 1 if NULL, 2 if error 0 if success.
*/
int env_set_key(char *key, char *value, data_of_program *data)
{
int i, key_length = 0, is_new_key = 1;
if (key == NULL || value == NULL || data->env == NULL)
return (1);
key_length = str_length(key);
for (i = 0; data->env[i]; i++)
{
if (str_compare(key, data->env[i], key_length) &&
data->env[i][key_length] == '=')
{
is_new_key = 0;
free(data->env[i]);
break;
}
}
data->env[i] = str_concat(str_duplicate(key), "=");
data->env[i] = str_concat(data->env[i], value);
if (is_new_key)
{
data->env[i + 1] = NULL;
}
return (0);
}
/**
* env_remove_key - remove a key from the environment
*
* @key: the key to remove
*
* @data: the sructure of the program's data
*
* Return: 1 if the key was removed, 0 if the key does not exist;
*/
int env_remove_key(char *key, data_of_program *data)
{
int i, key_length = 0;
if (key == NULL || data->env == NULL)
return (0);
key_length = str_length(key);
for (i = 0; data->env[i]; i++)
{
if (str_compare(key, data->env[i], key_length) &&
data->env[i][key_length] == '=')
{
free(data->env[i]);
i++;
for (; data->env[i]; i++)
{
data->env[i - 1] = data->env[i];
}
data->env[i - 1] = NULL;
return (1);
}
}
return (0);
}
/**
* print_environ - prints the current environ
*
* @data: struct for the program's data
*/
void print_environ(data_of_program *data)
{
int j;
for (j = 0; data->env[j]; j++)
{
_print(data->env[j]);
_print("\n");
}
}