projects
/
oweals
/
musl.git
/ blob
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
history
|
raw
|
HEAD
implement hcreate_r, hdestroy_r and hsearch_r
[oweals/musl.git]
/
src
/
env
/
setenv.c
1
#include <stdlib.h>
2
#include <string.h>
3
#include <errno.h>
4
5
int __putenv(char *s, int a);
6
7
int setenv(const char *var, const char *value, int overwrite)
8
{
9
char *s;
10
int l1, l2;
11
12
if (!var || !*var || strchr(var, '=')) {
13
errno = EINVAL;
14
return -1;
15
}
16
if (!overwrite && getenv(var)) return 0;
17
18
l1 = strlen(var);
19
l2 = strlen(value);
20
s = malloc(l1+l2+2);
21
if (s) {
22
memcpy(s, var, l1);
23
s[l1] = '=';
24
memcpy(s+l1+1, value, l2);
25
s[l1+l2+1] = 0;
26
if (!__putenv(s, 1)) return 0;
27
}
28
free(s);
29
return -1;
30
}