-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell_setenv.c
47 lines (46 loc) · 1.02 KB
/
shell_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
#include "shell.h"
/**
* shell_setenv - setting environment variable
* @args: argument list.
* Return: (0) on success, or -1 on failure.
*/
int shell_setenv(char **args)
{
char *name, *value, *new_env;
size_t i = 0;
int j = 0;
if (args[i] == NULL)
{
while (environ[j] != NULL)
{
printf("%s\n", environ[j]);
j++;
}
return (0);
}
name = _strtok(args[i], "="), value = _strtok(NULL, "=");
if ((name == NULL) || (value == NULL))
return (-1);
for (i = 0; environ[i] != NULL; i++)
{
if (strncmp(name, environ[i], _strlen(name)) == 0)
{
if ((args[2] != NULL) && (_strcmp(args[2], "1")) == 0)
snprintf(environ[i], _strlen(name) + _strlen(value) + 2,
"%s=%s", name, value);
return (0);
}
}
new_env = malloc(_strlen(name) + _strlen(value) + 2);
if (new_env == NULL)
return (-1);
sprintf(new_env, "%s=%s", name, value);
environ = realloc(environ, (i + 2) * sizeof(char *));
if (environ == NULL)
{
free(new_env);
return (-1);
}
environ[i++] = new_env, environ[i] = NULL;
return (0);
}