Tuesday, August 23, 2011

socket uname function sample c code


A simple program to permit you to test the values returned by uname(2) is shown in Listing below This program invokes uname(2) and then displays the contents of the information it has returned
in the structure utsname.

Example
 /* uname.c:
  *
  * Example of uname(2):
  */
  #include <stdio.h>
  #include <unistd.h>
  #include <stdlib.h>
  #include <errno.h>
  #include <string.h>
  #include <sys/utsname.h>

  int main(int argc,char **argv) {
     int z;
     struct utsname u_name;

     z = uname(&u_name);

     if ( z == -1 ) {
        fprintf(stderr, "%s: uname(2)\n",
        strerror(errno));
        exit(1);
     }

     printf(" sysname[]   = '%s';\n", u_name.sysname);
     printf(" nodename[]  = '%s';\n", u_name.nodename);
     printf(" release[]   = '%s';\n", u_name.release);
     printf(" version[]   = '%s';\n", u_name.version);
     printf(" machine[]   = '%s';\n", u_name.machine);
    
     return 0;
  }
/*
 * OUTPUT
 *
 [sgupta@rhel55x64 socket]$ gcc uname.c –o uname
 [sgupta@rhel55x64 socket]$ ./uname
 sysname[]  = 'Linux';
 nodename[] = 'rhel55x64';
 release[]  = '2.6.18-164.el5';
 version[]  = '#1 SMP Tue Aug 18 15:51:48 EDT 2009';
 machine[]  = 'x86_64';
 [sgupta@ rhel55x64 socket]$
*/

No comments:

Post a Comment