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