aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/util.cc
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2019-12-04 00:31:09 +0100
committerEelco Dolstra <edolstra@gmail.com>2019-12-04 00:31:09 +0100
commitc3c23a52ee1c5844343bc5ed075791ec7fec6413 (patch)
treebaf65223d87d2a5a5030f88208ceebab790624da /src/libutil/util.cc
parente721f99817bb7154d8098c902e25f84521a90b7f (diff)
parentc1d18050b4cfed9eba68d4d21b397c6cce035e37 (diff)
Merge remote-tracking branch 'origin/master' into flakes
Diffstat (limited to 'src/libutil/util.cc')
-rw-r--r--src/libutil/util.cc37
1 files changed, 16 insertions, 21 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 998037337..1e5e4851e 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -59,10 +59,11 @@ std::string SysError::addErrno(const std::string & s)
}
-string getEnv(const string & key, const string & def)
+std::optional<std::string> getEnv(const std::string & key)
{
char * value = getenv(key.c_str());
- return value ? string(value) : def;
+ if (!value) return {};
+ return std::string(value);
}
@@ -438,7 +439,7 @@ void deletePath(const Path & path, unsigned long long & bytesFreed)
static Path tempName(Path tmpRoot, const Path & prefix, bool includePid,
int & counter)
{
- tmpRoot = canonPath(tmpRoot.empty() ? getEnv("TMPDIR", "/tmp") : tmpRoot, true);
+ tmpRoot = canonPath(tmpRoot.empty() ? getEnv("TMPDIR").value_or("/tmp") : tmpRoot, true);
if (includePid)
return (format("%1%/%2%-%3%-%4%") % tmpRoot % prefix % getpid() % counter++).str();
else
@@ -479,7 +480,7 @@ Path createTempDir(const Path & tmpRoot, const Path & prefix,
std::pair<AutoCloseFD, Path> createTempFile(const Path & prefix)
{
- Path tmpl(getEnv("TMPDIR", "/tmp") + "/" + prefix + ".XXXXXX");
+ Path tmpl(getEnv("TMPDIR").value_or("/tmp") + "/" + prefix + ".XXXXXX");
// Strictly speaking, this is UB, but who cares...
AutoCloseFD fd(mkstemp((char *) tmpl.c_str()));
if (!fd)
@@ -491,7 +492,7 @@ std::pair<AutoCloseFD, Path> createTempFile(const Path & prefix)
std::string getUserName()
{
auto pw = getpwuid(geteuid());
- std::string name = pw ? pw->pw_name : getEnv("USER", "");
+ std::string name = pw ? pw->pw_name : getEnv("USER").value_or("");
if (name.empty())
throw Error("cannot figure out user name");
return name;
@@ -499,8 +500,8 @@ std::string getUserName()
static Lazy<Path> getHome2([]() {
- Path homeDir = getEnv("HOME");
- if (homeDir.empty()) {
+ auto homeDir = getEnv("HOME");
+ if (!homeDir) {
std::vector<char> buf(16384);
struct passwd pwbuf;
struct passwd * pw;
@@ -509,7 +510,7 @@ static Lazy<Path> getHome2([]() {
throw Error("cannot determine user's home directory");
homeDir = pw->pw_dir;
}
- return homeDir;
+ return *homeDir;
});
Path getHome() { return getHome2(); }
@@ -517,25 +518,21 @@ Path getHome() { return getHome2(); }
Path getCacheDir()
{
- Path cacheDir = getEnv("XDG_CACHE_HOME");
- if (cacheDir.empty())
- cacheDir = getHome() + "/.cache";
- return cacheDir;
+ auto cacheDir = getEnv("XDG_CACHE_HOME");
+ return cacheDir ? *cacheDir : getHome() + "/.cache";
}
Path getConfigDir()
{
- Path configDir = getEnv("XDG_CONFIG_HOME");
- if (configDir.empty())
- configDir = getHome() + "/.config";
- return configDir;
+ auto configDir = getEnv("XDG_CONFIG_HOME");
+ return configDir ? *configDir : getHome() + "/.config";
}
std::vector<Path> getConfigDirs()
{
Path configHome = getConfigDir();
- string configDirs = getEnv("XDG_CONFIG_DIRS");
+ string configDirs = getEnv("XDG_CONFIG_DIRS").value_or("");
std::vector<Path> result = tokenizeString<std::vector<string>>(configDirs, ":");
result.insert(result.begin(), configHome);
return result;
@@ -544,10 +541,8 @@ std::vector<Path> getConfigDirs()
Path getDataDir()
{
- Path dataDir = getEnv("XDG_DATA_HOME");
- if (dataDir.empty())
- dataDir = getHome() + "/.local/share";
- return dataDir;
+ auto dataDir = getEnv("XDG_DATA_HOME");
+ return dataDir ? *dataDir : getHome() + "/.local/share";
}