-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenvironment_lock.cpp
39 lines (34 loc) · 1.26 KB
/
environment_lock.cpp
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
#include "environment_lock.hpp"
#include <mutex>
#include <cstdio>
#ifdef _WIN32
# include <windows.h>
#endif
using namespace std::string_literals;
std::mutex globalProcessExecutionLock;
//#####################################################################################################################
void environmentLockedDo(std::function <void()> const& work)
{
std::lock_guard <std::mutex> {globalProcessExecutionLock};
work();
}
//---------------------------------------------------------------------------------------------------------------------
void doWithModifiedPath(std::function <void()> const& work, std::string const& path)
{
std::lock_guard <std::mutex> {globalProcessExecutionLock};
std::string prevPath{getenv("PATH")};
auto setPath = [](std::string const& to)
{
#ifdef _WIN32
std::string p = "PATH="s + to;
_putenv(p.c_str());
#else
setenv("PATH", to.c_str(), 1);
#endif
};
setPath(path);
work();
setPath(prevPath);
}
//---------------------------------------------------------------------------------------------------------------------
//#####################################################################################################################