2 * Copyright (c) 2003 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 SCPreferencesRef prefs
;
42 prefs
= SCPreferencesCreate(NULL
, CFSTR("scutil"), NULL
);
46 CFSTR("SCPreferencesCreate() failed: %s\n"),
47 SCErrorString(SCError()));
56 _save(SCPreferencesRef prefs
)
58 if (!SCPreferencesCommitChanges(prefs
)) {
60 case kSCStatusAccessError
:
61 SCPrint(TRUE
, stderr
, CFSTR("Permission denied.\n"));
66 CFSTR("SCPreferencesCommitChanges() failed: %s\n"),
67 SCErrorString(SCError()));
73 if (!SCPreferencesApplyChanges(prefs
)) {
76 CFSTR("SCPreferencesApplyChanges() failed: %s\n"),
77 SCErrorString(SCError()));
86 _copyStringFromSTDIN()
92 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
) {
97 if (buf
[len
-1] == '\n') {
101 utf8
= CFStringCreateWithBytes(NULL
, buf
, len
, kCFStringEncodingUTF8
, TRUE
);
107 get_ComputerName(int argc
, char **argv
)
109 CFStringEncoding encoding
;
110 CFStringRef hostname
;
113 hostname
= SCDynamicStoreCopyComputerName(NULL
, &encoding
);
115 int sc_status
= SCError();
118 case kSCStatusNoKey
:
121 CFSTR("ComputerName: not set\n"));
126 CFSTR("SCDynamicStoreCopyComputerName() failed: %s\n"),
127 SCErrorString(SCError()));
133 utf8
= CFStringCreateExternalRepresentation(NULL
, hostname
, kCFStringEncodingUTF8
, 0);
137 CFSTR("ComputerName: could not convert to external representation\n"));
140 SCPrint(TRUE
, stdout
, CFSTR("%.*s\n"), CFDataGetLength(utf8
), CFDataGetBytePtr(utf8
));
149 set_ComputerName(int argc
, char **argv
)
151 CFStringEncoding encoding
;
152 CFStringRef hostname
;
153 SCPreferencesRef prefs
;
156 hostname
= _copyStringFromSTDIN();
157 encoding
= kCFStringEncodingUTF8
;
159 hostname
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingASCII
);
160 encoding
= kCFStringEncodingASCII
;
164 if (!SCPreferencesSetComputerName(prefs
, hostname
, encoding
)) {
167 CFSTR("SCPreferencesSetComputerName() failed: %s\n"),
168 SCErrorString(SCError()));
179 get_LocalHostName(int argc
, char **argv
)
181 CFStringRef hostname
;
183 hostname
= SCDynamicStoreCopyLocalHostName(NULL
);
185 int sc_status
= SCError();
188 case kSCStatusNoKey
:
191 CFSTR("LocalHostName: not set\n"));
196 CFSTR("SCDynamicStoreCopyLocalHostName() failed: %s\n"),
197 SCErrorString(SCError()));
203 SCPrint(TRUE
, stdout
, CFSTR("%@\n"), hostname
);
210 set_LocalHostName(int argc
, char **argv
)
212 CFStringRef hostname
= NULL
;
213 SCPreferencesRef prefs
;
216 hostname
= _copyStringFromSTDIN();
218 hostname
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingASCII
);
222 if (!SCPreferencesSetLocalHostName(prefs
, hostname
)) {
225 CFSTR("SCPreferencesSetLocalHostName() failed: %s\n"),
226 SCErrorString(SCError()));
236 typedef void (*pref_func
) (int argc
, char **argv
);
243 { "ComputerName", get_ComputerName
, set_ComputerName
},
244 { "LocalHostName", get_LocalHostName
, set_LocalHostName
}
246 #define N_PREFS (sizeof(prefs) / sizeof(prefs[0]))
254 for (i
= 0; i
< (int)N_PREFS
; i
++) {
255 if (strcmp(pref
, prefs
[i
].pref
) == 0) {
265 do_getPref(char *pref
, int argc
, char **argv
)
271 (*prefs
[i
].get
)(argc
, argv
);
278 do_setPref(char *pref
, int argc
, char **argv
)
284 (*prefs
[i
].set
)(argc
, argv
);