xref: /netbsd-src/lib/libc/gen/getdomainname.c (revision 9e66e6d75e9910b3de5f4ef031995955f69a7dd1)
1*9e66e6d7Sabs /*	$NetBSD: getdomainname.c,v 1.13 2012/06/25 22:32:43 abs Exp $	*/
2b585e843Scgd 
33580a0daScgd /*
43580a0daScgd  * Copyright (c) 1989, 1993
53580a0daScgd  *	The Regents of the University of California.  All rights reserved.
63580a0daScgd  *
73580a0daScgd  * Redistribution and use in source and binary forms, with or without
83580a0daScgd  * modification, are permitted provided that the following conditions
93580a0daScgd  * are met:
103580a0daScgd  * 1. Redistributions of source code must retain the above copyright
113580a0daScgd  *    notice, this list of conditions and the following disclaimer.
123580a0daScgd  * 2. Redistributions in binary form must reproduce the above copyright
133580a0daScgd  *    notice, this list of conditions and the following disclaimer in the
143580a0daScgd  *    documentation and/or other materials provided with the distribution.
15eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
163580a0daScgd  *    may be used to endorse or promote products derived from this software
173580a0daScgd  *    without specific prior written permission.
183580a0daScgd  *
193580a0daScgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
203580a0daScgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
213580a0daScgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
223580a0daScgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
233580a0daScgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
243580a0daScgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
253580a0daScgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
263580a0daScgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
273580a0daScgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
283580a0daScgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
293580a0daScgd  * SUCH DAMAGE.
303580a0daScgd  */
313580a0daScgd 
3226cc2d4fSchristos #include <sys/cdefs.h>
333580a0daScgd #if defined(LIBC_SCCS) && !defined(lint)
34b585e843Scgd #if 0
35b585e843Scgd static char sccsid[] = "@(#)gethostname.c	8.1 (Berkeley) 6/4/93";
36b585e843Scgd #else
37*9e66e6d7Sabs __RCSID("$NetBSD: getdomainname.c,v 1.13 2012/06/25 22:32:43 abs Exp $");
38b585e843Scgd #endif
393580a0daScgd #endif /* LIBC_SCCS and not lint */
403580a0daScgd 
4143fa6fe3Sjtc #include "namespace.h"
423580a0daScgd #include <sys/param.h>
433580a0daScgd #include <sys/sysctl.h>
44b48252f3Slukem 
45b48252f3Slukem #include <assert.h>
46b48252f3Slukem #include <errno.h>
4726cc2d4fSchristos #include <unistd.h>
483580a0daScgd 
49a01bbaa1Skleink #ifdef __weak_alias
__weak_alias(getdomainname,_getdomainname)5060549036Smycroft __weak_alias(getdomainname,_getdomainname)
51a01bbaa1Skleink #endif
52a01bbaa1Skleink 
539c87841aSjtc int
54*9e66e6d7Sabs getdomainname(char *name, size_t namelen)
553580a0daScgd {
563580a0daScgd 	int mib[2];
573580a0daScgd 	size_t size;
5842f95dd8Sis 	int olderrno;
593580a0daScgd 
60b48252f3Slukem 	_DIAGASSERT(name != NULL);
61b48252f3Slukem 
623580a0daScgd 	mib[0] = CTL_KERN;
633580a0daScgd 	mib[1] = KERN_DOMAINNAME;
643580a0daScgd 	size = namelen;
6542f95dd8Sis 	olderrno = errno;
6642f95dd8Sis 	if (sysctl(mib, 2, name, &size, NULL, 0) == -1) {
6742f95dd8Sis 		if (errno == ENOMEM) {
6842f95dd8Sis 			errno = olderrno;
6942f95dd8Sis 			return (0);
7042f95dd8Sis 		}
713580a0daScgd 		return (-1);
7242f95dd8Sis 	}
7342f95dd8Sis 
743580a0daScgd 	return (0);
753580a0daScgd }
76