]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCDHostName.c
configd-24.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCDHostName.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23 #include <SystemConfiguration/SystemConfiguration.h>
24
25
26 CFStringRef
27 SCDKeyCreateHostName()
28 {
29 return SCDKeyCreate(CFSTR("%@/%@"),
30 kSCCacheDomainSetup,
31 kSCCompSystem);
32 }
33
34
35 SCDStatus
36 SCDHostNameGet(CFStringRef *name, CFStringEncoding *nameEncoding)
37 {
38 CFDictionaryRef dict;
39 SCDHandleRef handle = NULL;
40 CFStringRef key;
41 SCDSessionRef session = NULL;
42 SCDStatus status;
43
44 if (name == NULL) {
45 return SCD_FAILED;
46 }
47
48 /* get current user */
49 status = SCDOpen(&session, CFSTR("SCDHostNameGet"));
50 if (status != SCD_OK) {
51 goto done;
52 }
53
54 key = SCDKeyCreateHostName();
55 status = SCDGet(session, key, &handle);
56 CFRelease(key);
57 if (status != SCD_OK) {
58 goto done;
59 }
60
61 dict = SCDHandleGetData(handle);
62
63 *name = CFDictionaryGetValue(dict, kSCPropSystemComputerName);
64 if (*name == NULL) {
65 goto done;
66 }
67 CFRetain(*name);
68
69 if (nameEncoding) {
70 CFNumberRef num;
71
72 num = CFDictionaryGetValue(dict,
73 kSCPropSystemComputerNameEncoding);
74 if (num) {
75 CFNumberGetValue(num, kCFNumberIntType, nameEncoding);
76 } else {
77 *nameEncoding = CFStringGetSystemEncoding();
78 }
79 }
80
81 done :
82
83 if (handle) SCDHandleRelease(handle);
84 if (session) (void) SCDClose(&session);
85 return status;
86 }