--- /dev/null
+#ifndef ENVIRON_H_
+#define ENVIRON_H_
+
+#include <string>
+
+namespace Msp {
+
+std::string getenv(const std::string &);
+void setenv(const std::string &, const std::string &);
+void unsetenv(const std::string &);
+
+} // namespace Msp
+
+#endif
--- /dev/null
+#include <cstdlib>
+#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());
+}
+
+}
--- /dev/null
+#include <stdlib.h>
+#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(), "");
+}
+
+}
-#include <cstdlib>
-#include <unistd.h>
#include <dirent.h>
#include <msp/core/application.h>
+#include <msp/core/environ.h>
#include <msp/core/systemerror.h>
#include <msp/strings/regex.h>
#include <msp/strings/utils.h>
{
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))
break;
}
}
+ }
if(exe.empty())
exe = realpath(argv0);
Path path_lookup(const string &name)
{
- const char *path = getenv("PATH");
+ string path = getenv("PATH");
vector<string> dirs = split(path, ITEMSEP);
return path_lookup(name, vector<Path>(dirs.begin(), dirs.end()));
}
#include <msp/core/application.h>
+#include <msp/core/environ.h>
#include "dir.h"
using namespace std;
if(len)
return string(buf, len);
- const char *home = getenv("HOME");
- if(home)
+ string home = getenv("HOME");
+ if(!home.empty())
return home;
return ".";
-#include <cstdlib>
#include <msp/core/application.h>
+#include <msp/core/environ.h>
#include "dir.h"
using namespace std;
Path get_home_dir()
{
- const char *home = getenv("HOME");
- if(home)
+ string home = getenv("HOME");
+ if(!home.empty())
return home;
return ".";
}