]> andersk Git - moira.git/blobdiff - lib/fixhost.c
add sq_remove_last_data, to remove the most recently sq_get_data'd
[moira.git] / lib / fixhost.c
index dae2acc3057c5464aca63efca08e340331fb426e..d9797a001dd49b173faa62c16f69baefa462aef0 100644 (file)
@@ -1,35 +1,28 @@
-/*
- *     $Source$
- *     $Author$
- *     $Header$
+/* $Id$
+ *
+ * Canonicalize a hostname
  *
- *     Copyright (C) 1987 by the Massachusetts Institute of Technology
- *     For copying and distribution information, please see the file
- *     <mit-copyright.h>.
+ * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
+ * For copying and distribution information, please see the file
+ * <mit-copyright.h>.
  */
 
-#ifndef lint
-static char *rcsid_fixhost_c = "$Header$";
-#endif lint
-
 #include <mit-copyright.h>
+#include <moira.h>
+
 #include <sys/types.h>
 #include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/nameser.h>
-#if defined(sun) || defined(AIX386)
-#include <resolv.h>
-#else
-#include <arpa/resolv.h>
-#endif
+#include <sys/utsname.h>
+
 #include <netdb.h>
-#include <stdio.h>
-#include <strings.h>
+#include <netinet/in.h>
+
 #include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 
-extern char *malloc();
-extern char *realloc();
-extern char *strsave();
+RCSID("$Header$");
 
 /*
  * Canonicalize hostname:
@@ -42,46 +35,82 @@ extern char *strsave();
  * realloc'ed, so the old pointer should not be considered valid.
  */
 
-char *
-canonicalize_hostname(host)
-    char *host;
+char *canonicalize_hostname(char *host)
 {
-    register struct hostent *hp;
-    int n_len;
-    int has_dot = 0;
-    char tbuf[BUFSIZ];
-    register char *cp;
-    
-    if (strlen(host) > 2 && host[0] == '"' && host[strlen(host)-1] == '"') {
-       strcpy(tbuf, host+1);
-       free(host);
-       tbuf[strlen(tbuf)-1] = 0;
-       return(strsave(tbuf));
+  struct hostent *hp;
+  int len;
+  int has_dot = 0;
+  char *tbuf, *cp;
+
+  len = strlen(host);
+  if (len > 2 && host[0] == '"' && host[len - 1] == '"')
+    {
+      tbuf = malloc(len - 1);
+      if (!tbuf)
+       return NULL;
+      strncpy(tbuf, host + 1, len - 2);
+      tbuf[len - 2] = '\0';
+      free(host);
+      return tbuf;
     }
 
-    if (index(host, '*') || index(host, '?') || index(host, '['))
-      return(host);
+  if (strchr(host, '*') || strchr(host, '?') || *host == '[')
+    return host;
 
-    hp = gethostbyname(host);
+  hp = gethostbyname(host);
 
-    if (hp) {
-       n_len = strlen(hp->h_name) + 1;
-       host = realloc(host, (unsigned)n_len);
-       
-       (void) strcpy(host, hp->h_name);
-       return host;
-    } else {
-       /* can't get name from nameserver; fix up the format a bit */
-       for (cp = host; *cp; cp++) {
-           register int c;     /* pcc doesn't like register char */
-           if (islower(c = *cp)) *cp = toupper(c);
-           has_dot |= (c == '.');
+  if (hp)
+    {
+      host = realloc(host, strlen(hp->h_name) + 1);
+      if (host)
+       strcpy(host, hp->h_name);
+      return host;
+    }
+  else
+    {
+      /* can't get name from nameserver; fix up the format a bit */
+      for (cp = host; *cp; cp++)
+       {
+         if (islower(*cp))
+           *cp = toupper(*cp);
+         has_dot |= (*cp == '.');
        }
-       if (!has_dot) {
-           (void) sprintf(tbuf, "%s.%s", host, _res.defdname);
-           free(host);
-           host = strsave(tbuf);
+      if (!has_dot)
+       {
+         static char *domain = NULL;
+
+         if (domain == NULL)
+           {
+             char hostbuf[256];
+
+             if (mr_host(hostbuf, sizeof(hostbuf)) == MR_SUCCESS)
+               {
+                 cp = strchr(hostbuf, '.');
+                 if (cp)
+                   domain = strdup(++cp);
+               }
+             else
+               {
+                 struct utsname name;
+                 uname(&name);
+                 hp = gethostbyname(name.nodename);
+                 if (hp)
+                   {
+                     cp = strchr(hp->h_name, '.');
+                     if (cp)
+                       domain = strdup(++cp);
+                   }
+               }
+             if (!domain)
+               domain = "";
+           }
+         tbuf = malloc(strlen(host) + strlen(domain) + 2);
+         if (!tbuf)
+           return NULL;
+         sprintf(tbuf, "%s.%s", host, domain);
+         free(host);
+         host = tbuf;
        }
-       return host;
+      return host;
     }
 }
This page took 0.152049 seconds and 4 git commands to generate.