]> 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 c0c39ddf32324f269bd523179b959a7847b3a958..d9797a001dd49b173faa62c16f69baefa462aef0 100644 (file)
@@ -38,31 +38,32 @@ RCSID("$Header$");
 char *canonicalize_hostname(char *host)
 {
   struct hostent *hp;
-  int n_len;
+  int len;
   int has_dot = 0;
-  char tbuf[BUFSIZ];
-  struct utsname name;
-  char *cp;
+  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 strdup(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
@@ -70,10 +71,9 @@ char *canonicalize_hostname(char *host)
       /* can't get name from nameserver; fix up the format a bit */
       for (cp = host; *cp; cp++)
        {
-         int c;
-         if (islower(c = *cp))
-           *cp = toupper(c);
-         has_dot |= (c == '.');
+         if (islower(*cp))
+           *cp = toupper(*cp);
+         has_dot |= (*cp == '.');
        }
       if (!has_dot)
        {
@@ -81,17 +81,35 @@ char *canonicalize_hostname(char *host)
 
          if (domain == NULL)
            {
-             uname(&name);
-             hp = gethostbyname(name.nodename);
-             cp = strchr(hp->h_name, '.');
-             if (cp)
-               domain = strdup(++cp);
+             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 = strdup(tbuf);
+         host = tbuf;
        }
       return host;
     }
This page took 0.035717 seconds and 4 git commands to generate.