CFSystemDirectories.c [plain text]
#include <CoreFoundation/CFPriv.h>
#include "CFInternal.h"
#if defined(__MACH__)
#include <libc.h>
#include <stdio.h>
#include <stdlib.h>
#include <NSSystemDirectories.h>
CFSearchPathEnumerationState __CFStartSearchPathEnumeration(CFSearchPathDirectory dir, CFSearchPathDomainMask domainMask) {
return NSStartSearchPathEnumeration(dir, domainMask);
}
CFSearchPathEnumerationState __CFGetNextSearchPathEnumeration(CFSearchPathEnumerationState state, uint8_t *path, CFIndex pathSize) {
CFSearchPathEnumerationState result;
if (pathSize < PATH_MAX) {
uint8_t tempPath[PATH_MAX];
result = NSGetNextSearchPathEnumeration(state, tempPath);
strlcpy(path, tempPath, pathSize);
} else {
result = NSGetNextSearchPathEnumeration(state, path);
}
return result;
}
#endif
#if defined(__MACH__) || defined(__WIN32__)
CFArrayRef CFCopySearchPathForDirectoriesInDomains(CFSearchPathDirectory directory, CFSearchPathDomainMask domainMask, Boolean expandTilde) {
CFMutableArrayRef array;
CFSearchPathEnumerationState state;
CFIndex homeLen = -1;
char cPath[CFMaxPathSize], home[CFMaxPathSize];
array = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
state = __CFStartSearchPathEnumeration(directory, domainMask);
while ((state = __CFGetNextSearchPathEnumeration(state, cPath, sizeof(cPath)))) {
CFURLRef url = NULL;
if (expandTilde && (cPath[0] == '~')) {
if (homeLen < 0) {
CFURLRef homeURL = CFCopyHomeDirectoryURLForUser(NULL);
if (homeURL) {
CFURLGetFileSystemRepresentation(homeURL, true, home, CFMaxPathSize);
homeLen = strlen(home);
CFRelease(homeURL);
}
}
if (homeLen + strlen(cPath) < CFMaxPathSize) {
home[homeLen] = '\0';
strcat(home, &cPath[1]);
url = CFURLCreateFromFileSystemRepresentation(NULL, home, strlen(home), true);
}
} else {
url = CFURLCreateFromFileSystemRepresentation(NULL, cPath, strlen(cPath), true);
}
if (url) {
CFArrayAppendValue(array, url);
CFRelease(url);
}
}
return array;
}
#endif
#undef numDirs
#undef numApplicationDirs
#undef numLibraryDirs
#undef numDomains
#undef invalidDomains
#undef invalidDomains