Skip to content

Instantly share code, notes, and snippets.

@ddeville
Last active August 29, 2015 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddeville/90f341f6ed7c592900ab to your computer and use it in GitHub Desktop.
Save ddeville/90f341f6ed7c592900ab to your computer and use it in GitHub Desktop.
Get the actual home directory when running in the OS X sandbox
#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