gethostname.c revision 288029
1241675Suqs/* 2241675Suqs * Copyright (c) 1989, 1993 3241675Suqs * The Regents of the University of California. All rights reserved. 4241675Suqs * 5241675Suqs * Redistribution and use in source and binary forms, with or without 6241675Suqs * modification, are permitted provided that the following conditions 7241675Suqs * are met: 8241675Suqs * 1. Redistributions of source code must retain the above copyright 9241675Suqs * notice, this list of conditions and the following disclaimer. 10241675Suqs * 2. Redistributions in binary form must reproduce the above copyright 11241675Suqs * notice, this list of conditions and the following disclaimer in the 12241675Suqs * documentation and/or other materials provided with the distribution. 13241675Suqs * 4. Neither the name of the University nor the names of its contributors 14241675Suqs * may be used to endorse or promote products derived from this software 15241675Suqs * without specific prior written permission. 16241675Suqs * 17241675Suqs * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18241675Suqs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19241675Suqs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20241675Suqs * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21241675Suqs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22241675Suqs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23241675Suqs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24241675Suqs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25241675Suqs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26241675Suqs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27241675Suqs * SUCH DAMAGE. 28241675Suqs */ 29241675Suqs 30241675Suqs#if defined(LIBC_SCCS) && !defined(lint) 31241675Suqsstatic char sccsid[] = "@(#)gethostname.c 8.1 (Berkeley) 6/4/93"; 32241675Suqs#endif /* LIBC_SCCS and not lint */ 33241675Suqs#include <sys/cdefs.h> 34241675Suqs__FBSDID("$FreeBSD: head/lib/libc/gen/gethostname.c 288029 2015-09-20 20:23:16Z rodrigc $"); 35241675Suqs 36241675Suqs#include <sys/param.h> 37241675Suqs#include <sys/sysctl.h> 38241675Suqs 39241675Suqs#include <errno.h> 40241675Suqs#include <unistd.h> 41241675Suqs 42241675Suqsint 43241675Suqsgethostname(char *name, size_t namelen) 44241675Suqs{ 45241675Suqs int mib[2]; 46241675Suqs 47241675Suqs mib[0] = CTL_KERN; 48241675Suqs mib[1] = KERN_HOSTNAME; 49241675Suqs if (sysctl(mib, 2, name, &namelen, NULL, 0) == -1) { 50241675Suqs if (errno == ENOMEM) 51241675Suqs errno = ENAMETOOLONG; 52241675Suqs return (-1); 53241675Suqs } 54241675Suqs return (0); 55241675Suqs} 56241675Suqs