1 /*
2 * $smu-mark$
3 * $name: gethostname.c$
4 * $author: Salvatore Sanfilippo <antirez@invece.org>$
5 * $copyright: Copyright (C) 1999 by Salvatore Sanfilippo$
6 * $license: This software is under GPL version 2 of license$
7 * $date: Fri Nov 5 11:55:47 MET 1999$
8 * $rev: 8$
9 */
10
11 #include <stdio.h>
12 #include <netdb.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
17 #include <string.h>
18
19 size_t strlcpy(char *dst, const char *src, size_t siz);
20
get_hostname(char * addr)21 char *get_hostname(char* addr)
22 {
23 static char answer[1024];
24 static char lastreq[1024] = {'\0'}; /* last request */
25 struct hostent *he;
26 struct in_addr naddr;
27 static char *last_answerp = NULL;
28
29 printf(" get hostname..."); fflush(stdout);
30 printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
31 " "
32 "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
33
34 if (!strcmp(addr, lastreq))
35 return last_answerp;
36
37 strlcpy(lastreq, addr, 1024);
38 inet_aton(addr, &naddr);
39 he = gethostbyaddr((char*)&naddr, 4, AF_INET);
40
41 if (he == NULL) {
42 last_answerp = NULL;
43 return NULL;
44 }
45
46 strlcpy(answer, he->h_name, 1024);
47 last_answerp = answer;
48
49 return answer;
50 }
51
52