2016-03-13 01:28:39 +00:00
|
|
|
/* Environment handling.
|
|
|
|
*
|
|
|
|
* This file is part of OctaSTD. See COPYING.md for futher information.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OSTD_ENVIRON_HH
|
|
|
|
#define OSTD_ENVIRON_HH
|
|
|
|
|
2016-07-06 18:40:43 +00:00
|
|
|
#include "ostd/platform.hh"
|
|
|
|
#include "ostd/internal/win32.hh"
|
|
|
|
|
2016-03-13 01:28:39 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "ostd/maybe.hh"
|
|
|
|
#include "ostd/string.hh"
|
|
|
|
|
2016-07-06 18:40:43 +00:00
|
|
|
/* TODO: make POSIX version thread safe, the Windows version is... */
|
|
|
|
|
2016-03-13 01:28:39 +00:00
|
|
|
namespace ostd {
|
|
|
|
|
2016-07-08 18:48:11 +00:00
|
|
|
inline Maybe<String> env_get(ConstCharRange name) {
|
2016-03-13 01:28:39 +00:00
|
|
|
char buf[256];
|
2016-07-06 18:40:43 +00:00
|
|
|
auto tbuf = to_temp_cstr(name, buf, sizeof(buf));
|
|
|
|
#ifndef OSTD_PLATFORM_WIN32
|
|
|
|
char const *ret = getenv(tbuf.get());
|
2016-07-31 19:40:25 +00:00
|
|
|
if (!ret) {
|
|
|
|
return ostd::nothing;
|
|
|
|
}
|
2016-07-06 18:40:43 +00:00
|
|
|
return ostd::move(String(ret));
|
|
|
|
#else
|
|
|
|
String rbuf;
|
|
|
|
for (;;) {
|
2016-07-31 19:40:25 +00:00
|
|
|
auto ret = GetEnvironmentVariable(
|
|
|
|
tbuf.get(), rbuf.data(), rbuf.capacity() + 1
|
|
|
|
);
|
|
|
|
if (!ret) {
|
|
|
|
return ostd::nothing;
|
|
|
|
}
|
2016-07-06 18:40:43 +00:00
|
|
|
if (ret <= rbuf.capacity()) {
|
|
|
|
rbuf.advance(ret);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rbuf.reserve(ret - 1);
|
|
|
|
}
|
|
|
|
return ostd::move(rbuf);
|
|
|
|
#endif
|
2016-03-13 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
2016-07-31 19:40:25 +00:00
|
|
|
inline bool env_set(
|
|
|
|
ConstCharRange name, ConstCharRange value, bool update = true
|
|
|
|
) {
|
2016-03-13 01:28:39 +00:00
|
|
|
char sbuf[2048];
|
|
|
|
char *buf = sbuf;
|
|
|
|
bool alloc = (name.size() + value.size() + 2) > sizeof(sbuf);
|
2016-07-31 19:40:25 +00:00
|
|
|
if (alloc) {
|
2016-03-13 01:28:39 +00:00
|
|
|
buf = new char[name.size() + value.size() + 2];
|
2016-07-31 19:40:25 +00:00
|
|
|
}
|
2016-03-13 01:28:39 +00:00
|
|
|
memcpy(buf, name.data(), name.size());
|
|
|
|
buf[name.size()] = '\0';
|
|
|
|
memcpy(&buf[name.size() + 1], value.data(), value.size());
|
|
|
|
buf[name.size() + value.size() + 1] = '\0';
|
2016-07-06 18:40:43 +00:00
|
|
|
#ifndef OSTD_PLATFORM_WIN32
|
2016-03-13 01:28:39 +00:00
|
|
|
bool ret = !setenv(buf, &buf[name.size() + 1], update);
|
2016-07-06 18:40:43 +00:00
|
|
|
#else
|
2016-07-31 19:40:25 +00:00
|
|
|
if (!update && GetEnvironmentVariable(buf, nullptr, 0)) {
|
2016-07-06 18:40:43 +00:00
|
|
|
return true;
|
2016-07-31 19:40:25 +00:00
|
|
|
}
|
2016-07-06 18:40:43 +00:00
|
|
|
bool ret = !!SetEnvironmentVariable(buf, &buf[name.size() + 1]);
|
|
|
|
#endif
|
2016-07-31 19:40:25 +00:00
|
|
|
if (alloc) {
|
2016-03-13 01:28:39 +00:00
|
|
|
delete[] buf;
|
2016-07-31 19:40:25 +00:00
|
|
|
}
|
2016-03-13 01:28:39 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-07-08 18:48:11 +00:00
|
|
|
inline bool env_unset(ConstCharRange name) {
|
2016-03-13 01:28:39 +00:00
|
|
|
char buf[256];
|
|
|
|
if (name.size() < sizeof(buf)) {
|
|
|
|
memcpy(buf, name.data(), name.size());
|
|
|
|
buf[name.size()] = '\0';
|
2016-07-06 18:40:43 +00:00
|
|
|
#ifndef OSTD_PLATFORM_WIN32
|
2016-03-13 01:28:39 +00:00
|
|
|
return !unsetenv(buf);
|
2016-07-06 18:40:43 +00:00
|
|
|
#else
|
|
|
|
return !!SetEnvironmentVariable(buf, nullptr);
|
|
|
|
#endif
|
2016-03-13 01:28:39 +00:00
|
|
|
}
|
2016-07-06 18:40:43 +00:00
|
|
|
#ifndef OSTD_PLATFORM_WIN32
|
2016-03-13 01:28:39 +00:00
|
|
|
return !unsetenv(String(name).data());
|
2016-07-06 18:40:43 +00:00
|
|
|
#else
|
2016-07-31 19:40:25 +00:00
|
|
|
return !!SetEnvironmentVariable(String(name).data(), nullptr);
|
2016-07-06 18:40:43 +00:00
|
|
|
#endif
|
2016-03-13 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace ostd */
|
|
|
|
|
|
|
|
#endif
|