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>
25 extern char *malloc();
26 extern char *realloc();
29 * Canonicalize hostname:
30 * if it is in double-quotes, then strip the quotes and return the name.
31 * if it is in the namespace, call the nameserver to expand it
32 * otherwise uppercase it and append the default domain (using an, er,
33 * undocumented global of the nameserver).
35 * Assumes that host was allocated using malloc(); it may be freed or
36 * realloc'ed, so the old pointer should not be considered valid.
40 canonicalize_hostname(host)
43 register struct hostent *hp;
49 if (strlen(host) > 2 && host[0] == '"' && host[strlen(host)-1] == '"') {
52 tbuf[strlen(tbuf)-1] = 0;
53 return(strsave(tbuf));
56 if (index(host, '*') || index(host, '?') || index(host, '['))
59 hp = gethostbyname(host);
62 n_len = strlen(hp->h_name) + 1;
63 host = realloc(host, (unsigned)n_len);
65 (void) strcpy(host, hp->h_name);
68 /* can't get name from nameserver; fix up the format a bit */
69 for (cp = host; *cp; cp++) {
70 register int c; /* pcc doesn't like register char */
71 if (islower(c = *cp)) *cp = toupper(c);
72 has_dot |= (c == '.');
75 static char *domain = NULL;
78 gethostname(tbuf, sizeof(tbuf));
79 hp = gethostbyname(tbuf);
80 cp = index(hp->h_name, '.');
82 domain = strsave(++cp);
86 (void) sprintf(tbuf, "%s.%s", host, domain);