testing: cleanup
[m6w6/libmemcached] / test / lib / env.hpp
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #pragma once
17
18 #include "mem_config.h"
19
20 #include <cstdlib>
21
22 #if HAVE_SETENV
23 # define SET_ENV_EX(n, k, v, overwrite) setenv(k, v, (overwrite))
24 #else // !HAVE_SETENV
25 # define SET_ENV_EX(n, k, v, overwrite) \
26 do { \
27 static char n##_env[] = k "=" v; \
28 if ((overwrite) || !getenv(k)) { \
29 putenv(n##_env); \
30 } \
31 } while (false)
32 #endif
33
34 #define SET_ENV(symbolic_name, literal_env_var, literal_env_val) \
35 SET_ENV_EX(symbolic_name, literal_env_var, literal_env_val, true)
36
37 static inline const char *getenv_else(const char *var, const char *defval) {
38 auto val = getenv(var);
39 if (val && *val) {
40 return val;
41 }
42 return defval;
43 }