2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
30 * November 9, 2000 Allan Nathanson <ajn@apple.com>
34 #include <sys/types.h>
40 static CFComparisonResult
41 sort_keys(const void *p1
, const void *p2
, void *context
) {
42 CFStringRef key1
= (CFStringRef
)p1
;
43 CFStringRef key2
= (CFStringRef
)p2
;
44 return CFStringCompare(key1
, key2
, 0);
50 do_list(int argc
, char **argv
)
56 CFMutableArrayRef sortedList
;
58 pattern
= CFStringCreateWithCString(NULL
,
59 (argc
>= 1) ? argv
[0] : ".*",
60 kCFStringEncodingUTF8
);
62 list
= SCDynamicStoreCopyKeyList(store
, pattern
);
65 if (SCError() != kSCStatusOK
) {
66 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
68 SCPrint(TRUE
, stdout
, CFSTR(" no keys.\n"));
73 listCnt
= CFArrayGetCount(list
);
74 sortedList
= CFArrayCreateMutableCopy(NULL
, listCnt
, list
);
76 CFArraySortValues(sortedList
,
77 CFRangeMake(0, listCnt
),
82 for (i
= 0; i
< listCnt
; i
++) {
85 CFSTR(" subKey [%d] = %@\n"),
87 CFArrayGetValueAtIndex(sortedList
, i
));
90 SCPrint(TRUE
, stdout
, CFSTR(" no keys.\n"));
92 CFRelease(sortedList
);
100 do_add(int argc
, char **argv
)
104 key
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
107 if (!SCDynamicStoreAddValue(store
, key
, value
)) {
108 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
111 if (!SCDynamicStoreAddTemporaryValue(store
, key
, value
)) {
112 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
123 do_get(int argc
, char **argv
)
126 CFPropertyListRef newValue
;
128 key
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
129 newValue
= SCDynamicStoreCopyValue(store
, key
);
131 if (newValue
== NULL
) {
132 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
137 CFRelease(value
); /* we have new information, release the old */
147 do_set(int argc
, char **argv
)
151 key
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
152 if (!SCDynamicStoreSetValue(store
, key
, value
)) {
153 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
162 do_show(int argc
, char **argv
)
165 CFPropertyListRef newValue
;
167 key
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
170 newValue
= SCDynamicStoreCopyValue(store
, key
);
174 patterns
= CFArrayCreate(NULL
, (const void **)&key
, 1, &kCFTypeArrayCallBacks
);
175 newValue
= SCDynamicStoreCopyMultiple(store
, NULL
, patterns
);
180 if (newValue
== NULL
) {
181 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
185 SCPrint(TRUE
, stdout
, CFSTR("%@\n"), newValue
);
193 do_remove(int argc
, char **argv
)
197 key
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
198 if (!SCDynamicStoreRemoveValue(store
, key
)) {
199 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
208 do_notify(int argc
, char **argv
)
212 key
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
213 if (!SCDynamicStoreNotifyValue(store
, key
)) {
214 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
223 do_touch(int argc
, char **argv
)
227 key
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
228 if (!SCDynamicStoreTouchValue(store
, key
)) {
229 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));