]> andersk Git - moira.git/blob - lib/fixhost.c
Initial revision
[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 <arpa/nameser.h>
20 #if defined(sun) || defined(AIX386)
21 #include <resolv.h>
22 #else
23 #include <arpa/resolv.h>
24 #endif
25 #include <netdb.h>
26 #include <stdio.h>
27 #include <strings.h>
28 #include <ctype.h>
29
30 extern char *malloc();
31 extern char *realloc();
32 extern char *strsave();
33
34 /*
35  * Canonicalize hostname:
36  *  if it is in double-quotes, then strip the quotes and return the name.
37  *  if it is in the namespace, call the nameserver to expand it
38  *  otherwise uppercase it and append the default domain (using an, er,
39  *    undocumented global of the nameserver).
40  *
41  * Assumes that host was allocated using malloc(); it may be freed or
42  * realloc'ed, so the old pointer should not be considered valid.
43  */
44
45 char *
46 canonicalize_hostname(host)
47     char *host;
48 {
49     register struct hostent *hp;
50     int n_len;
51     int has_dot = 0;
52     char tbuf[BUFSIZ];
53     register char *cp;
54     
55     if (strlen(host) > 2 && host[0] == '"' && host[strlen(host)-1] == '"') {
56         strcpy(tbuf, host+1);
57         free(host);
58         tbuf[strlen(tbuf)-1] = 0;
59         return(strsave(tbuf));
60     }
61
62     if (index(host, '*') || index(host, '?') || index(host, '['))
63       return(host);
64
65     hp = gethostbyname(host);
66
67     if (hp) {
68         n_len = strlen(hp->h_name) + 1;
69         host = realloc(host, (unsigned)n_len);
70         
71         (void) strcpy(host, hp->h_name);
72         return host;
73     } else {
74         /* can't get name from nameserver; fix up the format a bit */
75         for (cp = host; *cp; cp++) {
76             register int c;     /* pcc doesn't like register char */
77             if (islower(c = *cp)) *cp = toupper(c);
78             has_dot |= (c == '.');
79         }
80         if (!has_dot) {
81             (void) sprintf(tbuf, "%s.%s", host, _res.defdname);
82             free(host);
83             host = strsave(tbuf);
84         }
85         return host;
86     }
87 }
This page took 1.236532 seconds and 5 git commands to generate.