]> git.saurik.com Git - apple/libc.git/blob - gen.subproj/NSSystemDirectories.c
1e37408c6eeab95d0562619a6ec340020632c5a3
[apple/libc.git] / gen.subproj / 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 8
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 };
45
46 // Ordered list of where to find applications in each domain (the numbers are NSSearchPathDirectory)
47 #define numApplicationDirs 4
48 static const char applicationDirs[numApplicationDirs] = {1, 4, 3, 2};
49
50 // Ordered list of where to find resources in each domain (the numbers are NSSearchPathDirectory)
51 #define numLibraryDirs 2
52 static const char libraryDirs[numLibraryDirs] = {5, 6};
53
54 // 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).
55 #define numDomains 4
56 static const struct {
57 char needsRootPrepended;
58 const char *domainPath;
59 const char *alternateDomainPath;
60 } domainInfo[numDomains] = {
61 {0, "~", "~"},
62 {1, "", ""},
63 {1, "/Network", "/Network"},
64 {1, "", "/System"}
65 };
66
67 #define invalidDomains 0x00 // some domains may be invalid on non-Mach systems
68
69 NSSearchPathEnumerationState NSStartSearchPathEnumeration(NSSearchPathDirectory dir, NSSearchPathDomainMask domainMask) {
70 // The state is AABBCCCC, where
71 // AA is the dir(s) requested
72 // BB is the current state of dirs (if AA < 100, then this is always 0; otherwise it goes up to number of dirs)
73 // CCCC is the domains requested
74 // the state always contains the next item; if CCCC is 0, then we're done
75 domainMask = domainMask & ((1 << numDomains) - 1) & ~invalidDomains; // Just leave useful bits in there
76 if (dir != NSAllLibrariesDirectory && dir != NSLibraryDirectory && dir != NSUserDirectory && dir != NSDocumentationDirectory && (domainMask & NSLocalDomainMask) && (domainMask & NSSystemDomainMask)) domainMask = domainMask & ~NSSystemDomainMask; // Hack to avoid duplication
77 return (((unsigned int)dir) << 24) + ((unsigned int)domainMask);
78 }
79
80 NSSearchPathEnumerationState NSGetNextSearchPathEnumeration(NSSearchPathEnumerationState state, char *path) {
81 static const char *nextRoot = NULL;
82 unsigned dir = (state >> 24) & 0xff;
83 unsigned dirState = (state >> 16) & 0xff;
84 unsigned domainMask = state & 0xffff;
85 unsigned int curDomain; // The current domain we're at...
86 unsigned int curDir = 0; // The current dir...
87
88 do {
89 if (domainMask == 0) return 0; // Looks like we're done
90 for (curDomain = 0; curDomain < numDomains; curDomain++) if ((domainMask & (1 << curDomain))) break;
91
92 // Determine directory
93 if (dir < NSAllApplicationsDirectory) { // One directory per domain, simple...
94 curDir = dir;
95 } else { // Can return multiple directories for each domain
96 if (dir == NSAllApplicationsDirectory) {
97 curDir = applicationDirs[dirState];
98 if (++dirState == numApplicationDirs) dirState = 0;
99 } else if (dir == NSAllLibrariesDirectory) {
100 curDir = libraryDirs[dirState];
101 if (++dirState == numLibraryDirs) dirState = 0;
102 }
103 }
104 if (dirState == 0) domainMask &= ~(1 << curDomain); // If necessary, jump to next domain
105 } while ((dirInfo[curDir - 1].invalidDomainMask & (1 << curDomain))); // If invalid, try again...
106
107 // Get NEXT_ROOT, if necessary.
108 if (domainInfo[curDomain].needsRootPrepended && nextRoot == 0) {
109 nextRoot = getenv("NEXT_ROOT");
110 if (nextRoot == NULL) {
111 nextRoot = "";
112 } else {
113 strcpy(malloc(strlen(nextRoot) + 1), nextRoot); // Be safe...
114 }
115 }
116
117 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);
118
119 return (dir << 24) + (dirState << 16) + domainMask;
120 }
121
122