NCBI C++ ToolKit
setenv.c
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /*
2  * setenv/unsetenv
3  */
4 
5 #include <config.h>
6 
7 #include <stdio.h>
8 
9 #if HAVE_STRING_H
10 #include <string.h>
11 #endif /* HAVE_STRING_H */
12 
13 #if HAVE_ERRNO_H
14 #include <errno.h>
15 #endif /* HAVE_ERRNO_H */
16 
17 #include <freetds/sysdep_private.h>
18 #include <freetds/bool.h>
19 #include <freetds/replacements.h>
20 
21 #ifndef _WIN32
22 #error Platform should have setenv/unsetenv
23 #endif
24 
25 static bool
26 check_name(const char *name)
27 {
28  if (!name || !name[0] || strchr(name, '=') != NULL) {
29  errno = EINVAL;
30  return false;
31  }
32  return true;
33 }
34 
35 static int
36 set_env_value(const char *name, const char *value)
37 {
38  char *s;
39  int res;
40 
41  if (asprintf(&s, "%s=%s", name, value ? value : "") < 0) {
42  errno = ENOMEM;
43  return -1;
44  }
45  res = _putenv(s);
46  free(s);
47  return res;
48 }
49 
50 int
51 tds_setenv(const char *name, const char *value, int overwrite)
52 {
53  if (!check_name(name))
54  return -1;
55  if (!overwrite && getenv(name))
56  return 0;
57  return set_env_value(name, value);
58 }
59 
60 int
61 tds_unsetenv(const char *name)
62 {
63  if (!check_name(name))
64  return -1;
65  return set_env_value(name, NULL);
66 }
#define asprintf
Definition: replacements.h:54
#define NULL
Definition: ncbistd.hpp:225
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
int tds_setenv(const char *name, const char *value, int overwrite)
Definition: setenv.c:51
static bool check_name(const char *name)
Definition: setenv.c:26
static int set_env_value(const char *name, const char *value)
Definition: setenv.c:36
int tds_unsetenv(const char *name)
Definition: setenv.c:61
void free(voidpf ptr)
Modified on Wed Jul 24 17:17:40 2024 by modify_doxy.py rev. 669887