]> andersk Git - moira.git/blame - lib/fixhost.c
putting hostname in ""'s suppresses canonicalization
[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$";
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>
19#include <arpa/nameser.h>
20#include <arpa/resolv.h>
21#include <netdb.h>
22#include <stdio.h>
23#include <strings.h>
24#include <ctype.h>
25
26extern char *malloc();
27extern char *realloc();
28extern char *strsave();
29
30/*
00bfb581 31 * Canonicalize hostname:
32 * if it is in double-quotes, then strip the quotes and return the name.
33 * if it is in the namespace, call the nameserver to expand it
34 * otherwise uppercase it and append the default domain (using an, er,
35 * undocumented global of the nameserver).
56a69e4e 36 *
37 * Assumes that host was allocated using malloc(); it may be freed or
38 * realloc'ed, so the old pointer should not be considered valid.
39 */
40
41char *
42canonicalize_hostname(host)
43 char *host;
44{
45 register struct hostent *hp;
46 int n_len;
47 int has_dot = 0;
48 char tbuf[BUFSIZ];
49 register char *cp;
50
00bfb581 51 if (strlen(host) > 2 && host[0] == '"' && host[strlen(host)-1] == '"') {
52 strcpy(host+1, tbuf);
53 free(host);
54 tbuf[strlen(tbuf)-1] = 0;
55 return(strsave(tbuf));
56 }
57
277817a5 58 if (index(host, '*') || index(host, '?'))
59 return(host);
60
56a69e4e 61 hp = gethostbyname(host);
62
63 if (hp) {
64 n_len = strlen(hp->h_name) + 1;
65 host = realloc(host, (unsigned)n_len);
66
67 (void) strcpy(host, hp->h_name);
68 return host;
69 } else {
70 /* can't get name from nameserver; fix up the format a bit */
71 for (cp = host; *cp; cp++) {
72 register int c; /* pcc doesn't like register char */
73 if (islower(c = *cp)) *cp = toupper(c);
74 has_dot |= (c == '.');
75 }
76 if (!has_dot) {
77 (void) sprintf(tbuf, "%s.%s", host, _res.defdname);
78 free(host);
79 host = strsave(tbuf);
80 }
81 return host;
82 }
83}
This page took 0.070814 seconds and 5 git commands to generate.