Penguin
Blame: gethostname(2)
EditPageHistoryDiffInfoLikePages
Annotated edit history of gethostname(2) version 7, including all changes. View license author blame.
Rev Author # Line
1 perry 1 !!NAME
2
2 PerryLorier 3 gethostname - get host name
1 perry 4 !!SYNOPSIS
2 PerryLorier 5 __#include <unistd.h>__
6 __int gethostname(char *__''name''__, size_t__ ''len''__);__
1 perry 7
8 !!DESCRIPTION
2 PerryLorier 9 These functions are used to access or to change the host name of the current processor. Note, that on the Internet each interface has a at least one address, and names are associated with addresses, so a machine may (and usually does) have multiple names. gethostname(2) will return the one that the machine identifies itself as.
1 perry 10
11 !!RETURN VALUE
2 PerryLorier 12 On success, zero is returned. On error, -1 is returned, and ''errno'' is set appropriately.
1 perry 13
14 !!ERRORS
6 PerryLorier 15 ;[EINVAL]: ''len'' is negative or, on Linux/i386, ''len'' is smaller than the actual size. (In this last case glibc 2.1 uses [ENAMETOOLONG].)
2 PerryLorier 16 ;[EFAULT]: ''name'' is an invalid address.
5 PerryLorier 17 ;[ENAMETOOLONG]: ''len'' is smaller than the length of the hostname
1 perry 18
19 !!CONFORMING TO
2 PerryLorier 20 SVr4, 4.4BSD (this function first appeared in 4.2BSD). POSIX.1 does not define these functions, but ISO/IEC 9945-1:1990 mentions them in B.4.4.1.
1 perry 21
22 !!BUGS
2 PerryLorier 23 According to the SUSv2, gethostname(2) must return ''len'' bytes (a truncated hostname, NUL-terminated or not) when the hostname is longer. Linux/Alpha (which has a system call gethostname(2)) complies with this requirement, but libc and glibc on Linux/i386 only return an error in this case.
1 perry 24
25 !!NOTES
2 PerryLorier 26 The definition of success varies. SUSv2 defines ''gethostname()'' as `return possibly truncated hostname', and having a small ''len'' does not cause an error return. Of course it must be possible to be certain that one has obtained the full hostname, and to this end
27 SUSv2 guarantees that `Host names are limited to 255 bytes'.
4 PerryLorier 28
29 !!EXAMPLE
30 #include <unistd.h>
31 #include <malloc.h>
32 #include <stdio.h>
33 #include <string.h>
34
35 int main(int argc,char **argv)
36 {
37 char domain[[256];
38 int ret=gethostname(domain,sizeof(domain));
39 if (ret==-1) {
40 perror("gethostname");
41 return 1;
42 }
43 printf("%s\n",domain);
44 return 0;
45 }
46
1 perry 47
48 !!SEE ALSO
2 PerryLorier 49 getdomainname(2), setdomainname(2), uname(2)
This page is a man page (or other imported legacy content). We are unable to automatically determine the license status of this page.

PHP Warning

lib/blame.php:177: Warning: Invalid argument supplied for foreach() (...repeated 9 times)