gethostname.c revision 158807
11553Srgrimes/* 21553Srgrimes * Copyright (c) 1989, 1993 31553Srgrimes * The Regents of the University of California. All rights reserved. 41553Srgrimes * 51553Srgrimes * Redistribution and use in source and binary forms, with or without 61553Srgrimes * modification, are permitted provided that the following conditions 71553Srgrimes * are met: 81553Srgrimes * 1. Redistributions of source code must retain the above copyright 91553Srgrimes * notice, this list of conditions and the following disclaimer. 101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111553Srgrimes * notice, this list of conditions and the following disclaimer in the 121553Srgrimes * documentation and/or other materials provided with the distribution. 131553Srgrimes * 3. All advertising materials mentioning features or use of this software 141553Srgrimes * must display the following acknowledgement: 151553Srgrimes * This product includes software developed by the University of 161553Srgrimes * California, Berkeley and its contributors. 171553Srgrimes * 4. Neither the name of the University nor the names of its contributors 181553Srgrimes * may be used to endorse or promote products derived from this software 191553Srgrimes * without specific prior written permission. 201553Srgrimes * 211553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241553Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3152007Speter * SUCH DAMAGE. 321553Srgrimes */ 331553Srgrimes 3479607Sdd#if defined(LIBC_SCCS) && !defined(lint) 351553Srgrimesstatic char sccsid[] = "@(#)gethostname.c 8.1 (Berkeley) 6/4/93"; 366494Sbde#endif /* LIBC_SCCS and not lint */ 3716073Sphk#include <sys/cdefs.h> 381553Srgrimes__FBSDID("$FreeBSD: head/lib/libc/gen/gethostname.c 158807 2006-05-22 05:04:53Z ache $"); 391553Srgrimes 4045775Speter#include <sys/param.h> 4145775Speter#include <sys/sysctl.h> 421553Srgrimes 4379607Sdd#include <errno.h> 4479607Sdd#include <unistd.h> 4579607Sdd 4679607Sddint 4779607Sddgethostname(name, namelen) 4879607Sdd char *name; 4979607Sdd size_t namelen; 5079607Sdd{ 5179607Sdd int mib[2]; 5279607Sdd 5379607Sdd mib[0] = CTL_KERN; 5479607Sdd mib[1] = KERN_HOSTNAME; 5579607Sdd if (sysctl(mib, 2, name, &namelen, NULL, 0) == -1) { 561553Srgrimes if (errno == ENOMEM) 571553Srgrimes errno = ENAMETOOLONG; 581553Srgrimes return (-1); 591553Srgrimes } 6072684Speter return (0); 611553Srgrimes} 621553Srgrimes