-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.c
More file actions
65 lines (57 loc) · 1.63 KB
/
env.c
File metadata and controls
65 lines (57 loc) · 1.63 KB
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
#include "hdr.h"
int print_env(struct Stringlist *env)
/* Prints each env value, one per line */
{
while (env != NULL) {
write_string(env->str);
print_char('\n');
env = env->next;
}
return 1;
}
int set_env(char **argv, struct Stringlist *env)
{
char *str;
struct Stringlist *new;
while (env->next != NULL) {
env = env->next;
}
new = malloc(sizeof(struct Stringlist));
if (new == NULL) {
perror("malloc");
return -1;
}
env->next = new;
str = concat_strings(argv[1], argv[2], '=');
new->str = str;
new->next = NULL;
return 1;
}
int unset_env(char **argv, struct Stringlist *env)
{
int i;
int len;
struct Stringlist *store;
i = 0;
len = str_len(argv[1]);
while (env != NULL) {
if (strn_compare(argv[1], env->str, len)) {
if (i == 0) {
free(env->str);
store = env->next;
env->str = store->str;
env->next = store->next;
free(store);
} else {
store->next = env->next;
free(env->str);
free(env);
}
return 1;
}
store = env;
env = env->next;
i++;
}
return -1;
}