Last active
August 29, 2015 14:01
-
-
Save ddeville/90f341f6ed7c592900ab to your computer and use it in GitHub Desktop.
Get the actual home directory when running in the OS X sandbox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <unistd.h> | |
#import <pwd.h> | |
NSURL *_LLActualHomeDirectoryLocation(void) { | |
static NSURL *homeDirectoryLocation = nil; | |
static dispatch_once_t homeDirectoryLocationPredicate = 0; | |
dispatch_once(&homeDirectoryLocationPredicate, ^ { | |
uid_t uid = getuid(); | |
long bufferSize = sysconf(_SC_GETPW_R_SIZE_MAX); | |
NSCParameterAssert(bufferSize != -1); | |
char buffer[bufferSize]; | |
struct passwd pwd = {}; | |
struct passwd *result = NULL; | |
int getpwError = getpwuid_r(uid, &pwd, buffer, bufferSize, &result); | |
NSCParameterAssert(getpwError == 0 && result != NULL); | |
const char *homeDirectoryPath = pwd.pw_dir; | |
homeDirectoryLocation = [NSURL fileURLWithPath:@(homeDirectoryPath)]; | |
}); | |
return homeDirectoryLocation; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment