]> git.tdb.fi Git - libs/core.git/blob - source/core/windows/environ.cpp
Add dllimport to the declaration of getenv_s on Windows
[libs/core.git] / source / core / windows / environ.cpp
1 #include <stdlib.h>
2 #include "environ.h"
3 #include "mutex.h"
4
5 #ifdef __GNUC__
6 extern "C" __declspec(dllimport) errno_t getenv_s(size_t *, char *, size_t, const char *);
7 #endif
8
9 using namespace std;
10
11 namespace Msp {
12
13 static Mutex &env_mutex()
14 {
15         static Mutex mutex;
16         return mutex;
17 }
18
19 string getenv(const string &name)
20 {
21         MutexLock _lock(env_mutex());
22         char buffer[1024];
23         size_t result;
24         if(!getenv_s(&result, buffer, sizeof(buffer), name.c_str()))
25                 return buffer;
26         else
27                 return string();
28 }
29
30 void setenv(const string &name, const string &value)
31 {
32         MutexLock _lock(env_mutex());
33         _putenv_s(name.c_str(), value.c_str());
34 }
35
36 void unsetenv(const string &name)
37 {
38         MutexLock _lock(env_mutex());
39         _putenv_s(name.c_str(), "");
40 }
41
42 }