aboutsummaryrefslogtreecommitdiff
path: root/src/util/fs.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/fs.cpp')
-rw-r--r--src/util/fs.cpp29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/util/fs.cpp b/src/util/fs.cpp
new file mode 100644
index 0000000..68c3dd9
--- /dev/null
+++ b/src/util/fs.cpp
@@ -0,0 +1,29 @@
+#include "fs.hpp"
+
+#include <cstdlib>
+#include <pwd.h>
+#include <unistd.h>
+
+auto get_current_user_home_path() noexcept -> std::filesystem::path
+{
+ const auto *home_path_env = std::getenv("HOME");
+
+ if (home_path_env == nullptr)
+ {
+ return getpwuid(getuid())->pw_dir;
+ }
+
+ return home_path_env;
+}
+
+auto expand_path_home(const std::filesystem::path &path) noexcept -> std::filesystem::path
+{
+ const auto path_str = path.string();
+
+ if (!path_str.starts_with("~/"))
+ {
+ return path;
+ }
+
+ return get_current_user_home_path() / std::filesystem::path(path_str.substr(2));
+}