]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | // |
2 | // Copyright (c) 2012 Artyom Beilis (Tonkikh) | |
3 | // Copyright (c) 2020 Alexander Grund | |
4 | // | |
5 | // Distributed under the Boost Software License, Version 1.0. (See | |
6 | // accompanying file LICENSE_1_0.txt or copy at | |
7 | // http://www.boost.org/LICENSE_1_0.txt) | |
8 | // | |
9 | ||
10 | #define BOOST_NOWIDE_SOURCE | |
11 | ||
12 | #ifdef _MSC_VER | |
13 | #define _CRT_SECURE_NO_WARNINGS | |
14 | #elif(defined(__MINGW32__) || defined(__CYGWIN__)) && defined(__STRICT_ANSI__) | |
15 | // Need the _w* functions which are extensions on MinGW/Cygwin | |
16 | #undef __STRICT_ANSI__ | |
17 | #endif | |
18 | ||
19 | #include <boost/nowide/cstdlib.hpp> | |
20 | ||
21 | #if !defined(BOOST_WINDOWS) | |
22 | namespace boost { | |
23 | namespace nowide { | |
24 | int setenv(const char* key, const char* value, int overwrite) | |
25 | { | |
26 | return ::setenv(key, value, overwrite); | |
27 | } | |
28 | ||
29 | int unsetenv(const char* key) | |
30 | { | |
31 | return ::unsetenv(key); | |
32 | } | |
33 | ||
34 | int putenv(char* string) | |
35 | { | |
36 | return ::putenv(string); | |
37 | } | |
38 | } // namespace nowide | |
39 | } // namespace boost | |
40 | #else | |
41 | #include <boost/nowide/stackstring.hpp> | |
42 | #include <vector> | |
43 | #include <windows.h> | |
44 | ||
45 | namespace boost { | |
46 | namespace nowide { | |
47 | char* getenv(const char* key) | |
48 | { | |
49 | static stackstring value; | |
50 | ||
51 | const wshort_stackstring name(key); | |
52 | ||
53 | static const size_t buf_size = 64; | |
54 | wchar_t buf[buf_size]; | |
55 | std::vector<wchar_t> tmp; | |
56 | wchar_t* ptr = buf; | |
57 | size_t n = GetEnvironmentVariableW(name.get(), buf, buf_size); | |
58 | if(n == 0 && GetLastError() == 203) // ERROR_ENVVAR_NOT_FOUND | |
59 | return 0; | |
60 | if(n >= buf_size) | |
61 | { | |
62 | tmp.resize(n + 1, L'\0'); | |
63 | n = GetEnvironmentVariableW(name.get(), &tmp[0], static_cast<unsigned>(tmp.size() - 1)); | |
64 | // The size may have changed | |
65 | if(n >= tmp.size() - 1) | |
66 | return 0; | |
67 | ptr = &tmp[0]; | |
68 | } | |
69 | value.convert(ptr); | |
70 | return value.get(); | |
71 | } | |
72 | ||
73 | int setenv(const char* key, const char* value, int overwrite) | |
74 | { | |
75 | const wshort_stackstring name(key); | |
76 | if(!overwrite) | |
77 | { | |
78 | wchar_t unused[2]; | |
79 | if(GetEnvironmentVariableW(name.get(), unused, 2) != 0 || GetLastError() != 203) // ERROR_ENVVAR_NOT_FOUND | |
80 | return 0; | |
81 | } | |
82 | const wstackstring wval(value); | |
83 | if(SetEnvironmentVariableW(name.get(), wval.get())) | |
84 | return 0; | |
85 | return -1; | |
86 | } | |
87 | ||
88 | int unsetenv(const char* key) | |
89 | { | |
90 | const wshort_stackstring name(key); | |
91 | if(SetEnvironmentVariableW(name.get(), 0)) | |
92 | return 0; | |
93 | return -1; | |
94 | } | |
95 | ||
96 | int putenv(char* string) | |
97 | { | |
98 | const char* key = string; | |
99 | const char* key_end = string; | |
100 | while(*key_end != '=' && *key_end != '\0') | |
101 | key_end++; | |
102 | if(*key_end == '\0') | |
103 | return -1; | |
104 | const wshort_stackstring wkey(key, key_end); | |
105 | const wstackstring wvalue(key_end + 1); | |
106 | ||
107 | if(SetEnvironmentVariableW(wkey.get(), wvalue.get())) | |
108 | return 0; | |
109 | return -1; | |
110 | } | |
111 | ||
112 | int system(const char* cmd) | |
113 | { | |
114 | if(!cmd) | |
115 | return _wsystem(0); | |
116 | const wstackstring wcmd(cmd); | |
117 | return _wsystem(wcmd.get()); | |
118 | } | |
119 | } // namespace nowide | |
120 | } // namespace boost | |
121 | #endif |