【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。失敗返回-1,errno被設為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;
}