NSSystemDirectories.c [plain text]
#import <libc.h>
#import <stdio.h>
#import <stdlib.h>
#import <NSSystemDirectories.h>
#define numDirs 14
static const struct {
unsigned char invalidDomainMask; unsigned char alternateDomainMask; const char *dirPath;
} dirInfo[numDirs] = {
{0, 0, "Applications"},
{0, 0, "Applications/Demos"},
{0, 0, "Developer/Applications"},
{0, 0, "Applications/Utilities"},
{0, 0x8, "Library"}, {0, 0, "Developer"},
{0x9, 0, "Users"}, {0, 0x8, "Library/Documentation"}, {0xe, 0, "Documents"}, {0x7, 0, "Library/CoreServices"}, {0xe, 0, "Documents/Autosaved"}, {0xe, 0, "Desktop"}, {0, 0, "Library/Caches"},
{0, 0, "Library/Application Support"}
};
enum {
NSAutosavedDocumentsDirectory = 11
};
#define numApplicationDirs 4
static const char applicationDirs[numApplicationDirs] = {1, 4, 3, 2};
#define numLibraryDirs 2
static const char libraryDirs[numLibraryDirs] = {5, 6};
#define numDomains 4
static const struct {
char needsRootPrepended;
const char *domainPath;
const char *alternateDomainPath;
} domainInfo[numDomains] = {
{0, "~", "~"},
{1, "", ""},
{1, "/Network", "/Network"},
{1, "", "/System"}
};
#define invalidDomains 0x00 // some domains may be invalid on non-Mach systems
NSSearchPathEnumerationState NSStartSearchPathEnumeration(NSSearchPathDirectory dir, NSSearchPathDomainMask domainMask) {
domainMask = domainMask & ((1 << numDomains) - 1) & ~invalidDomains; if (dir != NSAllLibrariesDirectory && dir != NSLibraryDirectory && dir != NSUserDirectory && dir != NSDocumentationDirectory && (domainMask & NSLocalDomainMask) && (domainMask & NSSystemDomainMask)) domainMask = domainMask & ~NSSystemDomainMask; return (((unsigned int)dir) << 24) + ((unsigned int)domainMask);
}
NSSearchPathEnumerationState NSGetNextSearchPathEnumeration(NSSearchPathEnumerationState state, char *path) {
static const char *nextRoot = NULL;
unsigned dir = (state >> 24) & 0xff;
unsigned dirState = (state >> 16) & 0xff;
unsigned domainMask = state & 0xffff;
unsigned int curDomain; unsigned int curDir = 0;
do {
if (domainMask == 0) return 0; for (curDomain = 0; curDomain < numDomains; curDomain++) if ((domainMask & (1 << curDomain))) break;
if (dir < NSAllApplicationsDirectory) { curDir = dir;
} else { if (dir == NSAllApplicationsDirectory) {
curDir = applicationDirs[dirState];
if (++dirState == numApplicationDirs) dirState = 0;
} else if (dir == NSAllLibrariesDirectory) {
curDir = libraryDirs[dirState];
if (++dirState == numLibraryDirs) dirState = 0;
}
}
if (dirState == 0) domainMask &= ~(1 << curDomain); } while ((dirInfo[curDir - 1].invalidDomainMask & (1 << curDomain)));
if (domainInfo[curDomain].needsRootPrepended && nextRoot == 0) {
nextRoot = getenv("NEXT_ROOT");
if (nextRoot == NULL) {
nextRoot = "";
} else {
strcpy(malloc(strlen(nextRoot) + 1), nextRoot); }
}
snprintf(path, PATH_MAX, "%s%s/%s", domainInfo[curDomain].needsRootPrepended ? nextRoot : "", (dirInfo[curDir - 1].alternateDomainMask & (1 << curDomain)) ? domainInfo[curDomain].alternateDomainPath : domainInfo[curDomain].domainPath, dirInfo[curDir - 1].dirPath);
return (dir << 24) + (dirState << 16) + domainMask;
}