C how to use the function uname

En_t8 picture En_t8 · Aug 29, 2010 · Viewed 32.1k times · Source

I should write a function to get some information about the system (the most important information is the the architecture). I found the function uname which can be used including sys/utsname.h. Well, though I googled and I read the documentation, I couldn't find any example of the function and I don't understand how to use uname. Anyone can explain me how to use it? it would be great if you can write an example, too. Thanks in advance.

Answer

Amber picture Amber · Aug 29, 2010

First, include the header:

#include <sys/utsname.h>

Then, define a utsname structure:

struct utsname unameData;

Then, call uname() with a pointer to the struct:

uname(&unameData); // Might check return value here (non-0 = failure)

After this, the struct will contain the info you want:

printf("%s", unameData.sysname);

http://opengroup.org/onlinepubs/007908775/xsh/sysutsname.h.html