reimplement finding personal & profile directories on unix (not yet used)

This commit is contained in:
walker0643 2018-03-29 15:54:41 -04:00
parent c5e70af09a
commit 96627f4f07
3 changed files with 84 additions and 2 deletions

View File

@ -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()

View File

@ -0,0 +1,76 @@
#include "../DataDirectories.h"
#include <unistd.h> // sysconf
#include <sys/types.h> // getpwuid(_r)
#include <pwd.h> // 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;
}

View File

@ -1,4 +1,4 @@
#include "DataDirectories.h"
#include "../DataDirectories.h"
#include <Shlobj.h>
@ -51,4 +51,5 @@ const std::string& DataDirectories::profile(const std::string& path)
{
_profile = path;
return _profile;
}
}