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