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