Skip to content

Commit fb7b4d9

Browse files
committed
Use std::filesystem to get current working dir
This doesn't work on GCC 7... TODO: Figure out whether we can safely bump the requirements to GCC 8. Signed-off-by: aszlig <aszlig@nix.build>
1 parent 0ff7fad commit fb7b4d9

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

src/blackhole.cc

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: LGPL-3.0-only
22
#include <cstring>
33
#include <climits>
4+
#include <filesystem>
45
#include <unistd.h>
56

67
#include <sys/stat.h>
@@ -60,15 +61,10 @@ static std::string get_tmpdir(void)
6061
if (is_writable_dir("/var/tmp"))
6162
return "/var/tmp";
6263

63-
int old_errno = errno;
64-
char *workdir = get_current_dir_name();
65-
errno = old_errno;
66-
if (workdir != nullptr) {
67-
std::string wdir_str(workdir);
68-
free(workdir);
69-
if (is_writable_dir(workdir))
70-
return workdir;
71-
}
64+
std::string workdir = std::filesystem::current_path();
65+
66+
if (is_writable_dir(workdir))
67+
return workdir;
7268

7369
LOG(FATAL) << "Unable to get temporary directory.";
7470
std::abort();

src/rules/parse.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: LGPL-3.0-only
22
#include <algorithm>
33
#include <iostream>
4+
#include <filesystem>
45
#include <fstream>
56
#include <memory>
67
#include <sstream>
@@ -322,7 +323,7 @@ std::string make_absolute(const std::string &path)
322323
if (path.empty() || path[0] == '/')
323324
return path;
324325

325-
return std::string(get_current_dir_name()) + '/' + path;
326+
return std::filesystem::current_path() / path;
326327
}
327328

328329
std::optional<Rule> parse_rule_arg(size_t rulepos, const std::string &arg)

0 commit comments

Comments
 (0)