]> andersk Git - moira.git/blame - lib/fixhost.c
Initial revision
[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
7 *
8 * $Log$
9 * Revision 1.1 1987-09-03 03:12:45 wesommer
10 * Initial revision
11 *
12 */
13
14#ifndef lint
15static char *rcsid_fixhost_c = "$Header$";
16#endif lint
17
18#include <sys/types.h>
19#include <sys/socket.h>
20#include <netinet/in.h>
21#include <arpa/nameser.h>
22#include <arpa/resolv.h>
23#include <netdb.h>
24#include <stdio.h>
25#include <strings.h>
26#include <ctype.h>
27
28extern char *malloc();
29extern char *realloc();
30extern char *strsave();
31
32/*
33 * Canonicalize hostname; if it is in the namespace, call the
34 * nameserver to expand it; otherwise uppercase it and append the
35 * default domain (using an, er, undocumented global of the
36 * nameserver).
37 *
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.
40 */
41
42char *
43canonicalize_hostname(host)
44 char *host;
45{
46 register struct hostent *hp;
47 int n_len;
48 int has_dot = 0;
49 char tbuf[BUFSIZ];
50 register char *cp;
51
52 hp = gethostbyname(host);
53
54 if (hp) {
55 n_len = strlen(hp->h_name) + 1;
56 host = realloc(host, (unsigned)n_len);
57
58 (void) strcpy(host, hp->h_name);
59 return host;
60 } else {
61 /* can't get name from nameserver; fix up the format a bit */
62 for (cp = host; *cp; cp++) {
63 register int c; /* pcc doesn't like register char */
64 if (islower(c = *cp)) *cp = toupper(c);
65 has_dot |= (c == '.');
66 }
67 if (!has_dot) {
68 (void) sprintf(tbuf, "%s.%s", host, _res.defdname);
69 free(host);
70 host = strsave(tbuf);
71 }
72 return host;
73 }
74}
This page took 0.064284 seconds and 5 git commands to generate.