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