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 #if defined(sun) || defined(AIX386)
23 #include <arpa/resolv.h>
30 extern char *malloc();
31 extern char *realloc();
32 extern char *strsave();
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).
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.
46 canonicalize_hostname(host)
49 register struct hostent *hp;
55 if (strlen(host) > 2 && host[0] == '"' && host[strlen(host)-1] == '"') {
58 tbuf[strlen(tbuf)-1] = 0;
59 return(strsave(tbuf));
62 if (index(host, '*') || index(host, '?'))
65 hp = gethostbyname(host);
68 n_len = strlen(hp->h_name) + 1;
69 host = realloc(host, (unsigned)n_len);
71 (void) strcpy(host, hp->h_name);
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 == '.');
81 (void) sprintf(tbuf, "%s.%s", host, _res.defdname);