#include <unistd.h>
int gethostname(char *name, size_t%uA0len)%uA0
int sethostname(const char *name, size_t%uA0len)
描述
These system calls are used to access or to change the host name of the current processor. The%uA0gethostname() system call returns a null-terminated hostname (set earlier by%uA0sethostname()) in the array%uA0name%uA0that has a length of%uA0len%uA0bytes. In case the null-terminated hostname does not fit, no error is returned, but the hostname is truncated. It is unspecified whether the truncated hostname will be null-terminated.
返回值
On success, zero is returned. On error, -1 is returned, and%uA0errno%uA0is set appropriately.
错误
标签
描述
EFAULT
name%uA0is an invalid address.
EINVAL
len%uA0is negative or, for%uA0sethostname(),%uA0len%uA0is larger than the maximum allowed size, or, for%uA0gethostname() on Linux/i386,%uA0lenis smaller than the actual size. (In this last case glibc 2.1 uses ENAMETOOLONG.)
EPERM
For%uA0sethostname(), the caller did not have theCAP_SYS_ADMINꃊpability.
遵循于
SVr4, 4.4BSD (this interfaces first appeared in 4.2BSD). POSIX.1-2001 specifiesgethostname() but not%uA0sethostname().
注意
SUSv2 guarantees that ‘Host names are limited to 255 bytes’. POSIX.1-2001 guarantees that ‘Host names (not including the terminating null byte) are limited to HOST_NAME_MAX bytes’.
glibc注意事项
The GNU C library implements%uA0gethostname() as a library function that calls%uA0uname(2) and copies up to%uA0len%uA0bytes from the returned%uA0nodename%uA0field into%uA0name. Having performed the copy, the function then checks if the length of the%uA0nodename%uA0was greater than or equal to%uA0len, and if it is, then the function returns -1 with%uA0errno%uA0set toENAMETOOLONG. Versions of glibc before 2.2 handle the case where the length of thenodename%uA0was greater than or equal to%uA0len%uA0differently: nothing is copied into%uA0name%uA0and the function returns -1 with%uA0errno%uA0set to%uA0ENAMETOOLONG.