]> git.saurik.com Git - apple/configd.git/blob - scutil.tproj/cache.c
configd-84.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 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25
26 /*
27 * Modification History
28 *
29 * June 1, 2001 Allan Nathanson <ajn@apple.com>
30 * - public API conversion
31 *
32 * November 9, 2000 Allan Nathanson <ajn@apple.com>
33 * - initial revision
34 */
35
36 #include <sys/types.h>
37
38 #include "scutil.h"
39 #include "cache.h"
40
41
42 static CFComparisonResult
43 sort_keys(const void *p1, const void *p2, void *context) {
44 CFStringRef key1 = (CFStringRef)p1;
45 CFStringRef key2 = (CFStringRef)p2;
46 return CFStringCompare(key1, key2, 0);
47 }
48
49
50 void
51 do_list(int argc, char **argv)
52 {
53 int i;
54 CFStringRef pattern;
55 CFArrayRef list;
56 CFIndex listCnt;
57 CFMutableArrayRef sortedList;
58
59 pattern = CFStringCreateWithCString(NULL,
60 (argc >= 1) ? argv[0] : ".*",
61 kCFStringEncodingMacRoman);
62
63 list = SCDynamicStoreCopyKeyList(store, pattern);
64 CFRelease(pattern);
65 if (!list) {
66 if (SCError() != kSCStatusOK) {
67 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
68 } else {
69 SCPrint(TRUE, stdout, CFSTR(" no keys.\n"));
70 }
71 return;
72 }
73
74 listCnt = CFArrayGetCount(list);
75 sortedList = CFArrayCreateMutableCopy(NULL, listCnt, list);
76 CFRelease(list);
77 CFArraySortValues(sortedList,
78 CFRangeMake(0, listCnt),
79 sort_keys,
80 NULL);
81
82 if (listCnt > 0) {
83 for (i = 0; i < listCnt; i++) {
84 SCPrint(TRUE,
85 stdout,
86 CFSTR(" subKey [%d] = %@\n"),
87 i,
88 CFArrayGetValueAtIndex(sortedList, i));
89 }
90 } else {
91 SCPrint(TRUE, stdout, CFSTR(" no keys.\n"));
92 }
93 CFRelease(sortedList);
94
95 return;
96 }
97
98
99 void
100 do_add(int argc, char **argv)
101 {
102 CFStringRef key;
103
104 key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingMacRoman);
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 void
122 do_get(int argc, char **argv)
123 {
124 CFStringRef key;
125 CFPropertyListRef newValue;
126
127 key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingMacRoman);
128 newValue = SCDynamicStoreCopyValue(store, key);
129 CFRelease(key);
130 if (!newValue) {
131 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
132 return;
133 }
134
135 if (value != NULL) {
136 CFRelease(value); /* we have new information, release the old */
137 }
138 value = newValue;
139
140 return;
141 }
142
143
144 void
145 do_set(int argc, char **argv)
146 {
147 CFStringRef key;
148
149 key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingMacRoman);
150 if (!SCDynamicStoreSetValue(store, key, value)) {
151 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
152 }
153 CFRelease(key);
154 return;
155 }
156
157
158 void
159 do_show(int argc, char **argv)
160 {
161 CFStringRef key;
162 CFPropertyListRef newValue;
163
164 key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingMacRoman);
165
166 if (argc == 1) {
167 newValue = SCDynamicStoreCopyValue(store, key);
168 } else {
169 CFArrayRef patterns;
170
171 patterns = CFArrayCreate(NULL, (const void **)&key, 1, &kCFTypeArrayCallBacks);
172 newValue = SCDynamicStoreCopyMultiple(store, NULL, patterns);
173 CFRelease(patterns);
174 }
175
176 CFRelease(key);
177 if (!newValue) {
178 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
179 return;
180 }
181
182 SCPrint(TRUE, stdout, CFSTR("%@\n"), newValue);
183 CFRelease(newValue);
184 return;
185 }
186
187
188 void
189 do_remove(int argc, char **argv)
190 {
191 CFStringRef key;
192
193 key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingMacRoman);
194 if (!SCDynamicStoreRemoveValue(store, key)) {
195 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
196 }
197 CFRelease(key);
198 return;
199 }
200
201
202 void
203 do_notify(int argc, char **argv)
204 {
205 CFStringRef key;
206
207 key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingMacRoman);
208 if (!SCDynamicStoreNotifyValue(store, key)) {
209 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
210 }
211 CFRelease(key);
212 return;
213 }
214
215
216 void
217 do_touch(int argc, char **argv)
218 {
219 CFStringRef key;
220
221 key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingMacRoman);
222 if (!SCDynamicStoreTouchValue(store, key)) {
223 SCPrint(TRUE, stdout, CFSTR(" %s\n"), SCErrorString(SCError()));
224 }
225 CFRelease(key);
226 return;
227 }