xref: /oneTBB/test/common/utils_env.h (revision c21e688a)
1 /*
2     Copyright (c) 2005-2022 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #ifndef __TBB_test_common_utils_env_H
18 #define __TBB_test_common_utils_env_H
19 
20 #include "test.h"
21 
22 #include <cstdlib> // getenv/setenv
23 
24 namespace utils {
25 
SetEnv(const char * envname,const char * envval)26 int SetEnv( const char *envname, const char *envval ) {
27     CHECK_MESSAGE( (envname && envval), "Harness::SetEnv() requires two valid C strings" );
28 #if __TBB_WIN8UI_SUPPORT
29     CHECK_MESSAGE( false, "Harness::SetEnv() should not be called in code built for win8ui" );
30     return -1;
31 #elif !(_MSC_VER || __MINGW32__ || __MINGW64__)
32     // On POSIX systems use setenv
33     return setenv(envname, envval, /*overwrite=*/1);
34 #elif __STDC_SECURE_LIB__>=200411
35     // this macro is set in VC & MinGW if secure API functions are present
36     return _putenv_s(envname, envval);
37 #else
38     // If no secure API on Windows, use _putenv
39     size_t namelen = strlen(envname), valuelen = strlen(envval);
40     char* buf = new char[namelen+valuelen+2];
41     strncpy(buf, envname, namelen);
42     buf[namelen] = '=';
43     strncpy(buf+namelen+1, envval, valuelen);
44     buf[namelen+1+valuelen] = char(0);
45     int status = _putenv(buf);
46     delete[] buf;
47     return status;
48 #endif
49 }
50 
GetEnv(const char * envname)51 char* GetEnv(const char *envname) {
52     CHECK_MESSAGE(envname, "Harness::GetEnv() requires a valid C string");
53 #if __TBB_WIN8UI_SUPPORT
54     return nullptr;
55 #else
56     return std::getenv(envname);
57 #endif
58 }
59 
60 } // namespace utils
61 
62 #endif // __TBB_test_common_utils_env_H
63 
64