]> andersk Git - moira.git/blob - lib/fixhost.c
missing coma in last change, causes compile_et to coredump
[moira.git] / lib / fixhost.c
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *      Copyright (C) 1987 by the Massachusetts Institute of Technology
7  *      For copying and distribution information, please see the file
8  *      <mit-copyright.h>.
9  */
10
11 #ifndef lint
12 static char *rcsid_fixhost_c = "$Header$";
13 #endif lint
14
15 #include <mit-copyright.h>
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <netdb.h>
20 #include <stdio.h>
21 #ifdef POSIX
22 #include <sys/utsname.h>
23 #endif
24 #include <string.h>
25 #include <ctype.h>
26 #include <moira.h>
27
28 extern char *malloc();
29 extern char *realloc();
30
31 /*
32  * Canonicalize hostname:
33  *  if it is in double-quotes, then strip the quotes and return the name.
34  *  if it is in the namespace, call the nameserver to expand it
35  *  otherwise uppercase it and append the default domain (using an, er,
36  *    undocumented global of the 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
42 char *
43 canonicalize_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 #ifdef POSIX
51     struct utsname name;
52 #endif
53     register char *cp;
54     
55     if (strlen(host) > 2 && host[0] == '"' && host[strlen(host)-1] == '"') {
56         strcpy(tbuf, host+1);
57         free(host);
58         tbuf[strlen(tbuf)-1] = 0;
59         return(strsave(tbuf));
60     }
61
62     if (strchr(host, '*') || strchr(host, '?') || strchr(host, '['))
63       return(host);
64
65     hp = gethostbyname(host);
66
67     if (hp) {
68         n_len = strlen(hp->h_name) + 1;
69         host = realloc(host, (unsigned)n_len);
70         
71         (void) strcpy(host, hp->h_name);
72         return host;
73     } else {
74         /* can't get name from nameserver; fix up the format a bit */
75         for (cp = host; *cp; cp++) {
76             register int c;     /* pcc doesn't like register char */
77             if (islower(c = *cp)) *cp = toupper(c);
78             has_dot |= (c == '.');
79         }
80         if (!has_dot) {
81             static char *domain = NULL;
82
83             if (domain == NULL) {
84 #ifdef POSIX
85                 (void) uname(&name);
86                 strncpy(tbuf, name.nodename, sizeof(tbuf));
87 #else
88                 gethostname(tbuf, sizeof(tbuf));
89 #endif
90                 hp = gethostbyname(tbuf);
91                 cp = strchr(hp->h_name, '.');
92                 if (cp)
93                   domain = strsave(++cp);
94                 else
95                   domain = "";
96             }
97             (void) sprintf(tbuf, "%s.%s", host, domain);
98             free(host);
99             host = strsave(tbuf);
100         }
101         return host;
102     }
103 }
This page took 0.047326 seconds and 5 git commands to generate.