-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetenv.c
78 lines (75 loc) · 1.46 KB
/
setenv.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
#include <stdlib.h>
#include "shell.h"
#include "main.h"
#include "lists.h"
/**
* _setenv - function searches the environment list to find the
* environment variable name, and sets to the corresponding
* value string.
* @params: parameters
*/
void _setenv(param_t *params)
{
char *tmp = NULL;
char *name = params->args[1], *value = params->args[2];
list_t *h = params->env_head;
if (params->tokCount != 3)
{
params->status = 0;
return;
}
while (h)
{
if (_strcmp(name, h->str) == 0) /* env var exists */
{
tmp = h->val;
free(tmp);
h->val = _strdup(value);
if (!h->val)
{
write(STDERR_FILENO, "setenv malloc error\n", 18);
exit(-1);
}
h->valLen = _strlen(value);
params->status = 0;
return;
}
h = h->next;
}
/* env var DNE */
params->env_head = add_node(&(params->env_head), name, value);
params->status = 0;
}
/**
* _unsetenv - function searches the environment list to find the
* environment variable name and removes it.
* @params: parameters
*/
void _unsetenv(param_t *params)
{
char *name = params->args[1];
list_t *prev = NULL, *h = params->env_head;
if (params->tokCount != 2)
{
params->status = 0;
return;
}
while (h)
{
if (_strcmp(name, h->str) == 0) /* env var exists */
{
if (h == params->env_head)
params->env_head = h->next;
else
prev->next = h->next;
free(h->str);
free(h->val);
free(h);
params->status = 0;
return;
}
prev = h;
h = h->next;
}
params->status = 0;
}