]> git.saurik.com Git - apple/configd.git/blobdiff - scutil.tproj/cache.c
configd-53.1.tar.gz
[apple/configd.git] / scutil.tproj / cache.c
index 759959bad742fb896445315caa6bc1778ef1b829..dafc649cefed1e6a651a2fbed7c08e4feb20cf6e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  *
  * @APPLE_LICENSE_HEADER_END@
  */
 
+/*
+ * Modification History
+ *
+ * June 1, 2001                        Allan Nathanson <ajn@apple.com>
+ * - public API conversion
+ *
+ * November 9, 2000            Allan Nathanson <ajn@apple.com>
+ * - initial revision
+ */
+
 #include <sys/types.h>
 
 #include "scutil.h"
 
+
+static CFComparisonResult
+sort_keys(const void *p1, const void *p2, void *context) {
+       CFStringRef key1 = (CFStringRef)p1;
+       CFStringRef key2 = (CFStringRef)p2;
+       return CFStringCompare(key1, key2, 0);
+}
+
+
 void
 do_list(int argc, char **argv)
 {
-       CFStringRef             key;
-       int                     regexOptions = 0;
-       SCDStatus               status;
+       int                     i;
+       CFStringRef             pattern;
        CFArrayRef              list;
        CFIndex                 listCnt;
-       int                     i;
-
-       key = CFStringCreateWithCString(NULL,
-                                       (argc >= 1) ? argv[0] : "",
-                                       kCFStringEncodingMacRoman);
-
-       if (argc == 2)
-               regexOptions = kSCDRegexKey;
-
-       status = SCDList(session, key, regexOptions, &list);
-       CFRelease(key);
-       if (status != SCD_OK) {
-               SCDLog(LOG_INFO, CFSTR("SCDList: %s"), SCDError(status));
+       CFMutableArrayRef       sortedList;
+
+       pattern = CFStringCreateWithCString(NULL,
+                                           (argc >= 1) ? argv[0] : ".*",
+                                           kCFStringEncodingMacRoman);
+
+       list = SCDynamicStoreCopyKeyList(store, pattern);
+       CFRelease(pattern);
+       if (!list) {
+               if (SCError() != kSCStatusOK) {
+                       SCPrint(TRUE, stdout, CFSTR("  %s\n"), SCErrorString(SCError()));
+               } else {
+                       SCPrint(TRUE, stdout, CFSTR("  no keys.\n"));
+               }
                return;
        }
 
        listCnt = CFArrayGetCount(list);
+       sortedList = CFArrayCreateMutableCopy(NULL, listCnt, list);
+       CFRelease(list);
+       CFArraySortValues(sortedList,
+                         CFRangeMake(0, listCnt),
+                         sort_keys,
+                         NULL);
+
        if (listCnt > 0) {
                for (i=0; i<listCnt; i++) {
-                       SCDLog(LOG_NOTICE, CFSTR("  subKey [%d] = %@"), i, CFArrayGetValueAtIndex(list, i));
+                       SCPrint(TRUE,
+                               stdout,
+                               CFSTR("  subKey [%d] = %@\n"),
+                               i,
+                               CFArrayGetValueAtIndex(sortedList, i));
                }
        } else {
-               SCDLog(LOG_NOTICE, CFSTR("  no subKey's"));
+               SCPrint(TRUE, stdout, CFSTR("  no keys.\n"));
        }
-       CFRelease(list);
+       CFRelease(sortedList);
 
        return;
 }
@@ -66,19 +96,16 @@ void
 do_add(int argc, char **argv)
 {
        CFStringRef     key;
-       SCDStatus       status;
 
        key    = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingMacRoman);
 
        if (argc < 2) {
-               status = SCDAdd(session, key, data);
-               if (status != SCD_OK) {
-                       SCDLog(LOG_INFO, CFSTR("SCDAdd: %s"), SCDError(status));
+               if (!SCDynamicStoreAddValue(store, key, value)) {
+                       SCPrint(TRUE, stdout, CFSTR("  %s\n"), SCErrorString(SCError()));
                }
        } else {
-               status = SCDAddSession(session, key, data);
-               if (status != SCD_OK) {
-                       SCDLog(LOG_INFO, CFSTR("SCDAddSession: %s"), SCDError(status));
+               if (!SCDynamicStoreAddTemporaryValue(store, key, value)) {
+                       SCPrint(TRUE, stdout, CFSTR("  %s\n"), SCErrorString(SCError()));
                }
        }
 
@@ -90,25 +117,21 @@ do_add(int argc, char **argv)
 void
 do_get(int argc, char **argv)
 {
-       SCDStatus       status;
-       CFStringRef     key;
-       SCDHandleRef    newData = NULL;
+       CFStringRef             key;
+       CFPropertyListRef       newValue;
 
-       key    = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingMacRoman);
-       status = SCDGet(session, key, &newData);
+       key      = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingMacRoman);
+       newValue = SCDynamicStoreCopyValue(store, key);
        CFRelease(key);
-       if (status != SCD_OK) {
-               SCDLog(LOG_INFO, CFSTR("SCDGet: %s"), SCDError(status));
-               if (newData != NULL) {
-                       SCDHandleRelease(newData);      /* toss the handle from SCDGet() */
-               }
+       if (!newValue) {
+               SCPrint(TRUE, stdout, CFSTR("  %s\n"), SCErrorString(SCError()));
                return;
        }
 
-       if (data != NULL) {
-               SCDHandleRelease(data);         /* we got a new handle from SCDGet() */
+       if (value != NULL) {
+               CFRelease(value);               /* we have new information, release the old */
        }
-       data = newData;
+       value = newValue;
 
        return;
 }
@@ -117,15 +140,33 @@ do_get(int argc, char **argv)
 void
 do_set(int argc, char **argv)
 {
-       SCDStatus       status;
        CFStringRef     key;
 
        key    = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingMacRoman);
-       status = SCDSet(session, key, data);
+       if (!SCDynamicStoreSetValue(store, key, value)) {
+               SCPrint(TRUE, stdout, CFSTR("  %s\n"), SCErrorString(SCError()));
+       }
        CFRelease(key);
-       if (status != SCD_OK) {
-               SCDLog(LOG_INFO, CFSTR("SCDSet: %s"), SCDError(status));
+       return;
+}
+
+
+void
+do_show(int argc, char **argv)
+{
+       CFStringRef             key;
+       CFPropertyListRef       newValue;
+
+       key      = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingMacRoman);
+       newValue = SCDynamicStoreCopyValue(store, key);
+       CFRelease(key);
+       if (!newValue) {
+               SCPrint(TRUE, stdout, CFSTR("  %s\n"), SCErrorString(SCError()));
+               return;
        }
+
+       SCPrint(TRUE, stdout, CFSTR("%@\n"), newValue);
+       CFRelease(newValue);
        return;
 }
 
@@ -133,15 +174,27 @@ do_set(int argc, char **argv)
 void
 do_remove(int argc, char **argv)
 {
-       SCDStatus       status;
        CFStringRef     key;
 
        key    = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingMacRoman);
-       status = SCDRemove(session, key);
+       if (!SCDynamicStoreRemoveValue(store, key)) {
+               SCPrint(TRUE, stdout, CFSTR("  %s\n"), SCErrorString(SCError()));
+       }
        CFRelease(key);
-       if (status != SCD_OK) {
-               SCDLog(LOG_INFO, CFSTR("SCDRemove: %s"), SCDError(status));
+       return;
+}
+
+
+void
+do_notify(int argc, char **argv)
+{
+       CFStringRef     key;
+
+       key    = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingMacRoman);
+       if (!SCDynamicStoreNotifyValue(store, key)) {
+               SCPrint(TRUE, stdout, CFSTR("  %s\n"), SCErrorString(SCError()));
        }
+       CFRelease(key);
        return;
 }
 
@@ -149,14 +202,12 @@ do_remove(int argc, char **argv)
 void
 do_touch(int argc, char **argv)
 {
-       SCDStatus       status;
        CFStringRef     key;
 
        key    = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingMacRoman);
-       status = SCDTouch(session, key);
-       CFRelease(key);
-       if (status != SCD_OK) {
-               SCDLog(LOG_INFO, CFSTR("SCDTouch: %s"), SCDError(status));
+       if (!SCDynamicStoreTouchValue(store, key)) {
+               SCPrint(TRUE, stdout, CFSTR("  %s\n"), SCErrorString(SCError()));
        }
+       CFRelease(key);
        return;
 }