-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathset_unset.c
More file actions
112 lines (100 loc) · 1.56 KB
/
set_unset.c
File metadata and controls
112 lines (100 loc) · 1.56 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
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
#include "shell.h"
/**
* search_env - env variable in linked list
* @env: envronment
* @s: string
*
* Return: index
*/
int search_env(list_t *env, char *s)
{
int index = 0;
int e = 0;
while (env != NULL)
{
for (e = 0; (env->var)[e] == s[e]; e++)
;
if (s[e] == '\0')
{
break;
}
env = env->next;
index++;
}
if (env == NULL)
return (-1);
return (index);
}
/**
* sh_setenv - modify env variable
* @env: environment
* @s: string
*
* Return: 0 or 1
*/
int sh_setenv(list_t **env, char **s)
{
int e = 0;
int idx = 0;
char *c;
list_t *fresh_n;
if (s[1] == NULL || s[2] == NULL)
{
write(STDOUT_FILENO, "Few args\n", 10);
sh_free_double_ptr(s);
return (-1);
}
c = _strdup(s[1]);
c = _strcat(c, "=");
c = _strcat(c, s[2]);
idx = search_env(*env, s[1]);
if (idx == -1)
{
add_node_end(env, c);
}
else
{
fresh_n = *env;
for (e = 0; e < idx; e++)
{
fresh_n = fresh_n->next;
}
free(fresh_n->var);
fresh_n->var = _strdup(c);
}
free(c);
sh_free_double_ptr(s);
return (0);
}
/**
* sh_unsetenv - remove env variable
* @env: environment
* @s: string command
*
*Return: 0
*/
int sh_unsetenv(list_t **env, char **s)
{
int e = 0;
int idx = 0;
if (s[1] == NULL)
{
write(STDOUT_FILENO, "Few args\n", 10);
sh_free_double_ptr(s);
return (-1);
}
idx = search_env(*env, s[1]);
sh_free_double_ptr(s);
if (idx == -1)
{
write(STDOUT_FILENO, "Can't find\n", 12);
return (-1);
}
e = delete_node_at_index(env, idx);
if (e == -1)
{
write(STDOUT_FILENO, "Can't find\n", 12);
return (-1);
}
return (0);
}