-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_unsetenv.c
More file actions
57 lines (52 loc) · 1.05 KB
/
Copy pathcustom_unsetenv.c
File metadata and controls
57 lines (52 loc) · 1.05 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
#include "main.h"
/**
*_unsetenv - unsets environment variable
*@data: shell data
*Return: must return 0 on success
*/
int _unsetenv(shell_data *data)
{
int i = 0, j;
char *env, *name;
while (data->_environ[i])
{
env = _strdup(data->_environ[i]);
name = _strtok(env, "=");
if (_strcmp(name, data->args[1]) == 0)
{
free(data->_environ[i]);
for (j = i; data->_environ[j]; j++)
data->_environ[j] = data->_environ[j + 1];
freeInputArgsData(data, 1, 0, 1);
free(env);
return (0);
}
free(env);
i++;
}
return (-1);
}
/**
*unsetenvCommand - unsetenv command
*@data: shell data
*/
void unsetenvCommand(shell_data *data)
{
if (data->args[2] != NULL)
{
printError(data, "./shell", ": too many arguments\n", 127);
freeInputArgsData(data, 1, 0, 1);
return;
}
if (data->args[1] == NULL)
{
printError(data, "./shell", ": not enough arguments\n", 127);
freeInputArgsData(data, 1, 0, 1);
return;
}
if (_unsetenv(data) == -1)
{
printError(data, "./shell", ": var do not exist\n", 127);
freeInputArgsData(data, 1, 0, 1);
}
}