]> andersk Git - moira.git/blobdiff - lib/fixhost.c
Command line printer manipulation client, and build goo.
[moira.git] / lib / fixhost.c
index 11e087d5a0ee5d6c20a3ef557ceb4ffd3c610ee4..ae574ada446fc7fb96a86dc9e39ab7c54b96ffe1 100644 (file)
@@ -1,28 +1,80 @@
-/*
- *     $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
-
 #include <mit-copyright.h>
+#include <moira.h>
+
 #include <sys/types.h>
+
+#ifdef HAVE_UNAME
+#include <sys/utsname.h>
+#endif
+
+#ifndef _WIN32
 #include <sys/socket.h>
-#include <netinet/in.h>
 #include <netdb.h>
+#include <netinet/in.h>
+#endif /* _WIN32 */
+
+#include <ctype.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <sys/utsname.h>
 #include <string.h>
-#include <ctype.h>
-#include <moira.h>
+
+RCSID("$Header$");
+
+static struct hostent *local_gethostbyname(void)
+{
+#ifdef HAVE_UNAME
+  struct utsname name;
+  uname(&name);
+  return gethostbyname(name.nodename);
+#else
+  char hostname[128];
+  gethostname(hostname, sizeof(hostname));
+  hostname[sizeof(hostname)-1] = 0;
+  return gethostbyname(hostname);
+#endif
+}
+
+static char *local_domain(void)
+{
+  static char *domain = NULL;
+  char *cp;
+  struct hostent *hp;
+
+  if (domain == NULL)
+    {
+      char hostbuf[256];
+
+      if (mr_host(hostbuf, sizeof(hostbuf)) == MR_SUCCESS)
+       {
+         cp = strchr(hostbuf, '.');
+         if (cp)
+           domain = strdup(++cp);
+       }
+      else
+       {
+         hp = local_gethostbyname();
+         if (hp)
+           {
+             cp = strchr(hp->h_name, '.');
+             if (cp)
+               domain = strdup(++cp);
+           }
+       }
+      if (!domain)
+       domain = "";
+    }
+
+  return domain;
+}
 
 /*
  * Canonicalize hostname:
@@ -38,61 +90,52 @@ static char *rcsid_fixhost_c = "$Header$";
 char *canonicalize_hostname(char *host)
 {
   struct hostent *hp;
-  int n_len;
-  int has_dot = 0;
-  char tbuf[BUFSIZ];
-  struct utsname name;
-  char *cp;
+  int len;
+  char *tbuf, *cp;
 
-  if (strlen(host) > 2 && host[0] == '"' && host[strlen(host) - 1] == '"')
+  len = strlen(host);
+  if (len > 2 && host[0] == '"' && host[len - 1] == '"')
     {
-      strcpy(tbuf, host + 1);
+      tbuf = malloc(len - 1);
+      if (!tbuf)
+       return NULL;
+      strncpy(tbuf, host + 1, len - 2);
+      tbuf[len - 2] = '\0';
       free(host);
-      tbuf[strlen(tbuf) - 1] = '\0';
-      return strsave(tbuf);
+      return tbuf;
     }
 
-  if (strchr(host, '*') || strchr(host, '?') || strchr(host, '['))
+  if (strchr(host, '*') || strchr(host, '?') || *host == '[')
     return host;
 
   hp = gethostbyname(host);
 
   if (hp)
     {
-      n_len = strlen(hp->h_name) + 1;
-      host = realloc(host, n_len);
-
-      strcpy(host, hp->h_name);
+      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++)
+      cp = strchr(host, '.');
+      if (!cp)
        {
-         int c;
-         if (islower(c = *cp))
-           *cp = toupper(c);
-         has_dot |= (c == '.');
-       }
-      if (!has_dot)
-       {
-         static char *domain = NULL;
-
-         if (domain == NULL)
-           {
-             uname(&name);
-             hp = gethostbyname(name.nodename);
-             cp = strchr(hp->h_name, '.');
-             if (cp)
-               domain = strsave(++cp);
-             else
-               domain = "";
-           }
-         sprintf(tbuf, "%s.%s", host, domain);
+         tbuf = malloc(strlen(host) + strlen(local_domain()) + 2);
+         if (!tbuf)
+           return NULL;
+         sprintf(tbuf, "%s.%s", host, local_domain());
          free(host);
-         host = strsave(tbuf);
+         host = tbuf;
        }
+      else if (strcasecmp(cp + 1, local_domain()) != 0)
+       return host;
+
+      /* This is a host in our local domain, so capitalize it. */
+      for (cp = host; *cp; cp++)
+       *cp = toupper(*cp);
       return host;
     }
 }
This page took 0.039202 seconds and 4 git commands to generate.