xref: /netbsd-src/lib/libc/gen/gethostname.c (revision 9e66e6d75e9910b3de5f4ef031995955f69a7dd1)
1*9e66e6d7Sabs /*	$NetBSD: gethostname.c,v 1.13 2012/06/25 22:32:43 abs Exp $	*/
2b585e843Scgd 
3060d0e3dScgd /*
4060d0e3dScgd  * Copyright (c) 1989, 1993
5060d0e3dScgd  *	The Regents of the University of California.  All rights reserved.
6060d0e3dScgd  *
7060d0e3dScgd  * Redistribution and use in source and binary forms, with or without
8060d0e3dScgd  * modification, are permitted provided that the following conditions
9060d0e3dScgd  * are met:
10060d0e3dScgd  * 1. Redistributions of source code must retain the above copyright
11060d0e3dScgd  *    notice, this list of conditions and the following disclaimer.
12060d0e3dScgd  * 2. Redistributions in binary form must reproduce the above copyright
13060d0e3dScgd  *    notice, this list of conditions and the following disclaimer in the
14060d0e3dScgd  *    documentation and/or other materials provided with the distribution.
15eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
16060d0e3dScgd  *    may be used to endorse or promote products derived from this software
17060d0e3dScgd  *    without specific prior written permission.
18060d0e3dScgd  *
19060d0e3dScgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20060d0e3dScgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21060d0e3dScgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22060d0e3dScgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23060d0e3dScgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24060d0e3dScgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25060d0e3dScgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26060d0e3dScgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27060d0e3dScgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28060d0e3dScgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29060d0e3dScgd  * SUCH DAMAGE.
30060d0e3dScgd  */
31060d0e3dScgd 
326f658062Schristos #include <sys/cdefs.h>
33060d0e3dScgd #if defined(LIBC_SCCS) && !defined(lint)
34b585e843Scgd #if 0
35060d0e3dScgd static char sccsid[] = "@(#)gethostname.c	8.1 (Berkeley) 6/4/93";
36b585e843Scgd #else
37*9e66e6d7Sabs __RCSID("$NetBSD: gethostname.c,v 1.13 2012/06/25 22:32:43 abs Exp $");
38b585e843Scgd #endif
39060d0e3dScgd #endif /* LIBC_SCCS and not lint */
40060d0e3dScgd 
4143fa6fe3Sjtc #include "namespace.h"
42060d0e3dScgd #include <sys/param.h>
43060d0e3dScgd #include <sys/sysctl.h>
44b48252f3Slukem 
45b48252f3Slukem #include <assert.h>
46b48252f3Slukem #include <errno.h>
476f658062Schristos #include <unistd.h>
48060d0e3dScgd 
4943fa6fe3Sjtc #ifdef __weak_alias
__weak_alias(gethostname,_gethostname)5060549036Smycroft __weak_alias(gethostname,_gethostname)
5143fa6fe3Sjtc #endif
5243fa6fe3Sjtc 
539c87841aSjtc int
54*9e66e6d7Sabs gethostname(char *name, size_t namelen)
55060d0e3dScgd {
56060d0e3dScgd 	int mib[2];
57060d0e3dScgd 	size_t size;
58060d0e3dScgd 
59b48252f3Slukem 	_DIAGASSERT(name != NULL);
60b48252f3Slukem 
61060d0e3dScgd 	mib[0] = CTL_KERN;
62060d0e3dScgd 	mib[1] = KERN_HOSTNAME;
63060d0e3dScgd 	size = namelen;
64641784aaSchristos 	if (sysctl(mib, 2, name, &size, NULL, 0) == -1)
65060d0e3dScgd 		return (-1);
66641784aaSchristos 
67060d0e3dScgd 	return (0);
68060d0e3dScgd }
69