]> git.ipfire.org Git - thirdparty/git.git/blame - compat/setenv.c
commit: refer to commit template as s->fp
[thirdparty/git.git] / compat / setenv.c
CommitLineData
85023577 1#include "../git-compat-util.h"
e40b61fb
JR
2
3int gitsetenv(const char *name, const char *value, int replace)
4{
5 int out;
6 size_t namelen, valuelen;
7 char *envstr;
8
9 if (!name || !value) return -1;
10 if (!replace) {
11 char *oldval = NULL;
12 oldval = getenv(name);
13 if (oldval) return 0;
14 }
15
16 namelen = strlen(name);
17 valuelen = strlen(value);
3a267433 18 envstr = malloc((namelen + valuelen + 2));
e40b61fb
JR
19 if (!envstr) return -1;
20
21 memcpy(envstr, name, namelen);
22 envstr[namelen] = '=';
23 memcpy(envstr + namelen + 1, value, valuelen);
24 envstr[namelen + valuelen + 1] = 0;
25
26 out = putenv(envstr);
3a267433
JH
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.
31 */
e40b61fb 32
e40b61fb
JR
33 return out;
34}