BRL-CAD
setenv.c
Go to the documentation of this file.
1 /* S E T E N V . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  *
19  * 3. The name of the author may not be used to endorse or promote
20  * products derived from this software without specific prior written
21  * permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
24  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
27  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include "common.h"
37 
38 #ifndef HAVE_SETENV
39 
40 #include <stdlib.h>
41 #include <string.h>
42 #include <errno.h>
43 #include "bio.h"
44 #include "sysv.h"
45 
46 
47 int
48 setenv(const char *name, const char *value, int overwrite)
49 {
50  int errcode = 0;
51 
52  if (!overwrite) {
53  size_t envsize = 0;
54 
55 #ifdef HAVE_GETENV_S
56  errcode = getenv_s(&envsize, NULL, 0, name);
57 #else
58  if (getenv(name) == NULL)
59  errcode = EINVAL;
60 #endif
61  if (errcode || envsize)
62  return errcode;
63  }
64 #ifdef HAVE__PUTENV_S
65  return _putenv_s(name, value);
66 #else
67  {
68  size_t maxlen = strlen(name)+strlen(value)+2;
69  char *keyval = (char *)malloc(maxlen);
70  if (!keyval)
71  return ENOMEM;
72  snprintf(keyval, maxlen, "%s=%s", name, value);
73  return putenv(keyval);
74  }
75 #endif
76 }
77 
78 #endif /* HAVE_SETENV */
79 
80 
81 /*
82  * Local Variables:
83  * tab-width: 8
84  * mode: C
85  * indent-tabs-mode: t
86  * c-file-style: "stroustrup"
87  * End:
88  * ex: shiftwidth=4 tabstop=8
89  */
Definition: clone.c:90
Header file for the BRL-CAD common definitions.
int setenv(const char *name, const char *value, int overwrite)
Definition: setenv.c:48