forked from EugeneJoe/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setenv.c
36 lines (34 loc) · 767 Bytes
/
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
#include "shellheader.h"
/**
* _setenv - Initialize a new environment variable, or modify an existing one
* @var: pointer to name of environment variable
* @value: pointer to value of environment variable
* @env: pointer to array of pointers to the environment variables
*
* Return: 0 on success
*/
int _setenv(char *var, char *value, char **env)
{
char *env_var;
int idx = 0, var_len = 0, status = 0;
var_len = _strlen(var);
env_var = buildstr(var, "=", value);
for (idx = 0; env[idx]; idx++)
{
if (_strncmp(env[idx], var, var_len) == 0)
{
free(env[idx]);
env[idx] = strdup(env_var);
status = 1;
break;
}
}
if (status != 1)
{
free(env[idx]);
env[idx] = _strdup(env_var);
env[++idx] = NULL;
}
free(env_var);
return (0);
}