diff --git a/src/lib/common/CMakeLists.txt b/src/lib/common/CMakeLists.txt index eda7fa1a..b3791c1c 100644 --- a/src/lib/common/CMakeLists.txt +++ b/src/lib/common/CMakeLists.txt @@ -33,3 +33,8 @@ if (BARRIER_ADD_HEADERS) endif() add_library(common STATIC ${sources}) + +if (HAVE_GETPWUID_R) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DHAVE_GETPWUID_R=1") +endif() + diff --git a/src/lib/common/unix/DataDirectories.cpp b/src/lib/common/unix/DataDirectories.cpp new file mode 100644 index 00000000..95aa08e1 --- /dev/null +++ b/src/lib/common/unix/DataDirectories.cpp @@ -0,0 +1,76 @@ +#include "../DataDirectories.h" + +#include // sysconf +#include // getpwuid(_r) +#include // getpwuid(_r) + +#ifdef WINAPI_XWINDOWS +const std::string ProfileSubdir = "/.barrier"; +#else // macos +const std::string ProfileSubdir = "/Library/Application Support/Barrier"; +#endif + +// static members +std::string DataDirectories::_personal; +std::string DataDirectories::_profile; + +static std::string pw_dir(struct passwd* pwentp) +{ + if (pwentp != NULL && pwentp->pw_dir != NULL) + return pwentp->pw_dir; + return ""; +} + +#ifdef HAVE_GETPWUID_R + +static std::string unix_home() +{ + long size = -1; +#if defined(_SC_GETPW_R_SIZE_MAX) + size = sysconf(_SC_GETPW_R_SIZE_MAX); +#endif + if (size == -1) + size = BUFSIZ; + + struct passwd pwent; + struct passwd* pwentp; + std::string buffer(size, 0); + getpwuid_r(getuid(), &pwent, &buffer[0], size, &pwentp); + return pw_dir(pwentp); +} + +#else // not HAVE_GETPWUID_R + +static std::string unix_home() +{ + return pw_dir(getpwuid(getuid())); +} + +#endif // HAVE_GETPWUID_R + +const std::string& DataDirectories::personal() +{ + if (_personal.empty()) + _personal = unix_home(); + return _personal; +} + +const std::string& DataDirectories::personal(const std::string& path) +{ + _personal = path; + return _personal; +} + +const std::string& DataDirectories::profile() +{ + if (_profile.empty()) + _profile = personal() + ProfileSubdir; + return _profile; +} + +const std::string& DataDirectories::profile(const std::string& path) +{ + _profile = path; + return _profile; +} + diff --git a/src/lib/common/win32/DataDirectories.cpp b/src/lib/common/win32/DataDirectories.cpp index 832fd821..02e216f3 100644 --- a/src/lib/common/win32/DataDirectories.cpp +++ b/src/lib/common/win32/DataDirectories.cpp @@ -1,4 +1,4 @@ -#include "DataDirectories.h" +#include "../DataDirectories.h" #include @@ -51,4 +51,5 @@ const std::string& DataDirectories::profile(const std::string& path) { _profile = path; return _profile; -} \ No newline at end of file +} +