-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_management.c
128 lines (110 loc) · 3.24 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
#include "simpleshell.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: a pointer to the value of the variable or NULL if it doesn't exist
*/
char *env_get_key(char *key, data_of_program *data)
{
int i, key_length = 0;
/* validate the arguments */
if (key == NULL || data->env == NULL)
return (NULL);
/* obtains the leng of the variable requested */
key_length = str_length(key);
for (i = 0; data->env[i]; i++)
{/* Iterates through the environ and check for coincidence of the vame */
if (str_compare(key, data->env[i], key_length) &&
data->env[i][key_length] == '=')
{/* returns the value of the key NAME= when find it*/
return (data->env[i] + key_length + 1);
}
}
/* returns NULL if did not find it */
return (NULL);
}
/**
* env_set_key - overwrite the value of the environment variable
* or create it if does not exist.
* @key: name of the variable to set
* @value: new value
* @data: struct of the program's data
* Return: 1 if the parameters are NULL, 2 if there is an erroror 0 if sucess.
*/
int env_set_key(char *key, char *value, data_of_program *data)
{
int i, key_length = 0, is_new_key = 1;
/* validate the arguments */
if (key == NULL || value == NULL || data->env == NULL)
return (1);
/* obtains the leng of the variable requested */
key_length = str_length(key);
for (i = 0; data->env[i]; i++)
{/* Iterates through the environ and check for coincidence of the vame */
if (str_compare(key, data->env[i], key_length) &&
data->env[i][key_length] == '=')
{/* If key already exists */
is_new_key = 0;
/* free the entire variable, it is new created below */
free(data->env[i]);
break;
}
}
/* make an string of the form key=value */
data->env[i] = str_concat(str_duplicate(key), "=");
data->env[i] = str_concat(data->env[i], value);
if (is_new_key)
{/* if the variable is new, it is create at end of actual list and we need*/
/* to put the NULL value in the next position */
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;
/* validate the arguments */
if (key == NULL || data->env == NULL)
return (0);
/* obtains the leng of the variable requested */
key_length = str_length(key);
for (i = 0; data->env[i]; i++)
{/* iterates through the environ and checks for coincidences */
if (str_compare(key, data->env[i], key_length) &&
data->env[i][key_length] == '=')
{/* if key already exists, remove them */
free(data->env[i]);
/* move the others keys one position down */
i++;
for (; data->env[i]; i++)
{
data->env[i - 1] = data->env[i];
}
/* put the NULL value at the new end of the list */
data->env[i - 1] = NULL;
return (1);
}
}
return (0);
}
/**
* print_environ - prints the current environ
* @data: struct for the program's data
* Return: nothing
*/
void print_environ(data_of_program *data)
{
int j;
for (j = 0; data->env[j]; j++)
{
_print(data->env[j]);
_print("\n");
}
}