Revision: 75884
          http://sourceforge.net/p/brlcad/code/75884
Author:   starseeker
Date:     2020-05-21 19:37:16 +0000 (Thu, 21 May 2020)
Log Message:
-----------
Set up the libbu fallback as something that can be called if the other 
mechanisms fail, not just as a compile-time fallback if system methods aren't 
available.

Modified Paths:
--------------
    brlcad/trunk/src/libbu/whereami.c

Modified: brlcad/trunk/src/libbu/whereami.c
===================================================================
--- brlcad/trunk/src/libbu/whereami.c   2020-05-21 19:05:08 UTC (rev 75883)
+++ brlcad/trunk/src/libbu/whereami.c   2020-05-21 19:37:16 UTC (rev 75884)
@@ -89,6 +89,54 @@
 #error unsupported compiler
 #endif
 
+/* LIBBU fallback implementation */
+static
+int _bu_getExecutablePath(char* out, int capacity, int* dirname_length)
+{
+    const char *pname = _bu_progname_raw();
+    struct bu_vls epath = BU_VLS_INIT_ZERO;
+
+    if (pname[0] == '.') {
+        char iwd[MAXPATHLEN];
+        char fullpath[MAXPATHLEN];
+        bu_getiwd(iwd, MAXPATHLEN);
+       // Use a VLS for this, since in principle there's nothing stopping the
+       // iwd and the bu_progname each individually from running right up to
+       // MAXPATHLEN, and if they do a MAXPATHLEN buffer won't hold both of
+       // them for realpath to try and digest down into something sane.
+        bu_vls_sprintf(&epath, "%s%c%s", iwd, BU_DIR_SEPARATOR, pname);
+        if (!bu_file_realpath(bu_vls_cstr(&epath), fullpath)) {
+            /* Unable to resolve initial path concatentation */
+           bu_vls_free(&epath);
+           return -1;
+        }
+       bu_vls_sprintf(&epath, "%s", fullpath);
+    } else {
+       bu_vls_sprintf(&epath, "%s", pname);
+    }
+
+    int length = bu_vls_strlen(&epath);
+    if (length <= capacity) {
+
+       memcpy(out, bu_vls_cstr(&epath), length);
+
+       if (dirname_length) {
+           int i;
+           for (i = length - 1; i >= 0; --i) {
+               if (out[i] == '/') {
+                   *dirname_length = i;
+                   break;
+               }
+           }
+       }
+    }
+
+    bu_vls_free(&epath);
+
+    return length;
+}
+
+
 #if defined(_WIN32)
 
 #define WIN32_LEAN_AND_MEAN
@@ -260,6 +308,10 @@
     break;
   }
 
+  if (length <= 0) {
+    return _bu_getExecutablePath(out, capacity, dirname_length);
+  }
+
   return length;
 }
 
@@ -452,6 +504,10 @@
   if (path != buffer1)
     WAI_FREE(path);
 
+  if (length <= 0) {
+    return _bu_getExecutablePath(out, capacity, dirname_length);
+  }
+
   return length;
 }
 
@@ -558,6 +614,10 @@
 
   fclose(self_exe);
 
+  if (length <= 0) {
+    return _bu_getExecutablePath(out, capacity, dirname_length);
+  }
+
   return length;
 }
 
@@ -666,6 +726,10 @@
   if (path != buffer1)
     WAI_FREE(path);
 
+  if (length <= 0) {
+    return _bu_getExecutablePath(out, capacity, dirname_length);
+  }
+
   return length;
 }
 
@@ -719,47 +783,7 @@
 WAI_FUNCSPEC
 int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
 {
-    const char *pname = _bu_progname_raw();
-    struct bu_vls epath = BU_VLS_INIT_ZERO;
-
-    if (pname[0] == '.') {
-        char iwd[MAXPATHLEN];
-        char fullpath[MAXPATHLEN];
-        bu_getiwd(iwd, MAXPATHLEN);
-       // Use a VLS for this, since in principle there's nothing stopping the
-       // iwd and the bu_progname each individually from running right up to
-       // MAXPATHLEN, and if they do a MAXPATHLEN buffer won't hold both of
-       // them for realpath to try and digest down into something sane.
-        bu_vls_sprintf(&epath, "%s%c%s", iwd, BU_DIR_SEPARATOR, pname);
-        if (!bu_file_realpath(bu_vls_cstr(&epath), fullpath)) {
-            /* Unable to resolve initial path concatentation */
-           bu_vls_free(&epath);
-           return -1;
-        }
-       bu_vls_sprintf(&epath, "%s", fullpath);
-    } else {
-       bu_vls_sprintf(&epath, "%s", pname);
-    }
-
-    int length = bu_vls_strlen(&epath);
-    if (length <= capacity) {
-
-       memcpy(out, bu_vls_cstr(&epath), length);
-
-       if (dirname_length) {
-           int i;
-           for (i = length - 1; i >= 0; --i) {
-               if (out[i] == '/') {
-                   *dirname_length = i;
-                   break;
-               }
-           }
-       }
-    }
-
-    bu_vls_free(&epath);
-
-    return length;
+    return _bu_getExecutablePath(out, capacity, dirname_length);
 }
 
 WAI_NOINLINE WAI_FUNCSPEC

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to