2 * Copyright (c) 2003-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * May 29, 2003 Allan Nathanson <ajn@apple.com>
34 #include <SCPreferencesSetSpecific.h>
37 static SCPreferencesRef
40 prefs
= SCPreferencesCreate(NULL
, CFSTR("scutil"), NULL
);
44 CFSTR("SCPreferencesCreate() failed: %s\n"),
45 SCErrorString(SCError()));
54 _save(SCPreferencesRef prefs
)
56 if (!SCPreferencesCommitChanges(prefs
)) {
58 case kSCStatusAccessError
:
59 SCPrint(TRUE
, stderr
, CFSTR("Permission denied.\n"));
64 CFSTR("SCPreferencesCommitChanges() failed: %s\n"),
65 SCErrorString(SCError()));
71 if (!SCPreferencesApplyChanges(prefs
)) {
74 CFSTR("SCPreferencesApplyChanges() failed: %s\n"),
75 SCErrorString(SCError()));
84 _copyStringFromSTDIN()
90 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
) {
95 if (buf
[len
-1] == '\n') {
99 utf8
= CFStringCreateWithBytes(NULL
, buf
, len
, kCFStringEncodingUTF8
, TRUE
);
105 get_ComputerName(int argc
, char **argv
)
107 CFStringEncoding encoding
;
108 CFStringRef hostname
;
111 hostname
= SCDynamicStoreCopyComputerName(NULL
, &encoding
);
113 int sc_status
= SCError();
116 case kSCStatusNoKey
:
119 CFSTR("ComputerName: not set\n"));
124 CFSTR("SCDynamicStoreCopyComputerName() failed: %s\n"),
125 SCErrorString(SCError()));
131 utf8
= CFStringCreateExternalRepresentation(NULL
, hostname
, kCFStringEncodingUTF8
, 0);
135 CFSTR("ComputerName: could not convert to external representation\n"));
138 SCPrint(TRUE
, stdout
, CFSTR("%.*s\n"), CFDataGetLength(utf8
), CFDataGetBytePtr(utf8
));
147 set_ComputerName(int argc
, char **argv
)
149 CFStringEncoding encoding
;
150 CFStringRef hostname
;
153 hostname
= _copyStringFromSTDIN();
154 encoding
= kCFStringEncodingUTF8
;
156 hostname
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingASCII
);
157 encoding
= kCFStringEncodingASCII
;
161 if (!SCPreferencesSetComputerName(prefs
, hostname
, encoding
)) {
164 CFSTR("SCPreferencesSetComputerName() failed: %s\n"),
165 SCErrorString(SCError()));
176 get_HostName(int argc
, char **argv
)
178 CFStringRef hostname
;
181 hostname
= SCPreferencesGetHostName(prefs
);
182 if (hostname
== NULL
) {
183 int sc_status
= SCError();
186 case kSCStatusNoKey
:
189 CFSTR("HostName: not set\n"));
194 CFSTR("SCPreferencesGetHostName() failed: %s\n"),
195 SCErrorString(SCError()));
202 SCPrint(TRUE
, stdout
, CFSTR("%@\n"), hostname
);
209 set_HostName(int argc
, char **argv
)
211 CFStringRef hostname
= NULL
;
214 hostname
= _copyStringFromSTDIN();
216 hostname
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingASCII
);
220 if (!SCPreferencesSetHostName(prefs
, hostname
)) {
223 CFSTR("SCPreferencesSetHostName() failed: %s\n"),
224 SCErrorString(SCError()));
237 get_LocalHostName(int argc
, char **argv
)
239 CFStringRef hostname
;
241 hostname
= SCDynamicStoreCopyLocalHostName(NULL
);
243 int sc_status
= SCError();
246 case kSCStatusNoKey
:
249 CFSTR("LocalHostName: not set\n"));
254 CFSTR("SCDynamicStoreCopyLocalHostName() failed: %s\n"),
255 SCErrorString(SCError()));
261 SCPrint(TRUE
, stdout
, CFSTR("%@\n"), hostname
);
268 set_LocalHostName(int argc
, char **argv
)
270 CFStringRef hostname
= NULL
;
273 hostname
= _copyStringFromSTDIN();
275 hostname
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingASCII
);
279 if (!SCPreferencesSetLocalHostName(prefs
, hostname
)) {
282 CFSTR("SCPreferencesSetLocalHostName() failed: %s\n"),
283 SCErrorString(SCError()));
293 typedef void (*pref_func
) (int argc
, char **argv
);
295 static const struct {
300 { "ComputerName", get_ComputerName
, set_ComputerName
},
301 { "HostName", get_HostName
, set_HostName
},
302 { "LocalHostName", get_LocalHostName
, set_LocalHostName
}
304 #define N_PREF_KEYS (sizeof(pref_keys) / sizeof(pref_keys[0]))
313 for (i
= 0; i
< (int)N_PREF_KEYS
; i
++) {
314 if (strcmp(pref
, pref_keys
[i
].pref
) == 0) {
325 do_getPref(char *pref
, int argc
, char **argv
)
331 (*pref_keys
[i
].get
)(argc
, argv
);
339 do_setPref(char *pref
, int argc
, char **argv
)
345 (*pref_keys
[i
].set
)(argc
, argv
);