From 5581962e7b94954431da277071a53a032c67a240 Mon Sep 17 00:00:00 2001
From: Dirk Zimoch <dirk.zimoch@psi.ch>
Date: Mon, 12 Oct 2015 14:48:05 +0200
Subject: [PATCH] minor fixes

---
 postModuleLoad.cmd |  2 +-
 require.c          | 29 +++++++++++------------------
 2 files changed, 12 insertions(+), 19 deletions(-)

diff --git a/postModuleLoad.cmd b/postModuleLoad.cmd
index 8cc206ab..276e7828 100644
--- a/postModuleLoad.cmd
+++ b/postModuleLoad.cmd
@@ -1 +1 @@
-dbLoadRecords("$(require_DIR)/db/moduleversion.template","IOC=$(IOC),MODULE=$(MODULE),VERSION=$($(MODULE)_VERSION=)"
+dbLoadRecords("$(require_DIR)db/moduleversion.template","IOC=$(IOC),MODULE=$(MODULE),VERSION=$($(MODULE)_VERSION=)"
diff --git a/require.c b/require.c
index 8d36c28c..216ba8a4 100644
--- a/require.c
+++ b/require.c
@@ -264,13 +264,19 @@ static int runLoadScript(const char* script, const char* module, const char* ver
     return 0;
 }
 
-static void setupDbPath(const char* module, const char* dbdir)
+static int setupDbPath(const char* module, const char* dbdir)
 {
     char* old_path;           
     char* p;
     size_t len;
 
     char* absdir = realpath(dbdir, NULL); /* so we can change directory later safely */
+    if (absdir == NULL)
+    {
+        if (requireDebug)
+            printf("require: cannot resolve %s\n", dbdir);
+        return -1;
+    }
     len = strlen(absdir);
 
     if (requireDebug)
@@ -320,6 +326,7 @@ static void setupDbPath(const char* module, const char* dbdir)
                  absdir, old_path);
     }
     free(absdir);
+    return 0;
 }
 
 static void registerModule(const char* module, const char* version, const char* location)
@@ -1322,30 +1329,16 @@ loadlib:
     if (requireDebug)
         printf("require: looking for template directory\n");
     /* filename = "<dirname>/[dirlen]<module>/<version>/R<epicsRelease>/[releasediroffs]..." */
-    if (TRY_FILE(releasediroffs, TEMPLATEDIR) ||
-        TRY_FILE(releasediroffs, ".." OSI_PATH_SEPARATOR TEMPLATEDIR))
-    {
-        setupDbPath(module, filename);
-    }
-    else
+    if (!((TRY_FILE(releasediroffs, TEMPLATEDIR) ||
+        TRY_FILE(releasediroffs, ".." OSI_PATH_SEPARATOR TEMPLATEDIR)) && setupDbPath(module, filename)))
     {
+        /* if no template directory found, restore TEMPLATES to initial value */
         char *t;
         t = getenv("TEMPLATES");
         if (globalTemplates && (!t || strcmp(globalTemplates, t) != 0))
             putenvprintf("TEMPLATES=%s", globalTemplates);
     }
 
-#define SETUP_PATH(NAME, args...) \
-    if (TRY_FILE(releasediroffs, args)) \
-    { \
-        putenvprintf("%s_" #NAME "=%s", module, filename); \
-        putenvprintf(#NAME "=%s", filename); \
-    }\
-    else \
-    { \
-        putenvprintf(#NAME "=."); \
-    }
-
     if (loaded && args == NULL) return 0; /* no need to execute startup script twice if not with new arguments */
 
     /* load startup script */
-- 
GitLab