| #include <stdio.h>
#include <unistd.h> #include <stdlib.h> #include <errno.h> 
...
char * hostname = NULL;
size_t sizebuf = 8;
hostname = malloc(size);
  
while( gethostname(hostname, sizebuf ) != 0 ) {
  if( errno != ENAMETOOLONG ) {
    perror("gethostname");
    exit(1);
  }
    sizebuf += 8;
  hostname = realloc(hostname, sizebuf);
}
  
printf("my hostname : %s\n", hostname);
free(hostname);
  
...
#include <stdio.h>
#include <sys/utsname.h> 
...
struct utsname my_utsname;
uname( &my_utsname );
printf("my hostname : %s\n", my_utsname.nodename);
...
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h> #include <netinet/in.h> #include <arpa/inet.h> 
...
struct in_addr address;
struct in_addr * ip; 
struct hostent * host;
int i;
char hostname[] = "a_valid_hostname";
if( (host = gethostbyname(hostname)) == NULL ) {
  herror("gethostbyname");
  exit(1);
}
for( i = 0; host->h_addr_list[i] != NULL; i++ ) {
  ip = ( struct in_addr *)(host->h_addr_list[i]);
  printf("address for %s : %s\n", hostname ,inet_ntoa(*ip));
} 
char addr[] = "127.0.0.1";
if( inet_aton(addr, &address) == 0 ) {
  fprintf(stderr, "invalid address\n");
  return 1;
}
if( (host = gethostbyaddr( (char *)&address, sizeof(struct in_addr), AF_INET )) == NULL ) {
  herror("gethostbyaddr");
  exit(1);
}
printf("hostname for %s : %s\n", addr, host->h_name );
  
...
 |