]> andersk Git - moira.git/blame - lib/fixhost.c
cant_fix takes an argument
[moira.git] / lib / fixhost.c
CommitLineData
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
12static char *rcsid_fixhost_c = "$Header$";
a43ce477 13#endif
56a69e4e 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>
a43ce477 21#include <stdlib.h>
8fd777cf 22#include <sys/utsname.h>
8fd777cf 23#include <string.h>
56a69e4e 24#include <ctype.h>
3dab9162 25#include <moira.h>
56a69e4e 26
56a69e4e 27/*
00bfb581 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).
56a69e4e 33 *
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.
36 */
37
38char *
39canonicalize_hostname(host)
40 char *host;
41{
42 register struct hostent *hp;
43 int n_len;
44 int has_dot = 0;
45 char tbuf[BUFSIZ];
8fd777cf 46 struct utsname name;
56a69e4e 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
8fd777cf 56 if (strchr(host, '*') || strchr(host, '?') || strchr(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) {
8fd777cf 78 (void) uname(&name);
355f984d 79 hp = gethostbyname(name.nodename);
8fd777cf 80 cp = strchr(hp->h_name, '.');
3dab9162 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}
This page took 0.821929 seconds and 5 git commands to generate.