]> granicus.if.org Git - graphviz/commitdiff
qualify configs by hostname in case NFS mounted home on multiple platforms
authorellson <devnull@localhost>
Mon, 18 Apr 2005 13:58:50 +0000 (13:58 +0000)
committerellson <devnull@localhost>
Mon, 18 Apr 2005 13:58:50 +0000 (13:58 +0000)
configure.ac
lib/gvc/Makefile.am
lib/gvc/gvc.h
lib/gvc/gvconfig.c
lib/gvc/gvhostname.c [new file with mode: 0644]

index 7813f2c71beec7f5289d988e1d3fb10711a05c8b..cf6285c9a214fe4c984be6c4ac195f6b44564089 100644 (file)
@@ -1229,7 +1229,7 @@ AC_FUNC_ALLOCA
 
 AC_CHECK_FUNCS([lrand48 drand48 srand48 setmode setenv getenv \
        __freadable _sysconf getrusage strerror cbrt lsqrt vsnprintf \
-       strtoul strtoll strtoull feenableexcept fesetenv])
+       strtoul strtoll strtoull feenableexcept fesetenv uname])
 
 AC_REPLACE_FUNCS([strcasecmp strncasecmp])
 
index 829a3a07601545faccc4911104898b1c8fb4a687..8632772c4a65ba491ce890c231618a2c5f5a6319 100644 (file)
@@ -15,6 +15,6 @@ pkginclude_HEADERS = gvc.h gvcint.h gvplugin.h \
 pkglib_LTLIBRARIES = libgvc.la
 
 libgvc_la_SOURCES = gvrender.c gvlayout.c gvtext.c gvcontext.c \
-       gvjobs.c gvevent.c gvplugin.c gvconfig.c
+       gvjobs.c gvevent.c gvplugin.c gvconfig.c gvhostname.c
 
 EXTRA_DIST = Makefile.old
index c803556daa75e1d850f66a43bf0cb89c9e1b0745..19cee5e6fd01777af866e112c9ea0c25f06a24cd 100644 (file)
@@ -34,6 +34,7 @@ extern "C" {
 /* configuration */
 
     extern void gvconfig(GVC_t * gvc);
+    extern char *gvhostname(void);
 
 /* plugins */
 
index 69cf58c85f3a326d3b74c00ef1dce524d6cfc219..fc846e09dda223d619f2f4e4c6ae08eeedfd6d65 100644 (file)
@@ -218,9 +218,10 @@ void gvconfig(GVC_t * gvc)
     glob_t globbuf;
 
     char *config_dir_name = ".graphviz";
+    char *config_file_name = "config";
     char *libdir = GVLIBDIR;
     char *plugin_glob = "libgvplugin*.so.?";
-    char *s, *config_file_name;
+    char *s;
 
 #define MAX_SZ_CONFIG 100000
 #endif
@@ -247,14 +248,13 @@ void gvconfig(GVC_t * gvc)
        rc = -1;
     }
     else {
-        config_file_name = s = strdup(libdir);
-        while ((s = strchr(s,'/')))
-           *s = '+';
-    
+       s = gvhostname();
         config_path = malloc(strlen(home) 
                                + 1
                                + strlen(config_dir_name)
                                + 1
+                               + strlen(s)
+                               + 1
                                + strlen(config_file_name)
                                + 1);
         strcpy(config_path, home);
@@ -263,10 +263,12 @@ void gvconfig(GVC_t * gvc)
         rc = mkdir(config_path, 0700);
 
         strcat(config_path, "/");
+       if (s[0]) {
+           strcat(config_path, s);
+           strcat(config_path, "_");
+       }
         strcat(config_path, config_file_name);
 
-       free(config_file_name);
-
         rc = stat(config_path, &config_st);
     }
        
diff --git a/lib/gvc/gvhostname.c b/lib/gvc/gvhostname.c
new file mode 100644 (file)
index 0000000..9889857
--- /dev/null
@@ -0,0 +1,146 @@
+/* $Id$ $Revision$ */
+/* vim:set shiftwidth=4 ts=8: */
+
+/**********************************************************
+*      This software is part of the graphviz package      *
+*                http://www.graphviz.org/                 *
+*                                                         *
+*            Copyright (c) 1994-2004 AT&T Corp.           *
+*                and is licensed under the                *
+*            Common Public License, Version 1.0           *
+*                      by AT&T Corp.                      *
+*                                                         *
+*        Information and Software Systems Research        *
+*              AT&T Research, Florham Park NJ             *
+**********************************************************/
+
+
+/* This code is adapted from tclInixSock.c from Tcl sources. */
+
+#include "config.h"
+
+#ifdef HAVE_UNAME
+#include <sys/utsname.h>
+#endif
+#include <netdb.h>
+
+#include <string.h>
+
+/*
+ * There is no portable macro for the maximum length
+ * of host names returned by gethostbyname().  We should only
+ * trust SYS_NMLN if it is at least 255 + 1 bytes to comply with DNS
+ * host name limits.
+ *
+ * Note:  SYS_NMLN is a restriction on "uname" not on gethostbyname!
+ *
+ * For example HP-UX 10.20 has SYS_NMLN == 9,  while gethostbyname()
+ * can return a fully qualified name from DNS of up to 255 bytes.
+ *
+ * Fix suggested by Viktor Dukhovni (viktor@esm.com)
+ */
+
+#if defined(SYS_NMLN) && SYS_NMLEN >= 256
+#define HOSTNAME_LEN SYS_NMLEN
+#else
+#define HOSTNAME_LEN 256
+#endif
+
+/*
+ * The following variable holds the network name of this host.
+ */
+
+static char hostname[HOSTNAME_LEN + 1];
+static int  hostnameInited = 0;
+#if 0
+TCL_DECLARE_MUTEX(hostMutex)
+#endif
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * gvhostname --
+ *
+ *      Returns the name of the local host.
+ *
+ * Results:
+ *      A string containing the network name for this machine, or
+ *      an empty string if we can't figure out the name.  The caller
+ *      must not modify or free this string.
+ *
+ * Side effects:
+ *      None.
+ *
+ *----------------------------------------------------------------------
+ */
+
+char *
+gvhostname(void)
+{
+#ifdef HAVE_UNAME
+    struct utsname u;
+    struct hostent *hp;
+#else
+    char buffer[sizeof(hostname)];
+#endif
+    char *native;
+
+#if 0
+    Tcl_MutexLock(&hostMutex);
+#endif
+    if (hostnameInited) {
+#if 0
+       Tcl_MutexUnlock(&hostMutex);
+#endif
+        return hostname;
+    }
+
+    native = NULL;
+#ifdef HAVE_UNAME
+    memset((void *) &u, (int) 0, sizeof(struct utsname));
+    if (uname(&u) > -1) {                               /* INTL: Native. */
+        hp = gethostbyname(u.nodename);                 /* INTL: Native. */
+        if (hp == NULL) {
+            /*
+             * Sometimes the nodename is fully qualified, but gets truncated
+             * as it exceeds SYS_NMLN.  See if we can just get the immediate
+             * nodename and get a proper answer that way.
+             */
+            char *dot = strchr(u.nodename, '.');
+            if (dot != NULL) {
+                char *node = malloc((unsigned) (dot - u.nodename + 1));
+                memcpy(node, u.nodename, (size_t) (dot - u.nodename));
+                node[dot - u.nodename] = '\0';
+                hp = gethostbyname(node);
+                free(node);
+            }
+        }
+        if (hp != NULL) {
+            native = hp->h_name;
+        } else {
+            native = u.nodename;
+        }
+    }
+#else
+    /*
+     * Uname doesn't exist; try gethostname instead.
+     */
+
+    if (gethostname(buffer, sizeof(buffer)) > -1) {     /* INTL: Native. */
+        native = buffer;
+    }
+#endif
+
+    if (native == NULL) {
+        hostname[0] = 0;
+    } else {
+       strncpy(hostname, native, sizeof(hostname)-1);
+        hostname[sizeof(hostname)-1] = 0;
+    }
+    hostnameInited = 1;
+#if 0
+    Tcl_MutexUnlock(&hostMutex);
+#endif
+    return hostname;
+}
+