C how to use the function uname

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.

14
задан En_t8 29 August 2010 в 19:48
поделиться

3 ответа

Сначала включите заголовок:

#include <sys/utsname.h>

Затем определите структуру utsname:

struct utsname unameData;

Затем вызовите uname() с указателем на структуру:

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

После этого структура будет содержать нужную информацию:

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

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

25
ответ дан 1 December 2019 в 06:07
поделиться

Судя по документации, вы должны использовать его следующим образом:

struct utsname my_uname;
if(uname(&my_uname) == -1)
   printf("uname call failed!");
else
   printf("System name: %s\nNodename:%s\nRelease:%s\nVersion:%s\nMachine:%s\n",
       my_uname.sysname, my_uname.nodename, my_uname.release,my_uname.version,my_uname.machine);
9
ответ дан 1 December 2019 в 06:07
поделиться

Функция uname() принимает указатель на структуру utsname, которая будет хранить результат в качестве входных данных. Поэтому просто создайте временный экземпляр utsname, передайте его адрес в uname и прочитайте содержимое этой структуры после успешного выполнения функции.

struct utsname retval;
if(uname(&retval) < 0) {     // <----
  perror("Failed to uname");
  // error handling...
} else {
  printf("System name = %s\n", retval.sysname);
  // print other info....
  // see http://www.opengroup.org/onlinepubs/009695399/basedefs/sys/utsname.h.html
  //   for other members...
}
6
ответ дан 1 December 2019 в 06:07
поделиться
Другие вопросы по тегам:

Похожие вопросы: