]> git.saurik.com Git - apple/cf.git/blob - Base.subproj/CFSystemDirectories.c
CF-368.tar.gz
[apple/cf.git] / Base.subproj / CFSystemDirectories.c
1 /*
2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /* CFSystemDirectories.c
24 Copyright 1997-2002, Apple, Inc. All rights reserved.
25 Responsibility: Ali Ozer
26 */
27
28 /*
29 This file defines CFCopySearchPathForDirectoriesInDomains().
30 On MacOS 8, this function returns empty array.
31 On Mach, it calls the System.framework enumeration functions.
32 On Windows, it calls the enumeration functions defined here.
33 */
34
35 #include <CoreFoundation/CFPriv.h>
36 #include "CFInternal.h"
37
38 #if defined(__MACH__)
39
40 /* We use the System framework implementation on Mach.
41 */
42 #include <libc.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <NSSystemDirectories.h>
46
47 CFSearchPathEnumerationState __CFStartSearchPathEnumeration(CFSearchPathDirectory dir, CFSearchPathDomainMask domainMask) {
48 return NSStartSearchPathEnumeration(dir, domainMask);
49 }
50
51 CFSearchPathEnumerationState __CFGetNextSearchPathEnumeration(CFSearchPathEnumerationState state, uint8_t *path, CFIndex pathSize) {
52 CFSearchPathEnumerationState result;
53 // NSGetNextSearchPathEnumeration requires a MAX_PATH size
54 if (pathSize < PATH_MAX) {
55 uint8_t tempPath[PATH_MAX];
56 result = NSGetNextSearchPathEnumeration(state, tempPath);
57 strlcpy(path, tempPath, pathSize);
58 } else {
59 result = NSGetNextSearchPathEnumeration(state, path);
60 }
61 return result;
62 }
63
64 #endif
65
66
67 #if defined(__MACH__) || defined(__WIN32__)
68
69 CFArrayRef CFCopySearchPathForDirectoriesInDomains(CFSearchPathDirectory directory, CFSearchPathDomainMask domainMask, Boolean expandTilde) {
70 CFMutableArrayRef array;
71 CFSearchPathEnumerationState state;
72 CFIndex homeLen = -1;
73 char cPath[CFMaxPathSize], home[CFMaxPathSize];
74
75 array = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
76 state = __CFStartSearchPathEnumeration(directory, domainMask);
77 while ((state = __CFGetNextSearchPathEnumeration(state, cPath, sizeof(cPath)))) {
78 CFURLRef url = NULL;
79 if (expandTilde && (cPath[0] == '~')) {
80 if (homeLen < 0) {
81 CFURLRef homeURL = CFCopyHomeDirectoryURLForUser(NULL);
82 if (homeURL) {
83 CFURLGetFileSystemRepresentation(homeURL, true, home, CFMaxPathSize);
84 homeLen = strlen(home);
85 CFRelease(homeURL);
86 }
87 }
88 if (homeLen + strlen(cPath) < CFMaxPathSize) {
89 home[homeLen] = '\0';
90 strcat(home, &cPath[1]);
91 url = CFURLCreateFromFileSystemRepresentation(NULL, home, strlen(home), true);
92 }
93 } else {
94 url = CFURLCreateFromFileSystemRepresentation(NULL, cPath, strlen(cPath), true);
95 }
96 if (url) {
97 CFArrayAppendValue(array, url);
98 CFRelease(url);
99 }
100 }
101 return array;
102 }
103
104 #endif
105
106 #undef numDirs
107 #undef numApplicationDirs
108 #undef numLibraryDirs
109 #undef numDomains
110 #undef invalidDomains
111 #undef invalidDomains
112