]> andersk Git - moira.git/blobdiff - lib/fixhost.c
Command line printer manipulation client, and build goo.
[moira.git] / lib / fixhost.c
index dc7789a956ebd9aabf01531e14928c25f9af212d..ae574ada446fc7fb96a86dc9e39ab7c54b96ffe1 100644 (file)
 #include <moira.h>
 
 #include <sys/types.h>
-#include <sys/socket.h>
+
+#ifdef HAVE_UNAME
 #include <sys/utsname.h>
+#endif
 
+#ifndef _WIN32
+#include <sys/socket.h>
 #include <netdb.h>
 #include <netinet/in.h>
+#endif /* _WIN32 */
 
 #include <ctype.h>
 #include <stdio.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:
  *  if it is in double-quotes, then strip the quotes and return the name.
@@ -39,7 +91,6 @@ char *canonicalize_hostname(char *host)
 {
   struct hostent *hp;
   int len;
-  int has_dot = 0;
   char *tbuf, *cp;
 
   len = strlen(host);
@@ -49,12 +100,12 @@ char *canonicalize_hostname(char *host)
       if (!tbuf)
        return NULL;
       strncpy(tbuf, host + 1, len - 2);
-      tbuf[len - 1] = '\0';
+      tbuf[len - 2] = '\0';
       free(host);
       return tbuf;
     }
 
-  if (strchr(host, '*') || strchr(host, '?'))
+  if (strchr(host, '*') || strchr(host, '?') || *host == '[')
     return host;
 
   hp = gethostbyname(host);
@@ -69,40 +120,22 @@ char *canonicalize_hostname(char *host)
   else
     {
       /* can't get name from nameserver; fix up the format a bit */
-      for (cp = host; *cp; cp++)
+      cp = strchr(host, '.');
+      if (!cp)
        {
-         if (islower(*cp))
-           *cp = toupper(*cp);
-         has_dot |= (*cp == '.');
-       }
-      if (!has_dot)
-       {
-         static char *domain = NULL;
-
-         if (domain == NULL)
-           {
-             struct utsname name;
-
-             uname(&name);
-             hp = gethostbyname(name.nodename);
-             if (hp)
-               {
-                 cp = strchr(hp->h_name, '.');
-                 if (cp)
-                   domain = strdup(++cp);
-                 if (!domain)
-                   domain = "";
-               }
-             else
-               domain = "";
-           }
-         tbuf = malloc(strlen(host) + strlen(domain) + 2);
+         tbuf = malloc(strlen(host) + strlen(local_domain()) + 2);
          if (!tbuf)
            return NULL;
-         sprintf(tbuf, "%s.%s", host, domain);
+         sprintf(tbuf, "%s.%s", host, local_domain());
          free(host);
          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.032545 seconds and 4 git commands to generate.