]> git.ipfire.org Git - thirdparty/git.git/blame - compat/setenv.c
index-pack usage of mmap() is unacceptably slower on many OSes other than Linux
[thirdparty/git.git] / compat / setenv.c
CommitLineData
e40b61fb
JR
1#include <stdlib.h>
2#include <string.h>
3
4int gitsetenv(const char *name, const char *value, int replace)
5{
6 int out;
7 size_t namelen, valuelen;
8 char *envstr;
9
10 if (!name || !value) return -1;
11 if (!replace) {
12 char *oldval = NULL;
13 oldval = getenv(name);
14 if (oldval) return 0;
15 }
16
17 namelen = strlen(name);
18 valuelen = strlen(value);
3a267433 19 envstr = malloc((namelen + valuelen + 2));
e40b61fb
JR
20 if (!envstr) return -1;
21
22 memcpy(envstr, name, namelen);
23 envstr[namelen] = '=';
24 memcpy(envstr + namelen + 1, value, valuelen);
25 envstr[namelen + valuelen + 1] = 0;
26
27 out = putenv(envstr);
3a267433
JH
28 /* putenv(3) makes the argument string part of the environment,
29 * and changing that string modifies the environment --- which
30 * means we do not own that storage anymore. Do not free
31 * envstr.
32 */
e40b61fb 33
e40b61fb
JR
34 return out;
35}