-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.c
165 lines (142 loc) · 2.81 KB
/
env.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "main.h"
/*
* int main(int ac, char **av, char **env)
* {
* printf("%p\n", environ[0]);
* printf("%p\n", env[0]);
* printf("%s\n", _getenv("PATH"));
* printf("%s\n", _getenv("PWD"));
* printenv();
* return (0);
* }
*/
/**
* printenv - displays current environment
*
*/
void printenv(void)
{
unsigned int i = 0;
while (environ[i])
{
_puts(environ[i]);
_putchar('\n');
i++;
}
}
/**
* _getenv - gets an environment variable
* @name: variable to search in the environment
*
* Return: pointer to the value in the environment, NULL if there is no match
*/
char *_getenv(const char *name)
{
unsigned int i = 0;
int diff;
int varlen = _strlen(name);
while (environ[i])
{
diff = _strncmp(environ[i], name, varlen);
if (diff == 0)
return (environ[i] + varlen + 1);
i++;
}
return (NULL);
}
/**
* envloc - locates a variable in the environment
* @name: variable to be located in the environment
*
* Return: location in environment array on success, -1 on error
*/
int envloc(const char *name)
{
unsigned int i = 0;
int diff;
int varlen = _strlen(name);
while (environ[i])
{
diff = _strncmp(environ[i], name, varlen);
if (diff == 0)
return (i);
i++;
}
return (-1);
}
/**
* _setenv - changes or adds an environment variable
* @name: name of variable to be changed/added
* @value: value of newly set variable
* @overwrite: previous value of newly set variable
*
* Return: 0 on success, -1 on error
*/
int _setenv(const char *name, const char *value, int overwrite)
{
char *nameCpy = _strdup(name), *valueCpy = _strdup(value);
int i, len = 0, loc = envloc(name);
char **new_environ;
char *temp = strcpycat(nameCpy, "=");
free(nameCpy);
nameCpy = strcpycat(temp, valueCpy);
free(valueCpy);
free(temp);
if (loc != -1)
{
if (overwrite == 0)
return (0);
environ[loc] = nameCpy;
return (0);
}
while (environ[len])
len++;
new_environ = malloc(sizeof(*new_environ) * (len + 2));
if (!new_environ)
{
free(nameCpy);
return (-1);
}
for (i = 0; i < len; i++)
new_environ[i] = environ[i];
new_environ[i] = nameCpy;
free(nameCpy);
new_environ[i + 1] = NULL;
free(environ);
free_array(environ);
environ = new_environ;
return (0);
}
/**
* _unsetenv - deletes the variable name from the environment
* @name: name of variable to be deleted
*
* Return: 0 on success, -1 on error
*/
int _unsetenv(const char *name)
{
int loc = envloc(name);
int i, j, len = 0;
char **new_environ;
while (environ[len])
len++;
if (loc != -1)
{
free(environ[loc]);
new_environ = malloc(sizeof(*new_environ) * len);
j = 0;
for (i = 0; i < len; i++)
{
if (i != loc)
{
new_environ[i] = environ[j];
j++;
}
}
new_environ[i] = NULL;
free(environ);
free_array(environ);
environ = new_environ;
}
return (0);
}