aboutsummaryrefslogtreecommitdiff
path: root/src/util/fs.cpp
blob: 68c3dd9393507edb25d2f7272450f378296ed424 (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
#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));
}