以下是例程。编译后只需要调用就可以了。程序很简单,看一下就知道uname()如何使用了。其中,涉及到一个utsname的结构。先解释一下:
struct utsname{
char sysname[_UTS_NAMESIZE]; /* name of this operating system */
char nodename[_UTS_NODESIZE]; /* name of this node */
char release[_UTS_NAMESIZE]; /* O.S release level */
char version[_UTS_NAMESIZE]; /* O.S version level */
char machine[_UTS_NAMESIZE]; /* hardware type */
};
#include <sys/utsname.h>
#include <netdb.h>
#include <sys/socket.h>
int main(int argc, char **argv)
{
struct hostent *hptr;
struct utsname myname;
char **pptr;
char str[32];
if(uname(&myname)<0)
{
printf("uname calling error\n");
return 1;
}
printf("localhost name:%s\n",myname.nodename);
if( (hptr = gethostbyname(myname.nodename)) == NULL)
{
printf("gethostbyname calling error\n");
return 1;
}
pptr=hptr->h_addr_list;
for(;*pptr!=NULL;pptr++)
printf(" address:%s\n", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
return 0;
}