GIT 1.6.0.1
[git/mingw.git] / compat / setenv.c
blob3a22ea7b751efb768d72afa2f97fd963e10eec7e
1 #include "../git-compat-util.h"
3 int gitsetenv(const char *name, const char *value, int replace)
5 int out;
6 size_t namelen, valuelen;
7 char *envstr;
9 if (!name || !value) return -1;
10 if (!replace) {
11 char *oldval = NULL;
12 oldval = getenv(name);
13 if (oldval) return 0;
16 namelen = strlen(name);
17 valuelen = strlen(value);
18 envstr = malloc((namelen + valuelen + 2));
19 if (!envstr) return -1;
21 memcpy(envstr, name, namelen);
22 envstr[namelen] = '=';
23 memcpy(envstr + namelen + 1, value, valuelen);
24 envstr[namelen + valuelen + 1] = 0;
26 out = putenv(envstr);
27 /* putenv(3) makes the argument string part of the environment,
28 * and changing that string modifies the environment --- which
29 * means we do not own that storage anymore. Do not free
30 * envstr.
33 return out;