forked from cryfs/cryfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.cpp
More file actions
49 lines (37 loc) · 965 Bytes
/
env.cpp
File metadata and controls
49 lines (37 loc) · 965 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "env.h"
#include <stdexcept>
#include <string>
#include <cerrno>
#if !defined(_MSC_VER)
#include <cstdlib>
namespace cpputils {
void setenv(const char* key, const char* value) {
int retval = ::setenv(key, value, 1);
if (0 != retval) {
throw std::runtime_error("Error setting environment variable. Errno: " + std::to_string(errno));
}
}
void unsetenv(const char* key) {
int retval = ::unsetenv(key);
if (0 != retval) {
throw std::runtime_error("Error unsetting environment variable. Errno: " + std::to_string(errno));
}
}
}
#else
#include <Windows.h>
#include <sstream>
namespace cpputils {
void setenv(const char* key, const char* value) {
std::ostringstream command;
command << key << "=" << value;
int retval = _putenv(command.str().c_str());
if (0 != retval) {
throw std::runtime_error("Error setting environment variable. Errno: " + std::to_string(errno));
}
}
void unsetenv(const char* key) {
setenv(key, "");
}
}
#endif