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>
23 #include <sys/utsname.h>
30 * Canonicalize hostname:
31 * if it is in double-quotes, then strip the quotes and return the name.
32 * if it is in the namespace, call the nameserver to expand it
33 * otherwise uppercase it and append the default domain (using an, er,
34 * undocumented global of the nameserver).
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.
41 canonicalize_hostname(host)
44 register struct hostent *hp;
53 if (strlen(host) > 2 && host[0] == '"' && host[strlen(host)-1] == '"') {
56 tbuf[strlen(tbuf)-1] = 0;
57 return(strsave(tbuf));
60 if (strchr(host, '*') || strchr(host, '?') || strchr(host, '['))
63 hp = gethostbyname(host);
66 n_len = strlen(hp->h_name) + 1;
67 host = realloc(host, (unsigned)n_len);
69 (void) strcpy(host, hp->h_name);
72 /* can't get name from nameserver; fix up the format a bit */
73 for (cp = host; *cp; cp++) {
74 register int c; /* pcc doesn't like register char */
75 if (islower(c = *cp)) *cp = toupper(c);
76 has_dot |= (c == '.');
79 static char *domain = NULL;
84 strncpy(tbuf, name.nodename, sizeof(tbuf));
86 gethostname(tbuf, sizeof(tbuf));
88 hp = gethostbyname(tbuf);
89 cp = strchr(hp->h_name, '.');
91 domain = strsave(++cp);
95 (void) sprintf(tbuf, "%s.%s", host, domain);