Add support for tab-completion when selecting by rule
[alpine.git] / pith / osdep / domnames.c
blobc7ea177e65ae08d6163c962934973e966dd54c83
1 /*
2 * ========================================================================
3 * Copyright 2013-2022 Eduardo Chappa
4 * Copyright 2008 University of Washington
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * ========================================================================
15 #include <system.h>
16 #include <general.h>
18 #include "domnames.h"
21 /*----------------------------------------------------------------------
22 Get the current host and domain names
24 Args: hostname -- buffer to return the hostname in
25 hsize -- size of buffer above
26 domainname -- buffer to return domain name in
27 dsize -- size of buffer above
29 Result: The system host and domain names are returned. If the full host
30 name is akbar.cac.washington.edu then the domainname is
31 cac.washington.edu.
33 On Internet connected hosts this look up uses /etc/hosts and DNS to
34 figure all this out. On other less well connected machines some other
35 file may be read. If there is no notion of a domain name the domain
36 name may be left blank. On a PC where there really isn't a host name
37 this should return blank strings. The .pinerc will take care of
38 configuring the domain names. That is, this should only return the
39 native system's idea of what the names are if the system has such
40 a concept.
41 ----*/
42 void
43 getdomainnames(char *hostname, int hsize, char *domainname, int dsize)
45 #if HAVE_NETDB_H
46 char *dn, hname[MAX_ADDRESS+1];
47 struct hostent *he;
48 char **alias;
49 char *maybe = NULL;
51 if(gethostname(hname, MAX_ADDRESS))
52 hname[0] = 0xff;
54 /* sanity check of hostname string */
55 for(dn = hname; (*dn > 0x20) && (*dn < 0x7f); ++dn)
58 if(*dn){ /* if invalid string returned, return "unknown" */
59 strncpy(domainname, "unknown", dsize-1);
60 domainname[dsize-1] = '\0';
61 strncpy(hostname, "unknown", hsize-1);
62 hostname[hsize-1] = '\0';
63 return;
66 he = gethostbyname(hname);
67 hostname[0] = '\0';
69 if(he == NULL){
70 strncpy(hostname, hname, hsize-1);
71 hostname[hsize-1] = '\0';
73 else{
75 * If no dot in hostname it may be the case that there
76 * is an alias which is really the fully-qualified
77 * hostname. This could happen if the administrator has
78 * (incorrectly) put the unqualified name first in the
79 * hosts file, for example. The problem with looking for
80 * an alias with a dot is that now we're guessing, since
81 * the aliases aren't supposed to be the official hostname.
82 * We'll compromise and only use an alias if the primary
83 * name has no dot and exactly one of the aliases has a
84 * dot.
86 strncpy(hostname, he->h_name, hsize-1);
87 hostname[hsize-1] = '\0';
88 if(strchr(hostname, '.') == NULL){ /* no dot in hostname */
89 for(alias = he->h_aliases; *alias; alias++){
90 if(strchr(*alias, '.') != NULL){ /* found one */
91 if(maybe){ /* oops, this is the second one */
92 maybe = NULL;
93 break;
95 else
96 maybe = *alias;
100 if(maybe){
101 strncpy(hostname, maybe, hsize-1);
102 hostname[hsize-1] = '\0';
107 hostname[hsize-1] = '\0';
109 if((dn = strchr(hostname, '.')) != NULL)
110 strncpy(domainname, dn+1, dsize-1);
111 else
112 strncpy(domainname, hostname, dsize-1);
114 domainname[dsize-1] = '\0';
115 #else /* !HAVE_NETDB_H */
116 /* should only be _WINDOWS */
118 #ifdef _WINDOWS
119 char *p;
120 extern char *mylocalhost(void);
122 hostname[0] = domainname[0] = '\0';
123 if(p = mylocalhost())
124 snprintf(hostname, hsize, "%s", p);
126 snprintf(domainname, dsize, "%s",
127 (hostname[0] && hostname[0] != '[' && (p = strchr(hostname,'.')))
128 ? p+1 : hostname);
129 #else /* !_WINDOWS */
131 char *p, hname[MAX_ADDRESS+1];
133 hostname[0] = domainname[0] = '\0';
134 if(gethostname(hname, MAX_ADDRESS) == 0)
135 snprintf(hostname, hsize, "%s", hname);
137 snprintf(domainname, dsize, "%s",
138 (hostname[0] && hostname[0] != '[' && (p = strchr(hostname,'.')))
139 ? p+1 : hostname);
140 #endif /* !_WINDOWS */
141 #endif /* !HAVE_NETDB_H */