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