]> andersk Git - moira.git/blame - lib/fixhost.c
strings.h no longer exists on the sun, and string.h is POSIX anyway
[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>
56a69e4e 19#include <netdb.h>
20#include <stdio.h>
8fd777cf 21#ifdef POSIX
22#include <sys/utsname.h>
23#endif
24#include <string.h>
56a69e4e 25#include <ctype.h>
3dab9162 26#include <moira.h>
56a69e4e 27
28extern char *malloc();
29extern char *realloc();
56a69e4e 30
31/*
00bfb581 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).
56a69e4e 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];
8fd777cf 50#ifdef POSIX
51 struct utsname name;
52#endif
56a69e4e 53 register char *cp;
54
00bfb581 55 if (strlen(host) > 2 && host[0] == '"' && host[strlen(host)-1] == '"') {
7fb8be7f 56 strcpy(tbuf, host+1);
00bfb581 57 free(host);
58 tbuf[strlen(tbuf)-1] = 0;
59 return(strsave(tbuf));
60 }
61
8fd777cf 62 if (strchr(host, '*') || strchr(host, '?') || strchr(host, '['))
277817a5 63 return(host);
64
56a69e4e 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) {
3dab9162 81 static char *domain = NULL;
82
83 if (domain == NULL) {
8fd777cf 84#ifdef POSIX
85 (void) uname(&name);
86 strncpy(tbuf, name.nodename, sizeof(tbuf));
87#else
3dab9162 88 gethostname(tbuf, sizeof(tbuf));
8fd777cf 89#endif
3dab9162 90 hp = gethostbyname(tbuf);
8fd777cf 91 cp = strchr(hp->h_name, '.');
3dab9162 92 if (cp)
93 domain = strsave(++cp);
94 else
95 domain = "";
96 }
97 (void) sprintf(tbuf, "%s.%s", host, domain);
56a69e4e 98 free(host);
99 host = strsave(tbuf);
100 }
101 return host;
102 }
103}
This page took 0.091978 seconds and 5 git commands to generate.