]> andersk Git - moira.git/blob - lib/fixhost.c
added GDSS stuff
[moira.git] / lib / fixhost.c
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *      Copyright (C) 1987 by the Massachusetts Institute of Technology
7  *      For copying and distribution information, please see the file
8  *      <mit-copyright.h>.
9  */
10
11 #ifndef lint
12 static char *rcsid_fixhost_c = "$Header$";
13 #endif lint
14
15 #include <mit-copyright.h>
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <netdb.h>
20 #include <stdio.h>
21 #include <strings.h>
22 #include <ctype.h>
23 #include <moira.h>
24
25 extern char *malloc();
26 extern char *realloc();
27
28 /*
29  * Canonicalize hostname:
30  *  if it is in double-quotes, then strip the quotes and return the name.
31  *  if it is in the namespace, call the nameserver to expand it
32  *  otherwise uppercase it and append the default domain (using an, er,
33  *    undocumented global of the nameserver).
34  *
35  * Assumes that host was allocated using malloc(); it may be freed or
36  * realloc'ed, so the old pointer should not be considered valid.
37  */
38
39 char *
40 canonicalize_hostname(host)
41     char *host;
42 {
43     register struct hostent *hp;
44     int n_len;
45     int has_dot = 0;
46     char tbuf[BUFSIZ];
47     register char *cp;
48     
49     if (strlen(host) > 2 && host[0] == '"' && host[strlen(host)-1] == '"') {
50         strcpy(tbuf, host+1);
51         free(host);
52         tbuf[strlen(tbuf)-1] = 0;
53         return(strsave(tbuf));
54     }
55
56     if (index(host, '*') || index(host, '?') || index(host, '['))
57       return(host);
58
59     hp = gethostbyname(host);
60
61     if (hp) {
62         n_len = strlen(hp->h_name) + 1;
63         host = realloc(host, (unsigned)n_len);
64         
65         (void) strcpy(host, hp->h_name);
66         return host;
67     } else {
68         /* can't get name from nameserver; fix up the format a bit */
69         for (cp = host; *cp; cp++) {
70             register int c;     /* pcc doesn't like register char */
71             if (islower(c = *cp)) *cp = toupper(c);
72             has_dot |= (c == '.');
73         }
74         if (!has_dot) {
75             static char *domain = NULL;
76
77             if (domain == NULL) {
78                 gethostname(tbuf, sizeof(tbuf));
79                 hp = gethostbyname(tbuf);
80                 cp = index(hp->h_name, '.');
81                 if (cp)
82                   domain = strsave(++cp);
83                 else
84                   domain = "";
85             }
86             (void) sprintf(tbuf, "%s.%s", host, domain);
87             free(host);
88             host = strsave(tbuf);
89         }
90         return host;
91     }
92 }
This page took 0.044567 seconds and 5 git commands to generate.