]>
Commit | Line | Data |
---|---|---|
56a69e4e | 1 | /* |
2 | * $Source$ | |
3 | * $Author$ | |
4 | * $Header$ | |
5 | * | |
6 | * Copyright (C) 1987 by the Massachusetts Institute of Technology | |
babbc197 | 7 | * For copying and distribution information, please see the file |
8 | * <mit-copyright.h>. | |
56a69e4e | 9 | */ |
10 | ||
11 | #ifndef lint | |
12 | static char *rcsid_fixhost_c = "$Header$"; | |
13 | #endif lint | |
14 | ||
babbc197 | 15 | #include <mit-copyright.h> |
56a69e4e | 16 | #include <sys/types.h> |
17 | #include <sys/socket.h> | |
18 | #include <netinet/in.h> | |
56a69e4e | 19 | #include <netdb.h> |
20 | #include <stdio.h> | |
21 | #include <strings.h> | |
22 | #include <ctype.h> | |
3dab9162 | 23 | #include <moira.h> |
56a69e4e | 24 | |
25 | extern char *malloc(); | |
26 | extern char *realloc(); | |
56a69e4e | 27 | |
28 | /* | |
00bfb581 | 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). | |
56a69e4e | 34 | * |
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. | |
37 | */ | |
38 | ||
39 | char * | |
40 | canonicalize_hostname(host) | |
41 | char *host; | |
42 | { | |
43 | register struct hostent *hp; | |
44 | int n_len; | |
45 | int has_dot = 0; | |
46 | char tbuf[BUFSIZ]; | |
47 | register char *cp; | |
48 | ||
00bfb581 | 49 | if (strlen(host) > 2 && host[0] == '"' && host[strlen(host)-1] == '"') { |
7fb8be7f | 50 | strcpy(tbuf, host+1); |
00bfb581 | 51 | free(host); |
52 | tbuf[strlen(tbuf)-1] = 0; | |
53 | return(strsave(tbuf)); | |
54 | } | |
55 | ||
93431049 | 56 | if (index(host, '*') || index(host, '?') || index(host, '[')) |
277817a5 | 57 | return(host); |
58 | ||
56a69e4e | 59 | hp = gethostbyname(host); |
60 | ||
61 | if (hp) { | |
62 | n_len = strlen(hp->h_name) + 1; | |
63 | host = realloc(host, (unsigned)n_len); | |
64 | ||
65 | (void) strcpy(host, hp->h_name); | |
66 | return host; | |
67 | } else { | |
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 == '.'); | |
73 | } | |
74 | if (!has_dot) { | |
3dab9162 | 75 | static char *domain = NULL; |
76 | ||
77 | if (domain == NULL) { | |
78 | gethostname(tbuf, sizeof(tbuf)); | |
79 | hp = gethostbyname(tbuf); | |
80 | cp = index(hp->h_name, '.'); | |
81 | if (cp) | |
82 | domain = strsave(++cp); | |
83 | else | |
84 | domain = ""; | |
85 | } | |
86 | (void) sprintf(tbuf, "%s.%s", host, domain); | |
56a69e4e | 87 | free(host); |
88 | host = strsave(tbuf); | |
89 | } | |
90 | return host; | |
91 | } | |
92 | } |