aboutsummaryrefslogtreecommitdiff
path: root/src/util/fs.cpp
blob: 87e3cc0867d00adf932edcc0491fc38cc836480d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "fs.hpp"

#include <pwd.h>
#include <unistd.h>
#include <cstdlib>
#include <string>

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));
}