-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetenv.c
More file actions
80 lines (77 loc) · 1.79 KB
/
Copy pathsetenv.c
File metadata and controls
80 lines (77 loc) · 1.79 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
#include "main.h"
/**
* concatenate - Concatenate strings
* @new_var: The destination buffer
* @name: The first string
* @equal: The second string
* @value: The third string
*/
void concatenate(char *new_var, char *name, char *equal, char *value)
{
_strcpy(new_var, name);
_strcat(new_var, equal);
_strcat(new_var, value);
}
/**
* _perro - Print an error message and return -1
* @c: The error message to print
*
* This function prints an error message using perror() and returns -1.
*
* Return: Always returns -1.
*/
int _perro(char *c)
{
perror(c);
return (-1);
}
/**
* _setenv - Set or update an environment variable
* @name: The name of the variable
* @value: The value to assign to the variable
* @overwrite: If 1, overwrite the variable if it already exists; if 0, keep it
*
* Return: 0 on success, -1 on failure
*/
int _setenv(const char *name, const char *value, int overwrite)
{
int i;
char *var, *token, *new_var;
if (name == NULL || _strchr((char *)name, '=') != NULL || value == NULL)
{
errno = EINVAL;
return (_perro("setenv"));
}
for (i = 0; environ[i]; i++)
{
var = _strdup(environ[i]);
token = _strtok(var, "=");
if (token != NULL && strcmp((char *)name, (char *)token) == 0)
{
if (overwrite != 0)
{
new_var = malloc(_strlen((char *)name) + 1 + _strlen((char *)value) + 1);
if (new_var == NULL)
{
perror("malloc");
free(var);
return (-1);
}
concatenate((char *)new_var, (char *)name, "=", (char *)value);
environ[i] = new_var;
free(var);
}
return (0);
}
free(var);
}
new_var = malloc(_strlen((char *)name) + 1 + _strlen((char *)value) + 1);
if (new_var == NULL)
{
return (_perro("malloc"));
}
concatenate((char *)new_var, (char *)name, "=", (char *)value);
environ[i] = new_var;
environ[i + 1] = NULL;
return (0);
}