现在的位置: 首页 > 综合 > 正文

linux struct utsname 结构详解

2012年11月19日 ⁄ 综合 ⁄ 共 1752字 ⁄ 字号 评论关闭

uname系统调用】

功能描述:
获取当前内核名称和其它信息。
用法:
#include <sys/utsname.h>
extern int uname (struct utsname *__name) __THROW;
The <sys/utsname.h> header defines structure utsname, which includes atleast the following members:

char sysname[]  name of thisimplementation of the operating system

char nodename[] name of this node within an implementation-dependent communicationsnetwork

char release[]  current release levelof this implementation

char version[]  current version levelof this release

char machine[]  name of the hardwaretype on which the system is running

The character arrays are of unspecifiedsize, but the data stored in them is terminated by a null byte.

The following is declared as a functionand may also be defined as a macro.
参数:
__name
:指向存放系统信息的缓冲区,原型如下
struct utsname
{ char sysname[_UTSNAME_SYSNAME_LENGTH];//
当前操作系统名
char nodename[_UTSNAME_NODENAME_LENGTH];//
网络上的名称
char release[_UTSNAME_RELEASE_LENGTH];//
当前发布级别
char version[_UTSNAME_VERSION_LENGTH];//
当前发布版本
char machine[_UTSNAME_MACHINE_LENGTH];//
当前硬件体系类型
#if _UTSNAME_DOMAIN_LENGTH - 0
/* Name of the domain of this node on the network. */
# ifdef __USE_GNU
char domainname[_UTSNAME_DOMAIN_LENGTH]; //
当前域名
# else
char __domainname[_UTSNAME_DOMAIN_LENGTH];
# endif
#endif
};
返回说明:
成功执行时,返回0。失败返回-1errno被设为EFAULT,表示buf无效。
关于uname的具体用法可以使用“man uname”来查看。
实例如下:
#include <sys/utsname.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct utsname testbuff;
int fb=0;
fb=uname(&testbuff);
if(fb<0)
{
perror("uname");
return 0;
}else
{
printf(" sysname:%s/n nodename:%s/n release:%s/n version:%s/n machine:%s/n/n ",/
testbuff.sysname,/
testbuff.nodename,/
testbuff.release,/
testbuff.version,/
testbuff.machine);
#if _UTSNAME_DOMAIN_LENGTH - 0
# ifdef __USE_GNU
printf(" domainame:%s/n ",testbuff.domainname);
//char domainname[_UTSNAME_DOMAIN_LENGTH]; //
当前域名
# else
printf(" __domainame:%s/n ",testbuff.__domainname);
//char __domainname[_UTSNAME_DOMAIN_LENGTH];
# endif
#endif
}
return 0;
}

抱歉!评论已关闭.