-
Notifications
You must be signed in to change notification settings - Fork 0
/
shellvars.c
217 lines (213 loc) · 4.26 KB
/
shellvars.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "shell.h"
#include "shellvars.h"
/**
* initsvars - initialize vars
* @ac: arguemnts intiger
* @av: string arguments
* Return: int
*/
int initsvars(int ac, char **av)
{
ShellVar **specialroot = getspecial();
ShellVar *special;
ShellVar *ptr;
int i = 0;
char nums[2] = {0, 0};
/* 0-9, #, $, ?, dash, underscore */
*specialroot = malloc(sizeof(ShellVar) * 15);
if (*specialroot == NULL)
return (-1);
special = *specialroot;
#ifdef DEBUGMODE
printf("special:%p:*getspecial():%p:\n", special, *(getspecial()));
#endif
special->val = _strdup("0");
special->name = _strdup("?");
ptr = special + 1;
special->next = ptr;
while (av[i] != NULL)
{
#ifdef DEBUGMODE
printf("av[%d]=%s\n", i, av[i]);
#endif
nums[0] = i + '0';
ptr->val = _strdup(av[i]);
ptr->name = _strdup(nums);
ptr->next = ptr + 1;
ptr = ptr->next;
i++;
}
while (i < 10)
{
nums[0] = i + '0';
ptr->val = _strdup("0");
ptr->name = _strdup(nums);
ptr->next = ptr + 1;
ptr = ptr->next;
i++;
}
ptr->name = _strdup("$");
ptr->val = _strdup("0");
ptr->next = ptr + 1;
ptr = ptr->next;
ptr->name = _strdup("#");
ptr->val = itos(ac);
ptr->next = NULL;
return (0);
}
/**
* getsvar - gets shell variable
* @name: name of shell var
*
* Return: original argument if not found
*/
char *getsvar(char *name)
{
ShellVar *special = *(getspecial()), *vars = *(getvars());
ShellVar *ptr = special;
while (ptr != NULL && _strcmp(ptr->name, name))
{
#ifdef DEBUGMODE
printf("Checked .%s. against .%s.\n", name, ptr->name);
#endif
ptr = ptr->next;
}
if (ptr != NULL)
{
#ifdef DEBUGMODE
printf("Returning special var %s:%s\n", ptr->name, ptr->val);
#endif
return (_strdup(ptr->val));
}
ptr = vars;
while (ptr != NULL && _strcmp(ptr->name, name))
{
#ifdef DEBUGMODE
printf("Checked .%s. against .%s.\n", name, ptr->name);
#endif
ptr = ptr->next;
}
if (ptr == NULL)
{
#ifdef DEBUGMODE
printf("Var not found %s\n", name);
#endif
return (name);
}
#ifdef DEBUGMODE
printf("Returning var %s\n", ptr->val);
#endif
return (_strdup(ptr->val));
}
/**
* setsvar - sets shell var
* @name: name of var
* @val: value of var
* Return: int
*/
int setsvar(char *name, char *val)
{
ShellVar **vars = getvars();
ShellVar *special = *(getspecial());
ShellVar *ptr = special, *new;
#ifdef DEBUGMODE
printf("in setsvar, special address: %p\n", special);
printf("Got %s %s\n", name, val);
#endif
while (_strcmp(ptr->name, name) && ptr->next != NULL)
{
ptr = ptr->next;
}
if (!_strcmp(ptr->name, name))
{
#ifdef DEBUGMODE
printf("Setting special %s to %s\n", ptr->name, val);
printf("ptr -> val %p\n", ptr->val);
#endif
free(ptr->val);
ptr->val = _strdup(val);
return (0);
}
ptr = *vars;
#ifdef DEBUGMODE
printf("vars address %p\n", *vars);
#endif
if (ptr == NULL)
{
#ifdef DEBUGMODE
printf("Setting new list %s to %s\n", name, val);
#endif
new = malloc(sizeof(ShellVar));
if (new == NULL)
return (-1);
new->name = _strdup(name);
new->val = _strdup(val);
new->next = NULL;
*vars = new;
return (0);
}
while (_strcmp(ptr->name, name) && ptr->next != NULL)
ptr = ptr->next;
if (ptr != NULL && !_strcmp(ptr->name, name))
{
#ifdef DEBUGMODE
printf("Setting %s to %s\n", ptr->name, val);
#endif
free(ptr->val);
ptr->val = _strdup(val);
}
else
{
#ifdef DEBUGMODE
printf("Setting new %s to %s\n", name, val);
#endif
new = malloc(sizeof(ShellVar));
if (new == NULL)
return (-1);
new->name = _strdup(name);
new->val = _strdup(val);
new->next = NULL;
ptr->next = new;
}
return (0);
}
/**
* unsetsvar - unset shell var
* @name: name of var to unset
* Return: int
*/
int unsetsvar(char *name)
{
ShellVar *vars = *getvars();
ShellVar *ptr = vars, *next;
#ifdef DEBUGMODE
printf("In unsetsvar:vars:%p:name:%s\n", vars, name);
#endif
if (vars == NULL)
return (0);
#ifdef DEBUGMODE
printf("ptr->name:%s\n", ptr->name);
#endif
if (!_strcmp(ptr->name, name))
{
#ifdef DEBUGMODE
printf("First node match\n");
#endif
*vars = *vars->next;
free(ptr->name);
free(ptr->val);
free(ptr);
return (0);
}
while (ptr->next != NULL && _strcmp(ptr->next->name, name))
ptr = ptr->next;
if (!_strcmp(ptr->next->name, name))
{
next = ptr->next->next;
free(ptr->next->name);
free(ptr->next->val);
free(ptr->next);
ptr->next = next;
}
return (0);
}