]> git.saurik.com Git - apple/configd.git/blob - scutil.tproj/prefs.c
configd-135.tar.gz
[apple/configd.git] / scutil.tproj / prefs.c
1 /*
2 * Copyright (c) 2003-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 * May 29, 2003 Allan Nathanson <ajn@apple.com>
28 * - initial revision
29 */
30
31 #include "scutil.h"
32 #include "prefs.h"
33
34 #include <SCPreferencesSetSpecific.h>
35
36
37 static SCPreferencesRef
38 _open()
39 {
40 prefs = SCPreferencesCreate(NULL, CFSTR("scutil"), NULL);
41 if (prefs == NULL) {
42 SCPrint(TRUE,
43 stdout,
44 CFSTR("SCPreferencesCreate() failed: %s\n"),
45 SCErrorString(SCError()));
46 exit (1);
47 }
48
49 return prefs;
50 }
51
52
53 static void
54 _save(SCPreferencesRef prefs)
55 {
56 if (!SCPreferencesCommitChanges(prefs)) {
57 switch (SCError()) {
58 case kSCStatusAccessError :
59 SCPrint(TRUE, stderr, CFSTR("Permission denied.\n"));
60 break;
61 default :
62 SCPrint(TRUE,
63 stdout,
64 CFSTR("SCPreferencesCommitChanges() failed: %s\n"),
65 SCErrorString(SCError()));
66 break;
67 }
68 exit (1);
69 }
70
71 if (!SCPreferencesApplyChanges(prefs)) {
72 SCPrint(TRUE,
73 stdout,
74 CFSTR("SCPreferencesApplyChanges() failed: %s\n"),
75 SCErrorString(SCError()));
76 exit (1);
77 }
78
79 return;
80 }
81
82
83 static CFStringRef
84 _copyStringFromSTDIN()
85 {
86 char buf[1024];
87 size_t len;
88 CFStringRef utf8;
89
90 if (fgets(buf, sizeof(buf), stdin) == NULL) {
91 return NULL;
92 }
93
94 len = strlen(buf);
95 if (buf[len-1] == '\n') {
96 buf[--len] = '\0';
97 }
98
99 utf8 = CFStringCreateWithBytes(NULL, (UInt8 *)buf, len, kCFStringEncodingUTF8, TRUE);
100 return utf8;
101 }
102
103
104 static void
105 get_ComputerName(int argc, char **argv)
106 {
107 CFStringEncoding encoding;
108 CFStringRef hostname;
109 CFDataRef utf8;
110
111 hostname = SCDynamicStoreCopyComputerName(NULL, &encoding);
112 if (!hostname) {
113 int sc_status = SCError();
114
115 switch (sc_status) {
116 case kSCStatusNoKey :
117 SCPrint(TRUE,
118 stderr,
119 CFSTR("ComputerName: not set\n"));
120 break;
121 default :
122 SCPrint(TRUE,
123 stderr,
124 CFSTR("SCDynamicStoreCopyComputerName() failed: %s\n"),
125 SCErrorString(SCError()));
126 break;
127 }
128 exit (1);
129 }
130
131 utf8 = CFStringCreateExternalRepresentation(NULL, hostname, kCFStringEncodingUTF8, 0);
132 if (!utf8) {
133 SCPrint(TRUE,
134 stderr,
135 CFSTR("ComputerName: could not convert to external representation\n"));
136 exit(1);
137 }
138 SCPrint(TRUE, stdout, CFSTR("%.*s\n"), CFDataGetLength(utf8), CFDataGetBytePtr(utf8));
139
140 CFRelease(utf8);
141 CFRelease(hostname);
142 exit(0);
143 }
144
145
146 static void
147 set_ComputerName(int argc, char **argv)
148 {
149 CFStringEncoding encoding;
150 CFStringRef hostname;
151
152 if (argc == 0) {
153 hostname = _copyStringFromSTDIN();
154 encoding = kCFStringEncodingUTF8;
155 } else {
156 hostname = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingASCII);
157 encoding = kCFStringEncodingASCII;
158 }
159
160 prefs = _open();
161 if (!SCPreferencesSetComputerName(prefs, hostname, encoding)) {
162 SCPrint(TRUE,
163 stdout,
164 CFSTR("SCPreferencesSetComputerName() failed: %s\n"),
165 SCErrorString(SCError()));
166 exit (1);
167 }
168 _save(prefs);
169 CFRelease(prefs);
170 CFRelease(hostname);
171 exit(0);
172 }
173
174
175 static void
176 get_HostName(int argc, char **argv)
177 {
178 CFStringRef hostname;
179
180 prefs = _open();
181 hostname = SCPreferencesGetHostName(prefs);
182 if (hostname == NULL) {
183 int sc_status = SCError();
184
185 switch (sc_status) {
186 case kSCStatusNoKey :
187 SCPrint(TRUE,
188 stderr,
189 CFSTR("HostName: not set\n"));
190 break;
191 default :
192 SCPrint(TRUE,
193 stderr,
194 CFSTR("SCPreferencesGetHostName() failed: %s\n"),
195 SCErrorString(SCError()));
196 break;
197 }
198 CFRelease(prefs);
199 exit (1);
200 }
201
202 SCPrint(TRUE, stdout, CFSTR("%@\n"), hostname);
203 CFRelease(hostname);
204 exit(0);
205 }
206
207
208 static void
209 set_HostName(int argc, char **argv)
210 {
211 CFStringRef hostname = NULL;
212
213 if (argc == 0) {
214 hostname = _copyStringFromSTDIN();
215 } else {
216 hostname = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingASCII);
217 }
218
219 prefs = _open();
220 if (!SCPreferencesSetHostName(prefs, hostname)) {
221 SCPrint(TRUE,
222 stderr,
223 CFSTR("SCPreferencesSetHostName() failed: %s\n"),
224 SCErrorString(SCError()));
225 CFRelease(hostname);
226 CFRelease(prefs);
227 exit (1);
228 }
229 _save(prefs);
230 CFRelease(hostname);
231 CFRelease(prefs);
232 exit(0);
233 }
234
235
236 static void
237 get_LocalHostName(int argc, char **argv)
238 {
239 CFStringRef hostname;
240
241 hostname = SCDynamicStoreCopyLocalHostName(NULL);
242 if (!hostname) {
243 int sc_status = SCError();
244
245 switch (sc_status) {
246 case kSCStatusNoKey :
247 SCPrint(TRUE,
248 stderr,
249 CFSTR("LocalHostName: not set\n"));
250 break;
251 default :
252 SCPrint(TRUE,
253 stderr,
254 CFSTR("SCDynamicStoreCopyLocalHostName() failed: %s\n"),
255 SCErrorString(SCError()));
256 break;
257 }
258 exit (1);
259 }
260
261 SCPrint(TRUE, stdout, CFSTR("%@\n"), hostname);
262 CFRelease(hostname);
263 exit(0);
264 }
265
266
267 static void
268 set_LocalHostName(int argc, char **argv)
269 {
270 CFStringRef hostname = NULL;
271
272 if (argc == 0) {
273 hostname = _copyStringFromSTDIN();
274 } else {
275 hostname = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingASCII);
276 }
277
278 prefs = _open();
279 if (!SCPreferencesSetLocalHostName(prefs, hostname)) {
280 SCPrint(TRUE,
281 stderr,
282 CFSTR("SCPreferencesSetLocalHostName() failed: %s\n"),
283 SCErrorString(SCError()));
284 exit (1);
285 }
286 _save(prefs);
287 CFRelease(prefs);
288 CFRelease(hostname);
289 exit(0);
290 }
291
292
293 typedef void (*pref_func) (int argc, char **argv);
294
295 static const struct {
296 char *pref;
297 pref_func get;
298 pref_func set;
299 } pref_keys[] = {
300 { "ComputerName", get_ComputerName, set_ComputerName },
301 { "HostName", get_HostName, set_HostName },
302 { "LocalHostName", get_LocalHostName, set_LocalHostName }
303 };
304 #define N_PREF_KEYS (sizeof(pref_keys) / sizeof(pref_keys[0]))
305
306
307 __private_extern__
308 int
309 findPref(char *pref)
310 {
311 int i;
312
313 for (i = 0; i < (int)N_PREF_KEYS; i++) {
314 if (strcmp(pref, pref_keys[i].pref) == 0) {
315 return i;
316 }
317 }
318
319 return -1;
320 }
321
322
323 __private_extern__
324 void
325 do_getPref(char *pref, int argc, char **argv)
326 {
327 int i;
328
329 i = findPref(pref);
330 if (i >= 0) {
331 (*pref_keys[i].get)(argc, argv);
332 }
333 return;
334 }
335
336
337 __private_extern__
338 void
339 do_setPref(char *pref, int argc, char **argv)
340 {
341 int i;
342
343 i = findPref(pref);
344 if (i >= 0) {
345 (*pref_keys[i].set)(argc, argv);
346 }
347 return;
348 }