struct utname结构体使用

struct utname 用于获取系统相关信息

#include 
extern int uname (struct utsname *__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
  };

实例:

#include 
#include 
#include 

int main()
{
    struct utsname uinfo;
    int ret=0;

    ret=uname(&uinfo);
    if(ret<0)
    {
        fprintf(stderr,"uname");
        return -1;
    }else
    {
        printf(" sysname:%s\n nodename:%s\n release:%s\n version:%s\n machine:%s\n \n ", uinfo.sysname, uinfo.nodename, uinfo.release, uinfo.version, uinfo.machine);
#if _UTSNAME_DOMAIN_LENGTH - 0
# ifdef __USE_GNU
    printf(" domainame:%s\n ",uinfo.domainname);
    //char domainname[_UTSNAME_DOMAIN_LENGTH]; //当前域名
# else
    printf(" __domainame:%s\n ",uinfo.__domainname);
    //char __domainname[_UTSNAME_DOMAIN_LENGTH];
# endif
#endif

    }
return 0;
}

执行结果;

 sysname:Linux
 nodename:backup
 release:2.6.9-89.EL
 version:#1 Mon Apr 20 10:23:08 EDT 2009
 machine:i686

  domainame:(none)


你可能感兴趣的:(linux学习)