/* XMRig * Copyright 2010 Jeff Garzik * Copyright 2012-2014 pooler * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2018-2019 SChernykh * Copyright 2016-2019 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "base/kernel/Env.h" #include #include #ifndef UV_MAXHOSTNAMESIZE # ifdef MAXHOSTNAMELEN # define UV_MAXHOSTNAMESIZE (MAXHOSTNAMELEN + 1) # else # define UV_MAXHOSTNAMESIZE 256 # endif #endif xmrig::String xmrig::Env::expand(const char *in) { if (in == nullptr) { return {}; } std::string text(in); if (text.size() < 4) { return text.c_str(); } static const std::regex env_re{R"--(\$\{([^}]+)\})--"}; std::map vars; for (std::sregex_iterator i = std::sregex_iterator(text.begin(), text.end(), env_re); i != std::sregex_iterator(); ++i) { std::smatch m = *i; const auto var = m.str(); if (vars.count(var)) { continue; } vars.insert({ var, get(m[1].str().c_str()) }); } for (const auto &kv : vars) { if (kv.second.isNull()) { continue; } size_t pos = 0; while ((pos = text.find(kv.first, pos)) != std::string::npos) { text.replace(pos, kv.first.size(), kv.second); pos += kv.second.size(); } } return text.c_str(); } xmrig::String xmrig::Env::get(const char *name) { return static_cast(getenv(name)); } xmrig::String xmrig::Env::hostname() { char buf[UV_MAXHOSTNAMESIZE]{}; size_t size = sizeof(buf); # if UV_VERSION_HEX >= 0x010c00 if (uv_os_gethostname(buf, &size) == 0) { return static_cast(buf); } # else if (gethostname(buf, size) == 0) { return static_cast(buf); } # endif return {}; }