gethostname.c

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "roken.h"

#ifndef HAVE_GETHOSTNAME

#ifdef HAVE_SYS_UTSNAME_H
#include <sys/utsname.h>
#endif

/**
 * Return the local host's name in "name", up to "namelen" characters.
 * "name" will be null-terminated if "namelen" is big enough.
 * The return code is 0 on success, -1 on failure.  (The calling
 * interface is identical to gethostname(2).)
 */

int ROKEN_LIB_FUNCTION
gethostname(char *name, int namelen)
{
#if defined(HAVE_UNAME)
    {
	struct utsname utsname;
	int ret;

	ret = uname (&utsname);
	if (ret < 0)
	    return ret;
	strlcpy (name, utsname.nodename, namelen);
	return 0;
    }
#else
    strlcpy (name, "some.random.host", namelen);
    return 0;
#endif
}

#endif /** GETHOSTNAME */

/*
utsname结构体在sys/utsname.h里面定义
struct utsname{
    char sysname[_SYS_NMLN]; 系统名称 uname -s
    char nodename[_SYS_NMLN]; 主机节点名称 uname -n
    char release[_SYS_NMLN]; 系统发布版本 uame -r
    char version[_SYS_NMLN]; 系统版本 uname -v
    char machine[_SYS_NMLN]; 系统硬件架构 uname -m
};
*/

你可能感兴趣的:(gethostname,netbsd)