/* * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * The contents of this file constitute Original Code as defined in and * are subject to the Apple Public Source License Version 1.1 (the * "License"). You may not use this file except in compliance with the * License. Please obtain a copy of the License at * http://www.apple.com/publicsource and read it before using this file. * * This Original Code and all software distributed under the License are * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the * License for the specific language governing rights and limitations * under the License. * * @APPLE_LICENSE_HEADER_END@ */ #include "configd.h" #include "session.h" SCDStatus _SCDList(SCDSessionRef session, CFStringRef key, int regexOptions, CFArrayRef *subKeys) { SCDSessionPrivateRef sessionPrivate = (SCDSessionPrivateRef)session; CFIndex cacheCnt; void **cacheKeys; void **cacheValues; CFMutableArrayRef keyArray; int i; CFStringRef cacheStr; CFDictionaryRef cacheValue; int regexStrLen; char *regexStr = NULL; regex_t preg; int reError; char reErrBuf[256]; int reErrStrLen; SCDLog(LOG_DEBUG, CFSTR("_SCDList:")); SCDLog(LOG_DEBUG, CFSTR(" key = %@"), key); SCDLog(LOG_DEBUG, CFSTR(" regexOptions = %0o"), regexOptions); if ((session == NULL) || (sessionPrivate->server == MACH_PORT_NULL)) { return SCD_NOSESSION; } cacheCnt = CFDictionaryGetCount(cacheData); keyArray = CFArrayCreateMutable(NULL, cacheCnt, &kCFTypeArrayCallBacks); if (regexOptions & kSCDRegexKey) { /* * compile the provided regular expression using the * provided regexOptions (passing only those flags * which would make sense). */ regexStrLen = CFStringGetLength(key) + 1; regexStr = CFAllocatorAllocate(NULL, regexStrLen, 0); if (!CFStringGetCString(key, regexStr, regexStrLen, kCFStringEncodingMacRoman)) { SCDLog(LOG_DEBUG, CFSTR("CFStringGetCString() key: could not convert to regex string")); CFAllocatorDeallocate(NULL, regexStr); return SCD_FAILED; } reError = regcomp(&preg, regexStr, REG_EXTENDED); if (reError != 0) { reErrStrLen = regerror(reError, &preg, reErrBuf, sizeof(reErrBuf)); cacheStr = CFStringCreateWithCString(NULL, reErrBuf, kCFStringEncodingMacRoman); CFArrayAppendValue(keyArray, cacheStr); CFRelease(cacheStr); *subKeys = CFArrayCreateCopy(NULL, keyArray); CFRelease(keyArray); CFAllocatorDeallocate(NULL, regexStr); return SCD_FAILED; } } cacheKeys = CFAllocatorAllocate(NULL, cacheCnt * sizeof(CFStringRef), 0); cacheValues = CFAllocatorAllocate(NULL, cacheCnt * sizeof(CFStringRef), 0); CFDictionaryGetKeysAndValues(cacheData, cacheKeys, cacheValues); for (i=0; isession, key, regexOptions, &subKeys); CFRelease(key); if (*scd_status != SCD_OK) { *listRef = NULL; *listLen = 0; return KERN_SUCCESS; } /* * serialize the array, copy it into an allocated buffer which will be * released when it is returned as part of a Mach message. */ xmlList = CFPropertyListCreateXMLData(NULL, subKeys); CFRelease(subKeys); *listLen = CFDataGetLength(xmlList); status = vm_allocate(mach_task_self(), (void *)listRef, *listLen, TRUE); if (status != KERN_SUCCESS) { SCDLog(LOG_DEBUG, CFSTR("vm_allocate(): %s"), mach_error_string(status)); *scd_status = SCD_FAILED; CFRelease(xmlList); *listRef = NULL; *listLen = 0; return KERN_SUCCESS; } bcopy((char *)CFDataGetBytePtr(xmlList), *listRef, *listLen); CFRelease(xmlList); return KERN_SUCCESS; }