CF_EXPORT CFURLRef CFCopyHomeDirectoryURLForUser(CFStringRef uName) { #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_FREEBSD if (!uName) { uid_t euid; __CFGetUGIDs(&euid, NULL); struct passwd *upwd = getpwuid(euid ? euid : getuid()); return _CFCopyHomeDirURLForUser(upwd, true); } else { struct passwd *upwd = NULL; char buf[128], *user; SInt32 len = CFStringGetLength(uName), size = CFStringGetMaximumSizeForEncoding(len, kCFPlatformInterfaceStringEncoding); CFIndex usedSize; if (size < 127) { user = buf; } else { user = CFAllocatorAllocate(kCFAllocatorSystemDefault, size+1, 0); } if (CFStringGetBytes(uName, CFRangeMake(0, len), kCFPlatformInterfaceStringEncoding, 0, true, (uint8_t *)user, size, &usedSize) == len) { user[usedSize] = '\0'; upwd = getpwnam(user); } if (buf != user) { CFAllocatorDeallocate(kCFAllocatorSystemDefault, user); } return _CFCopyHomeDirURLForUser(upwd, false); } #elif DEPLOYMENT_TARGET_WINDOWS // This code can only get the directory for the current user CFStringRef userName = uName ? CFCopyUserName() : NULL; if (uName && !CFEqual(uName, userName)) { CFLog(kCFLogLevelError, CFSTR("CFCopyHomeDirectoryURLForUser(): Unable to get home directory for other user")); if (userName) CFRelease(userName); return NULL; } if (userName) CFRelease(userName); return CFCopyHomeDirectoryURL(); #else #error Dont know how to compute users home directories on this platform #endif }
/* We spend a lot of time constructing these prefixes; we should cache. REW, 7/19/99 */ static CFStringRef _CFPreferencesCachePrefixForUserHost(CFStringRef userName, CFStringRef hostName) { if (userName == kCFPreferencesAnyUser && hostName == kCFPreferencesAnyHost) { return (CFStringRef)CFRetain(CFSTR("*/*/")); } CFMutableStringRef result = CFStringCreateMutable(__CFPreferencesAllocator(), 0); if (userName == kCFPreferencesCurrentUser) { userName = CFCopyUserName(); CFStringAppend(result, userName); CFRelease(userName); CFStringAppend(result, CFSTR("/")); } else if (userName == kCFPreferencesAnyUser) { CFStringAppend(result, CFSTR("*/")); } if (hostName == kCFPreferencesCurrentHost) { CFStringRef hostID = _CFPreferencesGetByHostIdentifierString(); CFStringAppend(result, hostID); CFStringAppend(result, CFSTR("/")); } else if (hostName == kCFPreferencesAnyHost) { CFStringAppend(result, CFSTR("*/")); } return result; }
/* These are sanitized versions of the above functions. We might want to eliminate the above ones someday. These can return NULL. */ CF_EXPORT CFStringRef CFGetUserName(void) { return CFCopyUserName(); }