Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2414546
  • 博文数量: 298
  • 博客积分: 7876
  • 博客等级: 准将
  • 技术积分: 5500
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-23 13:39
文章存档

2013年(2)

2012年(142)

2011年(154)

分类: LINUX

2011-04-14 09:51:53

Function : uname()

Header in #include

theory of functionsextern int uname (struct utsname *__name) __THROW;

return 0 if success, return -1 if failed, when errno is setted EFAULT, it means buffer is invalid
you can use “man uname” to know it
in detail

 

Argument __name store system information.

struct utsname
  { char sysname[_UTSNAME_SYSNAME_LENGTH];// current operating system name
   char nodename[_UTSNAME_NODENAME_LENGTH];//the name of the network
   char release[_UTSNAME_RELEASE_LENGTH];//the curren release level
   char version[_UTSNAME_VERSION_LENGTH];//the current release
   char machine[_UTSNAME_MACHINE_LENGTH];//types of current hardware systems
#if _UTSNAME_DOMAIN_LENGTH - 0
    /* Name of the domain of this node on the network.  */
# ifdef __USE_GNU
    char domainname[_UTSNAME_DOMAIN_LENGTH]; //current domain’s name
# else
    char __domainname[_UTSNAME_DOMAIN_LENGTH];
# endif
#endif
  };


for example

#include
#include
#include

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 [_UTSNAME_DOMAIN_LENGTH]; //current
# else
    printf(" __domainame:%s\n ",testbuff.__domainname);
    //char __domainname[_UTSNAME_DOMAIN_LENGTH];
# endif
#endif

    }
return 0;
}

 

for my machine : the program is to test byte order

#include    "unp.h"

#include   

int

main(int argc, char **argv)

{

    struct utsname testbuff;

    int fb=0;

    union {

      short  s;

      char   c[sizeof(short)];

    } un;

 

   fb=uname(&testbuff);

    if(fb<0)

    {

        perror("uname");

        return 0;

    }

    un.s = 0x0102;

    //printf("%s: ", CPU_VENDOR_OS);

   printf(" sysname:%s\n nodename:%s\n release:%s\n version:%s\n machine:%s\n ",\

                    testbuff.sysname,\

                    testbuff.nodename,\

                    testbuff.release,\

                    testbuff.version,\

                    testbuff.machine);

    if (sizeof(short) == 2) {

        if (un.c[0] == 1 && un.c[1] == 2)

            printf("big-endian\n");

        else if (un.c[0] == 2 && un.c[1] == 1)

            printf("little-endian\n");

        else

            printf("unknown\n");

    } else

        printf("sizeof(short) = %d\n", sizeof(short));

 

    exit(0);

}

Result on my machine

sysname:Linux

 nodename:localhost.localdomain

 release:2.6.18-194.el5

 version:#1 SMP Tue Mar 16 21:52:43 EDT 2010

 machine:i686

 little-endian

 

阅读(1683) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~