* New alpha version 2.24.1
[alpine.git] / pith / osdep / domnames.c
blobaa2d917f568a058500079b06777c798ac7c73e70
1 #if !defined(lint) && !defined(DOS)
2 static char rcsid[] = "$Id: domnames.c 1176 2008-09-29 21:16:42Z hubert@u.washington.edu $";
3 #endif
5 /*
6 * ========================================================================
7 * Copyright 2013-2021 Eduardo Chappa
8 * Copyright 2008 University of Washington
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * ========================================================================
19 #include <system.h>
20 #include <general.h>
22 #include "domnames.h"
25 /*----------------------------------------------------------------------
26 Get the current host and domain names
28 Args: hostname -- buffer to return the hostname in
29 hsize -- size of buffer above
30 domainname -- buffer to return domain name in
31 dsize -- size of buffer above
33 Result: The system host and domain names are returned. If the full host
34 name is akbar.cac.washington.edu then the domainname is
35 cac.washington.edu.
37 On Internet connected hosts this look up uses /etc/hosts and DNS to
38 figure all this out. On other less well connected machines some other
39 file may be read. If there is no notion of a domain name the domain
40 name may be left blank. On a PC where there really isn't a host name
41 this should return blank strings. The .pinerc will take care of
42 configuring the domain names. That is, this should only return the
43 native system's idea of what the names are if the system has such
44 a concept.
45 ----*/
46 void
47 getdomainnames(char *hostname, int hsize, char *domainname, int dsize)
49 #if HAVE_NETDB_H
50 char *dn, hname[MAX_ADDRESS+1];
51 struct hostent *he;
52 char **alias;
53 char *maybe = NULL;
55 if(gethostname(hname, MAX_ADDRESS))
56 hname[0] = 0xff;
58 /* sanity check of hostname string */
59 for(dn = hname; (*dn > 0x20) && (*dn < 0x7f); ++dn)
62 if(*dn){ /* if invalid string returned, return "unknown" */
63 strncpy(domainname, "unknown", dsize-1);
64 domainname[dsize-1] = '\0';
65 strncpy(hostname, "unknown", hsize-1);
66 hostname[hsize-1] = '\0';
67 return;
70 he = gethostbyname(hname);
71 hostname[0] = '\0';
73 if(he == NULL){
74 strncpy(hostname, hname, hsize-1);
75 hostname[hsize-1] = '\0';
77 else{
79 * If no dot in hostname it may be the case that there
80 * is an alias which is really the fully-qualified
81 * hostname. This could happen if the administrator has
82 * (incorrectly) put the unqualified name first in the
83 * hosts file, for example. The problem with looking for
84 * an alias with a dot is that now we're guessing, since
85 * the aliases aren't supposed to be the official hostname.
86 * We'll compromise and only use an alias if the primary
87 * name has no dot and exactly one of the aliases has a
88 * dot.
90 strncpy(hostname, he->h_name, hsize-1);
91 hostname[hsize-1] = '\0';
92 if(strchr(hostname, '.') == NULL){ /* no dot in hostname */
93 for(alias = he->h_aliases; *alias; alias++){
94 if(strchr(*alias, '.') != NULL){ /* found one */
95 if(maybe){ /* oops, this is the second one */
96 maybe = NULL;
97 break;
99 else
100 maybe = *alias;
104 if(maybe){
105 strncpy(hostname, maybe, hsize-1);
106 hostname[hsize-1] = '\0';
111 hostname[hsize-1] = '\0';
113 if((dn = strchr(hostname, '.')) != NULL)
114 strncpy(domainname, dn+1, dsize-1);
115 else
116 strncpy(domainname, hostname, dsize-1);
118 domainname[dsize-1] = '\0';
119 #else /* !HAVE_NETDB_H */
120 /* should only be _WINDOWS */
122 #ifdef _WINDOWS
123 char *p;
124 extern char *mylocalhost(void);
126 hostname[0] = domainname[0] = '\0';
127 if(p = mylocalhost())
128 snprintf(hostname, hsize, "%s", p);
130 snprintf(domainname, dsize, "%s",
131 (hostname[0] && hostname[0] != '[' && (p = strchr(hostname,'.')))
132 ? p+1 : hostname);
133 #else /* !_WINDOWS */
135 char *p, hname[MAX_ADDRESS+1];
137 hostname[0] = domainname[0] = '\0';
138 if(gethostname(hname, MAX_ADDRESS) == 0)
139 snprintf(hostname, hsize, "%s", hname);
141 snprintf(domainname, dsize, "%s",
142 (hostname[0] && hostname[0] != '[' && (p = strchr(hostname,'.')))
143 ? p+1 : hostname);
144 #endif /* !_WINDOWS */
145 #endif /* !HAVE_NETDB_H */