Browse Source

sys: implement lol::sys::getenv() for safety.

legacy
Sam Hocevar 5 years ago
parent
commit
e05e9ff143
3 changed files with 21 additions and 4 deletions
  1. +1
    -2
      src/base/features.cpp
  2. +1
    -2
      src/base/log.cpp
  3. +19
    -0
      src/lol/sys/init.h

+ 1
- 2
src/base/features.cpp View File

@@ -17,8 +17,7 @@ namespace lol


bool has_threads() bool has_threads()
{ {
static char const *var = getenv("LOL_NOTHREADS");
static bool const disable_threads = var && var[0];
static bool const disable_threads = sys::getenv("LOL_NOTHREADS").size() > 0;
#if defined __EMSCRIPTEN__ && !defined __EMSCRIPTEN_PTHREADS__ #if defined __EMSCRIPTEN__ && !defined __EMSCRIPTEN_PTHREADS__
// For some reason hardware_concurrency() will return the actual number // For some reason hardware_concurrency() will return the actual number
// of threads/cores even though the system cannot spawn threads. // of threads/cores even though the system cannot spawn threads.


+ 1
- 2
src/base/log.cpp View File

@@ -80,8 +80,7 @@ void msg::helper(message_type type, char const *fmt, va_list ap)
#if !defined LOL_BUILD_DEBUG && !_DEBUG #if !defined LOL_BUILD_DEBUG && !_DEBUG
if (type == message_type::debug) if (type == message_type::debug)
{ {
static char const *var = getenv("LOL_DEBUG");
static bool const disable_debug = !var || !var[0];
static bool const disable_debug = sys::getenv("LOL_DEBUG").size() > 0;
if (disable_debug) if (disable_debug)
return; return;
} }


+ 19
- 0
src/lol/sys/init.h View File

@@ -19,6 +19,7 @@


#include <string> #include <string>
#include <cstdint> #include <cstdint>
#include <cstdlib>


namespace lol namespace lol
{ {
@@ -56,6 +57,24 @@ extern void init(int argc, char *argv[],
extern void add_data_dir(std::string const &dir); extern void add_data_dir(std::string const &dir);
extern array<std::string> get_path_list(std::string const &file); extern array<std::string> get_path_list(std::string const &file);


static inline std::string getenv(std::string const &var)
{
#if _MSC_VER
char *buf = nullptr;
size_t count = 0;
if (_dupenv_s(&buf, &count, var.c_str()) == 0 && buf)
{
std::string ret(buf);
free(buf);
return ret;
}
#else
if (auto var = std::getenv(var.c_str()))
return std::string(var);
#endif
return std::string();
}

} /* namespace sys */ } /* namespace sys */


} /* namespace lol */ } /* namespace lol */


Loading…
Cancel
Save