C语言 C如何使用函数uname
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3596310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C how to use the function uname
提问by En_t8
I should write a function to get some information about the system (the most important information is the the architecture). I found the function unamewhich 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.
我应该编写一个函数来获取有关系统的一些信息(最重要的信息是架构)。我找到了可以使用的函数uname,包括sys/utsname.h。好吧,虽然我用谷歌搜索并阅读了文档,但我找不到该函数的任何示例,而且我不明白如何使用 uname。任何人都可以解释我如何使用它?如果你也能写一个例子就太好了。提前致谢。
回答by Amber
First, include the header:
首先,包含标题:
#include <sys/utsname.h>
Then, define a utsname structure:
然后,定义一个 utsname 结构:
struct utsname unameData;
Then, call uname() with a pointer to the struct:
然后,使用指向结构的指针调用 uname():
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
http://opengroup.org/onlinepubs/007908775/xsh/sysutsname.h.html
回答by tupiniquim
A fully working example is worth a thousand words. ;-)
一个完整的例子值一千字。;-)
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/utsname.h>
int main(void) {
struct utsname buffer;
errno = 0;
if (uname(&buffer) != 0) {
perror("uname");
exit(EXIT_FAILURE);
}
printf("system name = %s\n", buffer.sysname);
printf("node name = %s\n", buffer.nodename);
printf("release = %s\n", buffer.release);
printf("version = %s\n", buffer.version);
printf("machine = %s\n", buffer.machine);
#ifdef _GNU_SOURCE
printf("domain name = %s\n", buffer.domainname);
#endif
return EXIT_SUCCESS;
}
回答by jkerian
From the documentation, it looks like you'd use it like so:
从文档中,看起来你会像这样使用它:
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);
回答by kennytm
The uname()function takes a pointer to the utsnamestructure that will store the result as input. Therefore, just make a temporary utsnameinstance, pass the address of it to uname, and read the content of this struct after the function succeed.
该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...
}

