1 /*---------------------------------------------------------------------------*\
2 Version of the POSIX setenv(3) function, implemented in terms of the older
3 putenv(3) function, for systems that don't have setenv(3).
4
5 LICENSE
6
7 This source code is released under a BSD-style. See the LICENSE
8 file for details.
9 \*---------------------------------------------------------------------------*/
10
11
12 /*---------------------------------------------------------------------------*\
13 Includes
14 \*---------------------------------------------------------------------------*/
15
16 #include <stdlib.h>
17 #include <string.h>
18 #include "config.h"
19
20 /*---------------------------------------------------------------------------*\
21 Public Routines
22 \*---------------------------------------------------------------------------*/
23
setenv(const char * name,const char * value,int overwrite)24 int setenv(const char *name, const char *value, int overwrite)
25 {
26 int res = 0;
27
28 if ((name == NULL) || (strlen(name) == 0) || (strchr(name, '=') != NULL))
29 {
30 res = -1;
31 errno = EINVAL;
32 }
33
34 else
35 {
36 char *buf = (char *) malloc(strlen(name) + strlen(value) + 2);
37 strncat(buf, name, strlen(name));
38 strncat(buf, "=", 1);
39 strncat(buf, value, strlen(value));
40 res = putenv(buf);
41 }
42
43 return res;
44 }
45