6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
7 * For copying and distribution information, please see the file
12 static char *rcsid_fixhost_c = "$Header$";
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>
26 extern char *malloc();
27 extern char *realloc();
28 extern char *strsave();
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).
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.
42 canonicalize_hostname(host)
45 register struct hostent *hp;
51 if (strlen(host) > 2 && host[0] == '"' && host[strlen(host)-1] == '"') {
54 tbuf[strlen(tbuf)-1] = 0;
55 return(strsave(tbuf));
58 if (index(host, '*') || index(host, '?'))
61 hp = gethostbyname(host);
64 n_len = strlen(hp->h_name) + 1;
65 host = realloc(host, (unsigned)n_len);
67 (void) strcpy(host, hp->h_name);
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 == '.');
77 (void) sprintf(tbuf, "%s.%s", host, _res.defdname);