Friday, October 19, 2012

C program for getting Linux system information


In most of the Linux systems we use the command “uname” to print the system information in the terminal.
Ex:
           $uname -a

which gives the information about the system such as OS name, kernel version, release,domain name etc... Similerly we can get that information through C Programming.

Now I will let you how to print Linux system information through C programming in Linux, by using the structure “struct utsname”, and the system call uname() which return 0 on success, -1 on error.. See the code snnippet given below...

uname.c:

#include<stdio.h>
#include<sys/utsname.h>

int main()
{
int ret;
struct utsname buf;
ret = uname(&buf);
if(!ret) {
printf("OperatingSystem name: %s\n",buf.sysname);
printf("Node(Host) name: %s\n",buf.nodename);
printf("Kernel Release Version: %s\n",buf.release);
printf("OS Version: %s\n",buf.version);
printf("Hardware: %s\n",buf.machine);
}
else {
printf("Error in the System call");
return -1;
}
return 0;
}



OUTPUT:
OperatingSystem name: Linux
Node(Host) name: SAI-LAP
Kernel Release Version: 3.2.0-32-generic-pae
OS Version: #51-Ubuntu SMP Wed Sep 26 21:54:23 UTC 2012
Hardware: i686


Note: We can also access these details through /proc/sys/kernel directory, for example

         $cat /proc/sys/kernel/ostype

which gives Operating system type...similerly we can get osrelease,hostname,version,domainname (replace “ostype” with any of these).


2 comments:

  1. Great work. I am highly obliged that you are sharing such info with us. I would appreciate if you will post on daily basis and that too with good typical info which i didn't get from anywhere. Thanks to your info I am enrolling in http://www.wiziq.com/course/6314-learn-c-programming-language-low-priced-student-edition, I really appreciate your work

    ReplyDelete