]> git.saurik.com Git - apple/libc.git/blob - gen/NSSystemDirectories.c
fcbdb0529110746939ec33e9940b5e5f1527744c
[apple/libc.git] / gen / NSSystemDirectories.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 #import <libc.h>
25 #import <stdio.h>
26 #import <stdlib.h>
27 #import <NSSystemDirectories.h>
28
29 // Names of directories; index into this with NSSearchPathDirectory - 1
30 #define numDirs 10
31 static const struct {
32 unsigned char invalidDomainMask; // Domains in which this dir does not appear
33 unsigned char alternateDomainMask; // Domains in which this dir uses the alternate domain path
34 const char *dirPath;
35 } dirInfo[numDirs] = {
36 {0, 0, "Applications"},
37 {0, 0, "Applications/GrabBag"},
38 {0, 0, "Developer/Applications"},
39 {0, 0, "Applications/Utilities"},
40 {0, 0x8, "Library"}, // Uses alternate form in System domain
41 {0, 0, "Developer"},
42 {0x9, 0, "Users"}, // Not valid in the System and User domains
43 {0, 0x8, "Library/Documentation"}, // Uses alternate form in System domain
44 {0xe, 0, "Documents"}, // Only valid in user domain
45 {0x7, 0, "Library/CoreServices"} // Only valid in System domain
46 };
47
48 // Ordered list of where to find applications in each domain (the numbers are NSSearchPathDirectory)
49 #define numApplicationDirs 4
50 static const char applicationDirs[numApplicationDirs] = {1, 4, 3, 2};
51
52 // Ordered list of where to find resources in each domain (the numbers are NSSearchPathDirectory)
53 #define numLibraryDirs 2
54 static const char libraryDirs[numLibraryDirs] = {5, 6};
55
56 // Names of domains; index into this log2(domainMask). If the search path ordering is ever changed, then we need an indirection (as the domainMask values cannot be changed).
57 #define numDomains 4
58 static const struct {
59 char needsRootPrepended;
60 const char *domainPath;
61 const char *alternateDomainPath;
62 } domainInfo[numDomains] = {
63 {0, "~", "~"},
64 {1, "", ""},
65 {1, "/Network", "/Network"},
66 {1, "", "/System"}
67 };
68
69 #define invalidDomains 0x00 // some domains may be invalid on non-Mach systems
70
71 NSSearchPathEnumerationState NSStartSearchPathEnumeration(NSSearchPathDirectory dir, NSSearchPathDomainMask domainMask) {
72 // The state is AABBCCCC, where
73 // AA is the dir(s) requested
74 // BB is the current state of dirs (if AA < 100, then this is always 0; otherwise it goes up to number of dirs)
75 // CCCC is the domains requested
76 // the state always contains the next item; if CCCC is 0, then we're done
77 domainMask = domainMask & ((1 << numDomains) - 1) & ~invalidDomains; // Just leave useful bits in there
78 if (dir != NSAllLibrariesDirectory && dir != NSLibraryDirectory && dir != NSUserDirectory && dir != NSDocumentationDirectory && (domainMask & NSLocalDomainMask) && (domainMask & NSSystemDomainMask)) domainMask = domainMask & ~NSSystemDomainMask; // Hack to avoid duplication
79 return (((unsigned int)dir) << 24) + ((unsigned int)domainMask);
80 }
81
82 NSSearchPathEnumerationState NSGetNextSearchPathEnumeration(NSSearchPathEnumerationState state, char *path) {
83 static const char *nextRoot = NULL;
84 unsigned dir = (state >> 24) & 0xff;
85 unsigned dirState = (state >> 16) & 0xff;
86 unsigned domainMask = state & 0xffff;
87 unsigned int curDomain; // The current domain we're at...
88 unsigned int curDir = 0; // The current dir...
89
90 do {
91 if (domainMask == 0) return 0; // Looks like we're done
92 for (curDomain = 0; curDomain < numDomains; curDomain++) if ((domainMask & (1 << curDomain))) break;
93
94 // Determine directory
95 if (dir < NSAllApplicationsDirectory) { // One directory per domain, simple...
96 curDir = dir;
97 } else { // Can return multiple directories for each domain
98 if (dir == NSAllApplicationsDirectory) {
99 curDir = applicationDirs[dirState];
100 if (++dirState == numApplicationDirs) dirState = 0;
101 } else if (dir == NSAllLibrariesDirectory) {
102 curDir = libraryDirs[dirState];
103 if (++dirState == numLibraryDirs) dirState = 0;
104 }
105 }
106 if (dirState == 0) domainMask &= ~(1 << curDomain); // If necessary, jump to next domain
107 } while ((dirInfo[curDir - 1].invalidDomainMask & (1 << curDomain))); // If invalid, try again...
108
109 // Get NEXT_ROOT, if necessary.
110 if (domainInfo[curDomain].needsRootPrepended && nextRoot == 0) {
111 nextRoot = getenv("NEXT_ROOT");
112 if (nextRoot == NULL) {
113 nextRoot = "";
114 } else {
115 strcpy(malloc(strlen(nextRoot) + 1), nextRoot); // Be safe...
116 }
117 }
118
119 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);
120
121 return (dir << 24) + (dirState << 16) + domainMask;
122 }
123
124