1 /* This file is part of GNU Dico
2    Copyright (C) 2008-2020 Sergey Poznyakoff
3 
4    GNU Dico is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3, or (at your option)
7    any later version.
8 
9    GNU Dico is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with GNU Dico.  If not, see <http://www.gnu.org/licenses/>. */
16 
17 #include <config.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <netdb.h>
21 #include <xdico.h>
22 #include <xalloc.h>
23 #include <xgethostname.h>
24 #include <xgetdomainname.h>
25 
26 char *
xdico_local_hostname(void)27 xdico_local_hostname(void)
28 {
29     struct hostent *hp;
30     char *hostpart = xgethostname();
31     char *ret;
32 
33     hp = gethostbyname(hostpart);
34     if (hp)
35 	ret = xstrdup(hp->h_name);
36     else {
37 	char *domainpart = xgetdomainname();
38 
39 	if (domainpart && domainpart[0] && strcmp(domainpart, "(none)")) {
40 	    ret = xmalloc(strlen(hostpart) + 1
41 			       + strlen(domainpart) + 1);
42 	    strcpy(ret, hostpart);
43 	    strcat(ret, ".");
44 	    strcat(ret, domainpart);
45 	    free(hostpart);
46 	} else
47 	    ret = hostpart;
48 	free(domainpart);
49     }
50     return ret;
51 }
52 
53