]> git.saurik.com Git - apple/configd.git/blob - scutil.tproj/cache.c
configd-293.4.tar.gz
[apple/configd.git] / scutil.tproj / cache.c
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, 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 /*
25 * Modification History
26 *
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
29 *
30 * November 9, 2000 Allan Nathanson <ajn@apple.com>
31 * - initial revision
32 */
33
34 #include <sys/types.h>
35
36 #include "scutil.h"
37 #include "cache.h"
38
39
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);
45 }
46
47
48 __private_extern__
49 void
50 do_list(int argc, char **argv)
51 {
52 int i;
53 CFStringRef pattern;
54 CFArrayRef list;
55 CFIndex listCnt;
56 CFMutableArrayRef sortedList;
57
58 pattern = CFStringCreateWithCString(NULL,
59 (argc >= 1) ? argv[0] : ".*",
60 kCFStringEncodingUTF8);
61
62 list = SCDynamicStoreCopyKeyList(store, pattern);
63 CFRelease(pattern);
64 if (list == NULL) {
65 if (SCError() != kSCStatusOK) {
66 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
67 } else {
68 SCPrint(TRUE, stdout, CFSTR(" no keys.\n"));
69 }
70 return;
71 }
72
73 listCnt = CFArrayGetCount(list);
74 sortedList = CFArrayCreateMutableCopy(NULL, listCnt, list);
75 CFRelease(list);
76 CFArraySortValues(sortedList,
77 CFRangeMake(0, listCnt),
78 sort_keys,
79 NULL);
80
81 if (listCnt > 0) {
82 for (i = 0; i < listCnt; i++) {
83 SCPrint(TRUE,
84 stdout,
85 CFSTR(" subKey [%d] = %@\n"),
86 i,
87 CFArrayGetValueAtIndex(sortedList, i));
88 }
89 } else {
90 SCPrint(TRUE, stdout, CFSTR(" no keys.\n"));
91 }
92 CFRelease(sortedList);
93
94 return;
95 }
96
97
98 __private_extern__
99 void
100 do_add(int argc, char **argv)
101 {
102 CFStringRef key;
103
104 key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8);
105
106 if (argc < 2) {
107 if (!SCDynamicStoreAddValue(store, key, value)) {
108 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
109 }
110 } else {
111 if (!SCDynamicStoreAddTemporaryValue(store, key, value)) {
112 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
113 }
114 }
115
116 CFRelease(key);
117 return;
118 }
119
120
121 __private_extern__
122 void
123 do_get(int argc, char **argv)
124 {
125 CFStringRef key;
126 CFPropertyListRef newValue;
127
128 key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8);
129 newValue = SCDynamicStoreCopyValue(store, key);
130 CFRelease(key);
131 if (newValue == NULL) {
132 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
133 return;
134 }
135
136 if (value != NULL) {
137 CFRelease(value); /* we have new information, release the old */
138 }
139 value = newValue;
140
141 return;
142 }
143
144
145 __private_extern__
146 void
147 do_set(int argc, char **argv)
148 {
149 CFStringRef key;
150
151 key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8);
152 if (!SCDynamicStoreSetValue(store, key, value)) {
153 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
154 }
155 CFRelease(key);
156 return;
157 }
158
159
160 __private_extern__
161 void
162 do_show(int argc, char **argv)
163 {
164 CFStringRef key;
165 CFPropertyListRef newValue;
166
167 key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8);
168
169 if (argc == 1) {
170 newValue = SCDynamicStoreCopyValue(store, key);
171 } else {
172 CFArrayRef patterns;
173
174 patterns = CFArrayCreate(NULL, (const void **)&key, 1, &kCFTypeArrayCallBacks);
175 newValue = SCDynamicStoreCopyMultiple(store, NULL, patterns);
176 CFRelease(patterns);
177 }
178
179 CFRelease(key);
180 if (newValue == NULL) {
181 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
182 return;
183 }
184
185 SCPrint(TRUE, stdout, CFSTR("%@\n"), newValue);
186 CFRelease(newValue);
187 return;
188 }
189
190
191 __private_extern__
192 void
193 do_remove(int argc, char **argv)
194 {
195 CFStringRef key;
196
197 key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8);
198 if (!SCDynamicStoreRemoveValue(store, key)) {
199 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
200 }
201 CFRelease(key);
202 return;
203 }
204
205
206 __private_extern__
207 void
208 do_notify(int argc, char **argv)
209 {
210 CFStringRef key;
211
212 key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8);
213 if (!SCDynamicStoreNotifyValue(store, key)) {
214 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
215 }
216 CFRelease(key);
217 return;
218 }
219
220
221 __private_extern__
222 void
223 do_touch(int argc, char **argv)
224 {
225 CFStringRef key;
226
227 key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8);
228 if (!SCDynamicStoreTouchValue(store, key)) {
229 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
230 }
231 CFRelease(key);
232 return;
233 }