Fallback to getpwuid in the unlikely case $HOME is underfined

Add a homedir() helper function, and document the $kak_config
env var.
This commit is contained in:
Maxime Coste 2018-01-20 11:19:23 +11:00
parent e7cbf38af7
commit 07dfcd336d
4 changed files with 25 additions and 12 deletions

View File

@ -80,7 +80,10 @@ informations about Kakoune's state:
history id of the current buffer, the history id is an integer value
which is used to reference a specific buffer version in the undo tree
*kak_runtime*::
directory containing the kak binary
directory containing the kak support files, determined from kakoune's
binary location.
*kak_config*::
directory containing the user configuration
*kak_count*::
count parameter passed to the command
*kak_opt_<name>*::

View File

@ -10,13 +10,14 @@
#include "unicode.hh"
#include <cerrno>
#include <cstring>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <cstdlib>
#include <cstring>
#include <dirent.h>
#include <fcntl.h>
#include <pwd.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <unistd.h>
#if defined(__FreeBSD__)
#include <sys/sysctl.h>
@ -51,7 +52,7 @@ String parse_filename(StringView filename)
{
auto prefix = filename.substr(0_byte, 2_byte);
if (prefix == "~" or prefix == "~/")
return getenv("HOME") + filename.substr(1_byte);
return homedir() + filename.substr(1_byte);
return filename.str();
}
@ -105,10 +106,10 @@ String compact_path(StringView filename)
if (prefix_match(real_filename, real_cwd))
return real_filename.substr(real_cwd.length()).str();
const char* home = getenv("HOME");
if (home)
const StringView home = homedir();
if (not home.empty())
{
ByteCount home_len = (int)strlen(home);
ByteCount home_len = home.length();
if (real_filename.substr(0, home_len) == home)
return "~" + real_filename.substr(home_len);
}
@ -125,6 +126,14 @@ StringView tmpdir()
return "/tmp";
}
StringView homedir()
{
StringView home = getenv("HOME");
if (home.empty())
return getpwuid(geteuid())->pw_dir;
return home;
}
bool fd_readable(int fd)
{
fd_set rfds;
@ -331,7 +340,7 @@ String find_file(StringView filename, ConstArrayView<String> paths)
}
if (filename.substr(0_byte, 2_byte) == "~/")
{
String candidate = getenv("HOME") + filename.substr(1_byte);
String candidate = homedir() + filename.substr(1_byte);
if (stat(candidate.c_str(), &buf) == 0 and S_ISREG(buf.st_mode))
return candidate;
return "";

View File

@ -25,6 +25,7 @@ String real_path(StringView filename);
String compact_path(StringView filename);
StringView tmpdir();
StringView homedir();
// returns pair { directory, filename }
std::pair<StringView, StringView> split_path(StringView path);

View File

@ -72,7 +72,7 @@ String config_directory()
{
StringView config_home = getenv("XDG_CONFIG_HOME");
if (config_home.empty())
return format("{}/.config/kak", getenv("HOME"));
return format("{}/.config/kak", homedir());
return format("{}/kak", config_home);
}