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