]> granicus.if.org Git - yasm/commitdiff
Make yasm dynamic module loader able to find modules even when run from a
authorPeter Johnson <peter@tortall.net>
Sat, 7 Feb 2004 22:28:40 +0000 (22:28 -0000)
committerPeter Johnson <peter@tortall.net>
Sat, 7 Feb 2004 22:28:40 +0000 (22:28 -0000)
different location.  The new code uses dirname() to add the argv[0] path to
the LTDL module loader search path.  Added new replacements for dirname and
basename functions from OpenBSD.

Bugzilla: Bug 24
Reported by: Simon Paulger <simonjpaulger@hotmail.com>

svn path=/trunk/yasm/; revision=1098

basename.c [new file with mode: 0644]
configure.ac
dirname.c [new file with mode: 0644]
frontends/yasm/yasm-module.c
frontends/yasm/yasm.c

diff --git a/basename.c b/basename.c
new file mode 100644 (file)
index 0000000..d0369bc
--- /dev/null
@@ -0,0 +1,64 @@
+/*      $OpenBSD: basename.c,v 1.11 2003/06/17 21:56:23 millert Exp $        */
+
+/*
+ * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef lint
+static char rcsid[] = "$OpenBSD: basename.c,v 1.11 2003/06/17 21:56:23 millert Exp $";
+#endif /* not lint */
+
+#include <string.h>
+#ifdef HAVE_SYS_PARAM_H
+#include <sys/param.h>
+#elif !defined(MAXPATHLEN)
+#define MAXPATHLEN 1024
+#endif
+
+char *
+basename(const char *path)
+{
+       static char bname[MAXPATHLEN];
+       register const char *endp, *startp;
+
+       /* Empty or NULL string gets treated as "." */
+       if (path == NULL || *path == '\0') {
+               (void)strcpy(bname, ".");
+               return(bname);
+       }
+
+       /* Strip trailing slashes */
+       endp = path + strlen(path) - 1;
+       while (endp > path && *endp == '/')
+               endp--;
+
+       /* All slashes become "/" */
+       if (endp == path && *endp == '/') {
+               (void)strcpy(bname, "/");
+               return(bname);
+       }
+
+       /* Find the start of the base */
+       startp = endp;
+       while (startp > path && *(startp - 1) != '/')
+               startp--;
+
+       if (endp - startp + 2 > sizeof(bname)) {
+               return(NULL);
+       }
+       (void)strncpy(bname, startp, endp - startp + 1);
+       bname[endp - startp + 1] = '\0';
+       return(bname);
+}
index 3aca6ff3ea9a052c2611c9ffe26c3063136eb0ee..ad8f4e78cfc23e6c3bdd07229c8d3b224e284f35 100644 (file)
@@ -125,12 +125,13 @@ AC_TYPE_SIZE_T
 AC_FUNC_FORK
 AC_FUNC_MALLOC
 AC_FUNC_VPRINTF
-AC_CHECK_FUNCS([abort basename memcpy memmove strrchr toascii vsnprintf])
+AC_CHECK_FUNCS([abort memcpy memmove strrchr toascii vsnprintf])
 AC_CHECK_FUNCS([strsep mergesort])
 # Look for the case-insensitive comparison functions
 AC_CHECK_FUNCS([strcasecmp strncasecmp stricmp strcmpi])
 # Check for stuff wanted by the test suite.  None of this is required.
 AC_CHECK_FUNCS([msgctl msgget msgrcv msgsnd strerror snprintf wait])
+AC_REPLACE_FUNCS([basename dirname])
 AC_LIB_LTDL
 
 #
diff --git a/dirname.c b/dirname.c
new file mode 100644 (file)
index 0000000..fb27663
--- /dev/null
+++ b/dirname.c
@@ -0,0 +1,67 @@
+/*      $OpenBSD: dirname.c,v 1.10 2003/06/17 21:56:23 millert Exp $        */
+
+/*
+ * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef lint
+static char rcsid[] = "$OpenBSD: dirname.c,v 1.10 2003/06/17 21:56:23 millert Exp $";
+#endif /* not lint */
+
+#include <string.h>
+#ifdef HAVE_SYS_PARAM_H
+#include <sys/param.h>
+#elif !defined(MAXPATHLEN)
+#define MAXPATHLEN 1024
+#endif
+
+char *
+dirname(const char *path)
+{
+       static char bname[MAXPATHLEN];
+       register const char *endp;
+
+       /* Empty or NULL string gets treated as "." */
+       if (path == NULL || *path == '\0') {
+               (void)strcpy(bname, ".");
+               return(bname);
+       }
+
+       /* Strip trailing slashes */
+       endp = path + strlen(path) - 1;
+       while (endp > path && *endp == '/')
+               endp--;
+
+       /* Find the start of the dir */
+       while (endp > path && *endp != '/')
+               endp--;
+
+       /* Either the dir is "/" or there are no slashes */
+       if (endp == path) {
+               (void)strcpy(bname, *endp == '/' ? "/" : ".");
+               return(bname);
+       } else {
+               do {
+                       endp--;
+               } while (endp > path && *endp == '/');
+       }
+
+       if (endp - path + 2 > sizeof(bname)) {
+               return(NULL);
+       }
+       (void)strncpy(bname, path, endp - path + 1);
+       bname[endp - path + 1] = '\0';
+       return(bname);
+}
index ddc80ba164c6d7a41c1cf3bfab93fb675abb1f14..df940f7a241e690fd7381f3624a87f5398617472 100644 (file)
@@ -35,6 +35,8 @@
 #include "yasm-module.h"
 
 
+extern char *basename(const char *path);
+
 extern const lt_dlsymlist lt_preloaded_symbols[];
 
 typedef struct module {
@@ -145,22 +147,6 @@ typedef struct list_module_data {
     size_t matches_num, matches_alloc;
 } list_module_data;
 
-#ifdef HAVE_BASENAME
-extern char *basename(const char *);
-#else
-static const char *
-basename(const char *path)
-{
-    const char *base;
-    base = strrchr(path, '/');
-    if (!base)
-       base = path;
-    else
-       base++;
-    return base;
-}
-#endif
-
 static int
 list_module_load(const char *filename, lt_ptr data)
 {
index a3c12cb7f1fe23d124316059a40009ef85adabbc..33e38fa3b9841e41b8b305d8656ed88b933525f6 100644 (file)
@@ -44,6 +44,8 @@
 #endif
 
 #ifndef WIN32
+extern char *dirname(const char *path);
+
 extern const lt_dlsymlist lt_preloaded_symbols[];
 #endif
 
@@ -271,10 +273,14 @@ main(int argc, char *argv[])
        const char *path = getenv(YASM_MODULE_PATH_ENV);
        if (path)
            errors = lt_dladdsearchdir(path);
+    }
 #if defined(YASM_MODULEDIR)
-       if (errors == 0)
-           errors = lt_dladdsearchdir(YASM_MODULEDIR);
+    if (errors == 0)
+       errors = lt_dladdsearchdir(YASM_MODULEDIR);
 #endif
+    if (errors == 0) {
+       /* Path where yasm executable is running from (argv[0]) */
+       errors = lt_dladdsearchdir(dirname(argv[0]));
     }
     if (errors != 0) {
        print_error(_("%s: module loader initialization failed"), _("FATAL"));