来自APUE
#include <sys/utsname.h> int uname(struct utsname *name);
哪里
struct utsname { char sysname[]; /* name of the operating system */ char nodename[]; /* name of this node */ char release[]; /* current release of operating system */ char version[]; /* current version of this release */ char machine[]; /* name of hardware type */ };
此功能来自System V,在较旧的日子里,
nodename
元素足以引用UUCP网络上的主机。
和
BSD派生的系统提供了gethostname函数,仅返回主机的名称。如果主机连接到TCP / IP网络,则主机名通常是主机的完全限定域名。
#include <unistd.h> int gethostname(char *name, int namelen);
我想知道utsname
的场nodename
和name
的gethostname()
指出的输出字符串之间有什么区别?
谢谢。
既然你标记了这个linux,我假设我们专门讨论Linux。
Linux手册项目的man 2 gethostname说:
GNU C库不使用gethostname()系统调用;相反,它将gethostname()实现为一个库函数,它调用uname(2)并将返回的nodename字段中的len个字节复制到name中。
因此,在Linux上,两者都是由同一系统调用提供的,并没有区别。