3 * Canonicalize a hostname
5 * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
10 #include <mit-copyright.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <sys/utsname.h>
18 #include <netinet/in.h>
28 * Canonicalize hostname:
29 * if it is in double-quotes, then strip the quotes and return the name.
30 * if it is in the namespace, call the nameserver to expand it
31 * otherwise uppercase it and append the default domain (using an, er,
32 * undocumented global of the nameserver).
34 * Assumes that host was allocated using malloc(); it may be freed or
35 * realloc'ed, so the old pointer should not be considered valid.
38 char *canonicalize_hostname(char *host)
47 if (strlen(host) > 2 && host[0] == '"' && host[strlen(host) - 1] == '"')
49 strcpy(tbuf, host + 1);
51 tbuf[strlen(tbuf) - 1] = '\0';
55 if (strchr(host, '*') || strchr(host, '?') || strchr(host, '['))
58 hp = gethostbyname(host);
62 n_len = strlen(hp->h_name) + 1;
63 host = realloc(host, n_len);
65 strcpy(host, hp->h_name);
70 /* can't get name from nameserver; fix up the format a bit */
71 for (cp = host; *cp; cp++)
76 has_dot |= (c == '.');
80 static char *domain = NULL;
85 hp = gethostbyname(name.nodename);
86 cp = strchr(hp->h_name, '.');
88 domain = strdup(++cp);
92 sprintf(tbuf, "%s.%s", host, domain);