forked from bmc/daemonize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setenv.c
44 lines (34 loc) · 1.31 KB
/
setenv.c
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
/*---------------------------------------------------------------------------*\
Version of the POSIX setenv(3) function, implemented in terms of the older
putenv(3) function, for systems that don't have setenv(3).
LICENSE
This source code is released under a BSD-style. See the LICENSE
file for details.
\*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*\
Includes
\*---------------------------------------------------------------------------*/
#include <stdlib.h>
#include <string.h>
#include "config.h"
/*---------------------------------------------------------------------------*\
Public Routines
\*---------------------------------------------------------------------------*/
int setenv(const char *name, const char *value, int overwrite)
{
int res = 0;
if ((name == NULL) || (strlen(name) == 0) || (strchr(name, '=') != NULL))
{
res = -1;
errno = EINVAL;
}
else
{
char *buf = (char *) malloc(strlen(name) + strlen(value) + 2);
strncat(buf, name, strlen(name));
strncat(buf, "=", 1);
strncat(buf, value, strlen(value));
res = putenv(buf);
}
return res;
}