diff options
author | Andrei Karas <akaras@inbox.ru> | 2017-02-27 19:49:39 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2017-02-27 20:00:41 +0300 |
commit | bf41330551a76ea426ace38dbc57c333ef5112cc (patch) | |
tree | ea543733e0ff5b664923ec90053b2c81f2d3def3 | |
parent | 36c6674db35445d6c72f0f520b4abf9ef15b4da9 (diff) | |
download | mv-bf41330551a76ea426ace38dbc57c333ef5112cc.tar.gz mv-bf41330551a76ea426ace38dbc57c333ef5112cc.tar.bz2 mv-bf41330551a76ea426ace38dbc57c333ef5112cc.tar.xz mv-bf41330551a76ea426ace38dbc57c333ef5112cc.zip |
Add function for get home path.
-rw-r--r-- | src/fs/paths.cpp | 32 | ||||
-rw-r--r-- | src/fs/paths.h | 2 | ||||
-rw-r--r-- | src/fs/virtfsphys_unittest.cc | 2 |
3 files changed, 36 insertions, 0 deletions
diff --git a/src/fs/paths.cpp b/src/fs/paths.cpp index affc12285..aa1c453b0 100644 --- a/src/fs/paths.cpp +++ b/src/fs/paths.cpp @@ -47,6 +47,11 @@ #include <limits.h> #endif // WIN32 +#ifndef WIN32 +#include <sys/types.h> +#include <pwd.h> +#endif // WIN32 + #ifdef ANDROID #ifdef USE_SDL2 #include <SDL_system.h> @@ -203,6 +208,33 @@ std::string getPicturesDir() #endif // WIN32 } +std::string getHomePath() +{ +#ifdef WIN32 + return getSpecialFolderLocation(CSIDL_LOCAL_APPDATA); +#else + const char *path = getenv("HOME"); + if (path == nullptr) + { + uid_t uid = getuid(); + struct passwd *pw; + + pw = getpwuid(uid); + if (pw != NULL && + pw->pw_dir != nullptr) + { + path = pw->pw_dir; + } + if (path == nullptr) + return "/"; + } + std::string dir = path; + if (findLast(dir, "/") == false) + dir += "/"; + return dir; +#endif // WIN32 +} + #ifdef ANDROID std::string getSdStoragePath() { diff --git a/src/fs/paths.h b/src/fs/paths.h index fc90730f3..48a6eeea8 100644 --- a/src/fs/paths.h +++ b/src/fs/paths.h @@ -47,4 +47,6 @@ std::string getPackageDir() A_WARN_UNUSED; void setPackageDir(const std::string &dir); +std::string getHomePath(); + #endif // UTILS_PATHS_H diff --git a/src/fs/virtfsphys_unittest.cc b/src/fs/virtfsphys_unittest.cc index 93f86db64..79acaa434 100644 --- a/src/fs/virtfsphys_unittest.cc +++ b/src/fs/virtfsphys_unittest.cc @@ -20,6 +20,7 @@ #include "catch.hpp" +#include "fs/paths.h" #include "fs/virtfs.h" #include "fs/virtfsphys.h" #include "fs/virtfstools.h" @@ -52,6 +53,7 @@ TEST_CASE("VirtFsPhys getUserDir") { VirtFsPhys::initFuncs(); REQUIRE(VirtFsPhys::getUserDir() != nullptr); + REQUIRE(VirtFsPhys::getUserDir() == getHomePath()); } TEST_CASE("VirtFsPhys exists") |