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>
22 #include <sys/utsname.h>
28 extern char *malloc();
29 extern char *realloc();
32 * Canonicalize hostname:
33 * if it is in double-quotes, then strip the quotes and return the name.
34 * if it is in the namespace, call the nameserver to expand it
35 * otherwise uppercase it and append the default domain (using an, er,
36 * undocumented global of the nameserver).
38 * Assumes that host was allocated using malloc(); it may be freed or
39 * realloc'ed, so the old pointer should not be considered valid.
43 canonicalize_hostname(host)
46 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 (strchr(host, '*') || strchr(host, '?') || strchr(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 static char *domain = NULL;
86 strncpy(tbuf, name.nodename, sizeof(tbuf));
88 gethostname(tbuf, sizeof(tbuf));
90 hp = gethostbyname(tbuf);
91 cp = strchr(hp->h_name, '.');
93 domain = strsave(++cp);
97 (void) sprintf(tbuf, "%s.%s", host, domain);