From f804a61c1c58529e7c98555a921b56bc05059d5e Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 30 Aug 2021 02:15:57 +0300 Subject: [PATCH] Add wrappers for environment manipulation functions --- source/core/environ.h | 14 +++++++++++ source/core/unix/environ.cpp | 34 ++++++++++++++++++++++++++ source/core/windows/environ.cpp | 42 +++++++++++++++++++++++++++++++++ source/fs/dir.cpp | 10 ++++---- source/fs/osx/dir_location.cpp | 5 ++-- source/fs/unix/dir_location.cpp | 6 ++--- 6 files changed, 102 insertions(+), 9 deletions(-) create mode 100644 source/core/environ.h create mode 100644 source/core/unix/environ.cpp create mode 100644 source/core/windows/environ.cpp diff --git a/source/core/environ.h b/source/core/environ.h new file mode 100644 index 0000000..67fde05 --- /dev/null +++ b/source/core/environ.h @@ -0,0 +1,14 @@ +#ifndef ENVIRON_H_ +#define ENVIRON_H_ + +#include + +namespace Msp { + +std::string getenv(const std::string &); +void setenv(const std::string &, const std::string &); +void unsetenv(const std::string &); + +} // namespace Msp + +#endif diff --git a/source/core/unix/environ.cpp b/source/core/unix/environ.cpp new file mode 100644 index 0000000..037a0e9 --- /dev/null +++ b/source/core/unix/environ.cpp @@ -0,0 +1,34 @@ +#include +#include "environ.h" +#include "mutex.h" + +using namespace std; + +namespace Msp { + +static Mutex &env_mutex() +{ + static Mutex mutex; + return mutex; +} + +string getenv(const string &name) +{ + MutexLock _lock(env_mutex()); + const char *value = std::getenv(name.c_str()); + return (value ? string(value) : string()); +} + +void setenv(const string &name, const string &value) +{ + MutexLock _lock(env_mutex()); + ::setenv(name.c_str(), value.c_str(), true); +} + +void unsetenv(const string &name) +{ + MutexLock _lock(env_mutex()); + ::unsetenv(name.c_str()); +} + +} diff --git a/source/core/windows/environ.cpp b/source/core/windows/environ.cpp new file mode 100644 index 0000000..ff5c2b0 --- /dev/null +++ b/source/core/windows/environ.cpp @@ -0,0 +1,42 @@ +#include +#include "environ.h" +#include "mutex.h" + +#ifdef __GNUC__ +extern "C" errno_t getenv_s(size_t *, char *, size_t, const char *); +#endif + +using namespace std; + +namespace Msp { + +static Mutex &env_mutex() +{ + static Mutex mutex; + return mutex; +} + +string getenv(const string &name) +{ + MutexLock _lock(env_mutex()); + char buffer[1024]; + size_t result; + if(!getenv_s(&result, buffer, sizeof(buffer), name.c_str())) + return buffer; + else + return string(); +} + +void setenv(const string &name, const string &value) +{ + MutexLock _lock(env_mutex()); + _putenv_s(name.c_str(), value.c_str()); +} + +void unsetenv(const string &name) +{ + MutexLock _lock(env_mutex()); + _putenv_s(name.c_str(), ""); +} + +} diff --git a/source/fs/dir.cpp b/source/fs/dir.cpp index 0980203..218a7a5 100644 --- a/source/fs/dir.cpp +++ b/source/fs/dir.cpp @@ -1,7 +1,6 @@ -#include -#include #include #include +#include #include #include #include @@ -38,7 +37,9 @@ const Path &get_bin_dir(const string &argv0) { Path exe; if(argv0.find(DIRSEP)==string::npos) - if(const char *path = getenv("PATH")) + { + string path = getenv("PATH"); + if(!path.empty()) { for(const string &d: split(path, ITEMSEP)) if(exists(Path(d)/argv0)) @@ -47,6 +48,7 @@ const Path &get_bin_dir(const string &argv0) break; } } + } if(exe.empty()) exe = realpath(argv0); @@ -191,7 +193,7 @@ Path path_lookup(const string &name, const vector &paths) Path path_lookup(const string &name) { - const char *path = getenv("PATH"); + string path = getenv("PATH"); vector dirs = split(path, ITEMSEP); return path_lookup(name, vector(dirs.begin(), dirs.end())); } diff --git a/source/fs/osx/dir_location.cpp b/source/fs/osx/dir_location.cpp index 89d6f57..b58b971 100644 --- a/source/fs/osx/dir_location.cpp +++ b/source/fs/osx/dir_location.cpp @@ -1,4 +1,5 @@ #include +#include #include "dir.h" using namespace std; @@ -16,8 +17,8 @@ Path get_home_dir() if(len) return string(buf, len); - const char *home = getenv("HOME"); - if(home) + string home = getenv("HOME"); + if(!home.empty()) return home; return "."; diff --git a/source/fs/unix/dir_location.cpp b/source/fs/unix/dir_location.cpp index c980bb2..7382587 100644 --- a/source/fs/unix/dir_location.cpp +++ b/source/fs/unix/dir_location.cpp @@ -1,5 +1,5 @@ -#include #include +#include #include "dir.h" using namespace std; @@ -9,8 +9,8 @@ namespace FS { Path get_home_dir() { - const char *home = getenv("HOME"); - if(home) + string home = getenv("HOME"); + if(!home.empty()) return home; return "."; } -- 2.43.0