]> andersk Git - moira.git/blob - lib/fixhost.c
declare whoami
[moira.git] / lib / fixhost.c
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *      Copyright (C) 1987 by the Massachusetts Institute of Technology
7  *
8  *      $Log$
9  *      Revision 1.2  1988-08-02 21:12:18  mar
10  *      don't change hostname if it contains wildcards
11  *
12  * Revision 1.1  87/09/03  03:12:45  wesommer
13  * Initial revision
14  * 
15  */
16
17 #ifndef lint
18 static char *rcsid_fixhost_c = "$Header$";
19 #endif lint
20
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/nameser.h>
25 #include <arpa/resolv.h>
26 #include <netdb.h>
27 #include <stdio.h>
28 #include <strings.h>
29 #include <ctype.h>
30
31 extern char *malloc();
32 extern char *realloc();
33 extern char *strsave();
34
35 /*
36  * Canonicalize hostname; if it is in the namespace, call the
37  * nameserver to expand it; otherwise uppercase it and append the
38  * default domain (using an, er, undocumented global of the
39  * nameserver).
40  *
41  * Assumes that host was allocated using malloc(); it may be freed or
42  * realloc'ed, so the old pointer should not be considered valid.
43  */
44
45 char *
46 canonicalize_hostname(host)
47     char *host;
48 {
49     register struct hostent *hp;
50     int n_len;
51     int has_dot = 0;
52     char tbuf[BUFSIZ];
53     register char *cp;
54     
55     if (index(host, '*') || index(host, '?'))
56       return(host);
57
58     hp = gethostbyname(host);
59
60     if (hp) {
61         n_len = strlen(hp->h_name) + 1;
62         host = realloc(host, (unsigned)n_len);
63         
64         (void) strcpy(host, hp->h_name);
65         return host;
66     } else {
67         /* can't get name from nameserver; fix up the format a bit */
68         for (cp = host; *cp; cp++) {
69             register int c;     /* pcc doesn't like register char */
70             if (islower(c = *cp)) *cp = toupper(c);
71             has_dot |= (c == '.');
72         }
73         if (!has_dot) {
74             (void) sprintf(tbuf, "%s.%s", host, _res.defdname);
75             free(host);
76             host = strsave(tbuf);
77         }
78         return host;
79     }
80 }
This page took 0.111322 seconds and 5 git commands to generate.