]> git.saurik.com Git - apple/configd.git/blob - scutil.tproj/prefs.c
configd-84.6.tar.gz
[apple/configd.git] / scutil.tproj / prefs.c
1 /*
2 * Copyright (c) 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 * 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 SCPreferencesRef prefs;
41
42 prefs = SCPreferencesCreate(NULL, CFSTR("scutil"), NULL);
43 if (!prefs) {
44 SCPrint(TRUE,
45 stdout,
46 CFSTR("SCPreferencesCreate() failed: %s\n"),
47 SCErrorString(SCError()));
48 exit (1);
49 }
50
51 return prefs;
52 }
53
54
55 static void
56 _save(SCPreferencesRef prefs)
57 {
58 if (!SCPreferencesCommitChanges(prefs)) {
59 switch (SCError()) {
60 case kSCStatusAccessError :
61 SCPrint(TRUE, stderr, CFSTR("Permission denied.\n"));
62 break;
63 default :
64 SCPrint(TRUE,
65 stdout,
66 CFSTR("SCPreferencesCommitChanges() failed: %s\n"),
67 SCErrorString(SCError()));
68 break;
69 }
70 exit (1);
71 }
72
73 if (!SCPreferencesApplyChanges(prefs)) {
74 SCPrint(TRUE,
75 stdout,
76 CFSTR("SCPreferencesApplyChanges() failed: %s\n"),
77 SCErrorString(SCError()));
78 exit (1);
79 }
80
81 return;
82 }
83
84
85 static CFStringRef
86 _copyStringFromSTDIN()
87 {
88 char buf[1024];
89 size_t len;
90 CFStringRef utf8;
91
92 if (fgets(buf, sizeof(buf), stdin) == NULL) {
93 return NULL;
94 }
95
96 len = strlen(buf);
97 if (buf[len-1] == '\n') {
98 buf[--len] = '\0';
99 }
100
101 utf8 = CFStringCreateWithBytes(NULL, buf, len, kCFStringEncodingUTF8, TRUE);
102 return utf8;
103 }
104
105
106 static void
107 get_ComputerName(int argc, char **argv)
108 {
109 CFStringEncoding encoding;
110 CFStringRef hostname;
111 CFDataRef utf8;
112
113 hostname = SCDynamicStoreCopyComputerName(NULL, &encoding);
114 if (!hostname) {
115 int sc_status = SCError();
116
117 switch (sc_status) {
118 case kSCStatusNoKey :
119 SCPrint(TRUE,
120 stderr,
121 CFSTR("ComputerName: not set\n"));
122 break;
123 default :
124 SCPrint(TRUE,
125 stderr,
126 CFSTR("SCDynamicStoreCopyComputerName() failed: %s\n"),
127 SCErrorString(SCError()));
128 break;
129 }
130 exit (1);
131 }
132
133 utf8 = CFStringCreateExternalRepresentation(NULL, hostname, kCFStringEncodingUTF8, 0);
134 if (!utf8) {
135 SCPrint(TRUE,
136 stderr,
137 CFSTR("ComputerName: could not convert to external representation\n"));
138 exit(1);
139 }
140 SCPrint(TRUE, stdout, CFSTR("%.*s\n"), CFDataGetLength(utf8), CFDataGetBytePtr(utf8));
141
142 CFRelease(utf8);
143 CFRelease(hostname);
144 exit(0);
145 }
146
147
148 static void
149 set_ComputerName(int argc, char **argv)
150 {
151 CFStringEncoding encoding;
152 CFStringRef hostname;
153 SCPreferencesRef prefs;
154
155 if (argc == 0) {
156 hostname = _copyStringFromSTDIN();
157 encoding = kCFStringEncodingUTF8;
158 } else {
159 hostname = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingASCII);
160 encoding = kCFStringEncodingASCII;
161 }
162
163 prefs = _open();
164 if (!SCPreferencesSetComputerName(prefs, hostname, encoding)) {
165 SCPrint(TRUE,
166 stdout,
167 CFSTR("SCPreferencesSetComputerName() failed: %s\n"),
168 SCErrorString(SCError()));
169 exit (1);
170 }
171 _save(prefs);
172 CFRelease(prefs);
173 CFRelease(hostname);
174 exit(0);
175 }
176
177
178 static void
179 get_LocalHostName(int argc, char **argv)
180 {
181 CFStringRef hostname;
182
183 hostname = SCDynamicStoreCopyLocalHostName(NULL);
184 if (!hostname) {
185 int sc_status = SCError();
186
187 switch (sc_status) {
188 case kSCStatusNoKey :
189 SCPrint(TRUE,
190 stderr,
191 CFSTR("LocalHostName: not set\n"));
192 break;
193 default :
194 SCPrint(TRUE,
195 stderr,
196 CFSTR("SCDynamicStoreCopyLocalHostName() failed: %s\n"),
197 SCErrorString(SCError()));
198 break;
199 }
200 exit (1);
201 }
202
203 SCPrint(TRUE, stdout, CFSTR("%@\n"), hostname);
204 CFRelease(hostname);
205 exit(0);
206 }
207
208
209 static void
210 set_LocalHostName(int argc, char **argv)
211 {
212 CFStringRef hostname = NULL;
213 SCPreferencesRef prefs;
214
215 if (argc == 0) {
216 hostname = _copyStringFromSTDIN();
217 } else {
218 hostname = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingASCII);
219 }
220
221 prefs = _open();
222 if (!SCPreferencesSetLocalHostName(prefs, hostname)) {
223 SCPrint(TRUE,
224 stderr,
225 CFSTR("SCPreferencesSetLocalHostName() failed: %s\n"),
226 SCErrorString(SCError()));
227 exit (1);
228 }
229 _save(prefs);
230 CFRelease(prefs);
231 CFRelease(hostname);
232 exit(0);
233 }
234
235
236 typedef void (*pref_func) (int argc, char **argv);
237
238 static struct {
239 char *pref;
240 pref_func get;
241 pref_func set;
242 } prefs[] = {
243 { "ComputerName", get_ComputerName, set_ComputerName },
244 { "LocalHostName", get_LocalHostName, set_LocalHostName }
245 };
246 #define N_PREFS (sizeof(prefs) / sizeof(prefs[0]))
247
248
249 int
250 findPref(char *pref)
251 {
252 int i;
253
254 for (i = 0; i < (int)N_PREFS; i++) {
255 if (strcmp(pref, prefs[i].pref) == 0) {
256 return i;
257 }
258 }
259
260 return -1;
261 }
262
263
264 void
265 do_getPref(char *pref, int argc, char **argv)
266 {
267 int i;
268
269 i = findPref(pref);
270 if (i >= 0) {
271 (*prefs[i].get)(argc, argv);
272 }
273 return;
274 }
275
276
277 void
278 do_setPref(char *pref, int argc, char **argv)
279 {
280 int i;
281
282 i = findPref(pref);
283 if (i >= 0) {
284 (*prefs[i].set)(argc, argv);
285 }
286 return;
287 }