]> git.saurik.com Git - apple/cf.git/blob - CFSystemDirectories.c
CF-550.13.tar.gz
[apple/cf.git] / CFSystemDirectories.c
1 /*
2 * Copyright (c) 2009 Apple 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
24 /* CFSystemDirectories.c
25 Copyright (c) 1997-2009, Apple Inc. All rights reserved.
26 Responsibility: Ali Ozer
27 */
28
29 /*
30 This file defines CFCopySearchPathForDirectoriesInDomains().
31 On MacOS 8, this function returns empty array.
32 On Mach, it calls the System.framework enumeration functions.
33 On Windows, it calls the enumeration functions defined here.
34 */
35
36 #include <CoreFoundation/CFPriv.h>
37 #include "CFInternal.h"
38
39 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
40
41 /* We use the System framework implementation on Mach.
42 */
43 #include <libc.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <NSSystemDirectories.h>
47
48 CFSearchPathEnumerationState __CFStartSearchPathEnumeration(CFSearchPathDirectory dir, CFSearchPathDomainMask domainMask) {
49 return NSStartSearchPathEnumeration(dir, domainMask);
50 }
51
52 CFSearchPathEnumerationState __CFGetNextSearchPathEnumeration(CFSearchPathEnumerationState state, uint8_t *path, CFIndex pathSize) {
53 CFSearchPathEnumerationState result;
54 // NSGetNextSearchPathEnumeration requires a MAX_PATH size
55 if (pathSize < PATH_MAX) {
56 uint8_t tempPath[PATH_MAX];
57 result = NSGetNextSearchPathEnumeration(state, (char *)tempPath);
58 strlcpy((char *)path, (char *)tempPath, pathSize);
59 } else {
60 result = NSGetNextSearchPathEnumeration(state, (char *)path);
61 }
62 return result;
63 }
64
65 #endif
66
67
68 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS_SYNC
69
70 CFArrayRef CFCopySearchPathForDirectoriesInDomains(CFSearchPathDirectory directory, CFSearchPathDomainMask domainMask, Boolean expandTilde) {
71 CFMutableArrayRef array;
72 CFSearchPathEnumerationState state;
73 CFIndex homeLen = -1;
74 char cPath[CFMaxPathSize], home[CFMaxPathSize];
75
76 array = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeArrayCallBacks);
77 state = __CFStartSearchPathEnumeration(directory, domainMask);
78 while ((state = __CFGetNextSearchPathEnumeration(state, (uint8_t *)cPath, sizeof(cPath)))) {
79 CFURLRef url = NULL;
80 if (expandTilde && (cPath[0] == '~')) {
81 if (homeLen < 0) {
82 CFURLRef homeURL = CFCopyHomeDirectoryURLForUser(NULL);
83 if (homeURL) {
84 CFURLGetFileSystemRepresentation(homeURL, true, (uint8_t *)home, CFMaxPathSize);
85 homeLen = strlen(home);
86 CFRelease(homeURL);
87 }
88 }
89 if (homeLen + strlen(cPath) < CFMaxPathSize) {
90 home[homeLen] = '\0';
91 strlcat(home, &cPath[1], sizeof(home));
92 url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorSystemDefault, (uint8_t *)home, strlen(home), true);
93 }
94 } else {
95 url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorSystemDefault, (uint8_t *)cPath, strlen(cPath), true);
96 }
97 if (url) {
98 CFArrayAppendValue(array, url);
99 CFRelease(url);
100 }
101 }
102 return array;
103 }
104
105 #endif
106
107 #undef numDirs
108 #undef numApplicationDirs
109 #undef numLibraryDirs
110 #undef numDomains
111 #undef invalidDomains
112 #undef invalidDomains
113