mirror of
https://github.com/ksherlock/mpw-shell.git
synced 2025-01-02 11:30:04 +00:00
look for ~/mpw, /usr/share/mpw, /usr/local/mpw
This commit is contained in:
parent
ed96470e18
commit
474b10ccaa
@ -41,32 +41,50 @@ namespace fs = filesystem;
|
|||||||
|
|
||||||
bool utf8 = false;
|
bool utf8 = false;
|
||||||
|
|
||||||
|
fs::path home() {
|
||||||
|
|
||||||
|
const char *cp = getenv("HOME");
|
||||||
|
if (cp && cp) {
|
||||||
|
auto pw = getpwuid(getuid());
|
||||||
|
if (pw) return fs::path(pw->pw_dir);
|
||||||
|
|
||||||
|
}
|
||||||
|
return fs::path();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
fs::path root() {
|
fs::path root() {
|
||||||
|
|
||||||
static fs::path root;
|
static fs::path root;
|
||||||
|
bool init = false;
|
||||||
|
|
||||||
if (root.empty()) {
|
static std::array<filesystem::path, 2> locations = { {
|
||||||
const char *cp = getenv("HOME");
|
"/usr/share/mpw/",
|
||||||
if (!cp || !*cp) {
|
"/usr/local/share/mpw/"
|
||||||
auto pw = getpwuid(getuid());
|
} };
|
||||||
if (!pw) {
|
|
||||||
fprintf(stderr,"### Unable to determine home directory\n.");
|
if (!init) {
|
||||||
exit(EX_NOUSER);
|
init = true;
|
||||||
|
std::error_code ec;
|
||||||
|
fs::path p;
|
||||||
|
|
||||||
|
p = home();
|
||||||
|
if (!p.empty()) {
|
||||||
|
p /= "mpw/";
|
||||||
|
if (fs::is_directory(p, ec)) {
|
||||||
|
root = std::move(p);
|
||||||
|
return root;
|
||||||
}
|
}
|
||||||
cp = pw->pw_dir;
|
|
||||||
}
|
}
|
||||||
root = cp;
|
for (fs::path p : locations) {
|
||||||
root /= "mpw/";
|
p /= "mpw/";
|
||||||
fs::error_code ec;
|
if (fs::is_directory(p, ec)) {
|
||||||
fs::file_status st = status(root, ec);
|
root = std::move(p);
|
||||||
if (!fs::exists(st)) {
|
return root;
|
||||||
fprintf(stderr, "### Warning: %s does not exist.\n", root.c_str());
|
}
|
||||||
}
|
|
||||||
|
|
||||||
else if (!fs::is_directory(st)) {
|
|
||||||
fprintf(stderr, "### Warning: %s is not a directory.\n", root.c_str());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fprintf(stderr, "### Warning: Unable to find mpw directory.\n");
|
||||||
}
|
}
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user