1/* 2 * Copyright (c) 2014 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-2013, Apple Inc. All rights reserved. 26 Responsibility: Kevin Perry 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 <pwd.h> 47#include <NSSystemDirectories.h> 48 49CFSearchPathEnumerationState __CFStartSearchPathEnumeration(CFSearchPathDirectory dir, CFSearchPathDomainMask domainMask) { 50 return NSStartSearchPathEnumeration(dir, domainMask); 51} 52 53CFSearchPathEnumerationState __CFGetNextSearchPathEnumeration(CFSearchPathEnumerationState state, uint8_t *path, CFIndex pathSize) { 54 CFSearchPathEnumerationState result; 55 // NSGetNextSearchPathEnumeration requires a MAX_PATH size 56 if (pathSize < PATH_MAX) { 57 uint8_t tempPath[PATH_MAX]; 58 result = NSGetNextSearchPathEnumeration(state, (char *)tempPath); 59 strlcpy((char *)path, (char *)tempPath, pathSize); 60 } else { 61 result = NSGetNextSearchPathEnumeration(state, (char *)path); 62 } 63 return result; 64} 65 66#endif 67 68 69#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS 70 71CFArrayRef CFCopySearchPathForDirectoriesInDomains(CFSearchPathDirectory directory, CFSearchPathDomainMask domainMask, Boolean expandTilde) { 72 CFMutableArrayRef array; 73 CFSearchPathEnumerationState state; 74 CFIndex homeLen = -1; 75 char cPath[CFMaxPathSize], home[CFMaxPathSize]; 76 77 array = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeArrayCallBacks); 78 state = __CFStartSearchPathEnumeration(directory, domainMask); 79 while ((state = __CFGetNextSearchPathEnumeration(state, (uint8_t *)cPath, sizeof(cPath)))) { 80 CFURLRef url = NULL; 81 if (expandTilde && (cPath[0] == '~')) { 82 if (homeLen < 0) { 83 CFURLRef homeURL = CFCopyHomeDirectoryURLForUser(NULL); 84 if (homeURL) { 85 CFURLGetFileSystemRepresentation(homeURL, true, (uint8_t *)home, CFMaxPathSize); 86 homeLen = strlen(home); 87 CFRelease(homeURL); 88 } 89 } 90 if (homeLen + strlen(cPath) < CFMaxPathSize) { 91 home[homeLen] = '\0'; 92 strlcat(home, &cPath[1], sizeof(home)); 93 url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorSystemDefault, (uint8_t *)home, strlen(home), true); 94 } 95 } else { 96 url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorSystemDefault, (uint8_t *)cPath, strlen(cPath), true); 97 } 98 if (url) { 99 CFArrayAppendValue(array, url); 100 CFRelease(url); 101 } 102 } 103 return array; 104} 105 106#endif 107 108 109#undef numDirs 110#undef numApplicationDirs 111#undef numLibraryDirs 112#undef numDomains 113#undef invalidDomains 114#undef invalidDomains 115 116