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