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