Eliminated BSPF::getenv(), as only the UNIX version is used.

I spent a lot of time trying to eliminate warnings in the Windows version of this
function, only to realize it's only ever used in POSIX-specific code!
This commit is contained in:
Stephen Anthony 2022-04-08 19:51:15 -02:30
parent 071e8f7eb1
commit 3b9184ec5f
4 changed files with 9 additions and 31 deletions

View File

@ -39,8 +39,8 @@ FilesystemNodeZIP::FilesystemNodeZIP(const string& p)
if (_zipFile[0] == '~') if (_zipFile[0] == '~')
{ {
#if defined(BSPF_UNIX) || defined(BSPF_MACOS) #if defined(BSPF_UNIX) || defined(BSPF_MACOS)
const string& home = BSPF::getenv("HOME"); const char* home = std::getenv("HOME");
if (home != EmptyString) if (home != nullptr)
_zipFile.replace(0, 1, home); _zipFile.replace(0, 1, home);
#elif defined(BSPF_WINDOWS) #elif defined(BSPF_WINDOWS)
_zipFile.replace(0, 1, myHomeFinder.getHomePath()); _zipFile.replace(0, 1, myHomeFinder.getHomePath());

View File

@ -355,30 +355,6 @@ namespace BSPF
return tm_snapshot; return tm_snapshot;
} }
// Coverity complains if 'getenv' is used unrestricted
inline string getenv(const string& env_var)
{
#if (defined BSPF_WINDOWS || defined __WIN32__) && !defined __GNUG__
char* buf = nullptr;
size_t sz = 0;
if(_dupenv_s(&buf, &sz, env_var.c_str()) == 0 && buf != nullptr)
{
string val(buf);
free(buf);
return val;
}
return EmptyString;
#else
try {
const char* val = std::getenv(env_var.c_str());
return val ? string(val) : EmptyString;
}
catch(...) {
return EmptyString;
}
#endif
}
inline bool isWhiteSpace(const char s) inline bool isWhiteSpace(const char s)
{ {
const string WHITESPACES = " ,.;:+-*&/\\'"; const string WHITESPACES = " ,.;:+-*&/\\'";

View File

@ -37,8 +37,8 @@ FilesystemNodePOSIX::FilesystemNodePOSIX(const string& path, bool verify)
// Expand '~' to the HOME environment variable // Expand '~' to the HOME environment variable
if(_path[0] == '~') if(_path[0] == '~')
{ {
const string& home = BSPF::getenv("HOME"); const char* home = std::getenv("HOME");
if(home != EmptyString) if (home != nullptr)
_path.replace(0, 1, home); _path.replace(0, 1, home);
} }
// Get absolute path (only used for relative directories) // Get absolute path (only used for relative directories)
@ -78,7 +78,9 @@ void FilesystemNodePOSIX::setFlags()
string FilesystemNodePOSIX::getShortPath() const string FilesystemNodePOSIX::getShortPath() const
{ {
// If the path starts with the home directory, replace it with '~' // If the path starts with the home directory, replace it with '~'
const string& home = BSPF::getenv("HOME"); const char* env_home = std::getenv("HOME");
const string& home = env_home != nullptr ? env_home : EmptyString;
if(home != EmptyString && BSPF::startsWithIgnoreCase(_path, home)) if(home != EmptyString && BSPF::startsWithIgnoreCase(_path, home))
{ {
string path = "~"; string path = "~";

View File

@ -26,8 +26,8 @@ void OSystemUNIX::getBaseDirectories(string& basedir, string& homedir,
bool useappdir, const string& usedir) bool useappdir, const string& usedir)
{ {
// Use XDG_CONFIG_HOME if defined, otherwise use the default // Use XDG_CONFIG_HOME if defined, otherwise use the default
string configDir = BSPF::getenv("XDG_CONFIG_HOME"); const char* cfg_home = std::getenv("XDG_CONFIG_HOME");
if(configDir == EmptyString) configDir = "~/.config"; const string& configDir = cfg_home != nullptr ? cfg_home : "~/.config";
basedir = configDir + "/stella"; basedir = configDir + "/stella";
homedir = "~/"; homedir = "~/";