Partially sync files.el from XEmacs 21.5 for wildcard support.
[sxemacs] / src / dumper.c
index 1b782a9..8e72ea4 100644 (file)
@@ -34,6 +34,9 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 #include <sys/mman.h>
 #endif
 
+/* For PATH_EXEC */
+#include <sxe-paths.h>
+
 #ifndef SEPCHAR
 #define SEPCHAR ':'
 #endif
@@ -1266,58 +1269,26 @@ static int pdump_file_get(const char *path)
        return 1;
 }
 
-static int pdump_file_try(char *exe_path, size_t size)
+static int
+pdump_file_try(char *exe_path, size_t size)
 {
-       char *w = exe_path + strlen(exe_path);
-       int sz;
-       size -= strlen(exe_path);
-
-       do {
-
-#ifdef EMACS_PATCH_LEVEL
-               sz = snprintf(w, size, "-%d.%d.%d-%08x.dmp",
-                        EMACS_MAJOR_VERSION, EMACS_MINOR_VERSION,
-                        EMACS_PATCH_LEVEL, dump_id);
-               if (sz >=0 && sz < size && pdump_file_get(exe_path)) {
-                       if (pdump_load_check()) {
-                               return 1;
-                       }
-                       pdump_free();
-               }
-#endif /* EMACS_PATCH_LEVEL */
-#ifdef EMACS_BETA_VERSION
-               sz = snprintf(w, size, "-%d.%d.%d-%08x.dmp",
-                        EMACS_MAJOR_VERSION, EMACS_MINOR_VERSION,
-                        EMACS_BETA_VERSION, dump_id);
-               if (sz >=0 && (size_t)sz < size && pdump_file_get(exe_path)) {
+       static const char pdump_ext[] = ".dmp";
+       size_t exe_path_len = strlen(exe_path);
+       char *w = exe_path + exe_path_len;
+
+       if (exe_path_len + sizeof(pdump_ext) <= size) {
+               /* just hammer the pdump extension onto w */
+               memcpy(w, pdump_ext, sizeof(pdump_ext));
+               /* exists? */
+               if (pdump_file_get(exe_path)) {
+                       /* works? */
                        if (pdump_load_check()) {
                                return 1;
                        }
+                       /* bugger */
                        pdump_free();
                }
-#endif /* EMACS_BETA_VERSION */
-
-               sz = snprintf(w, size, "-%08x.dmp", dump_id);
-               if (sz >=0 && (size_t)sz < size && pdump_file_get(exe_path)) {
-                       if (pdump_load_check()) {
-                               return 1;
-                       }
-                       pdump_free();
-               }
-
-               sz = snprintf(w, size, ".dmp");
-               if (sz >=0 && (size_t)sz < size && pdump_file_get(exe_path)) {
-                       if (pdump_load_check()) {
-                               return 1;
-                       }
-                       pdump_free();
-               }
-
-               do {
-                       w--;
-               } while (w > exe_path && !IS_DIRECTORY_SEP(*w) && (*w != '-')
-                        && (*w != '.'));
-       } while (w > exe_path && !IS_DIRECTORY_SEP(*w));
+       }
        return 0;
 }
 
@@ -1356,6 +1327,7 @@ wipe_out_libtool_stuff(char *path)
 int pdump_load(const char *argv0)
 {
        char exe_path[PATH_MAX], real_exe_path[PATH_MAX];
+       char libarchdir_path[PATH_MAX] = PATH_EXEC "/sxemacs";
        char *w;
        const char *dir, *p;
 
@@ -1375,7 +1347,7 @@ int pdump_load(const char *argv0)
                /* invocation-name includes a directory component -- presumably it
                   is relative to cwd, not $PATH */
                assert(strlen(dir) < sizeof(exe_path));
-               strncpy(exe_path, dir, sizeof(exe_path)-1);
+               xstrncpy(exe_path, dir, sizeof(exe_path));
                exe_path[sizeof(exe_path)-1]='\0';
        } else {
                const char *path = getenv("PATH");
@@ -1407,8 +1379,7 @@ int pdump_load(const char *argv0)
                                remain--;
                        }
                        if (remain > 0) {
-                               strncpy(w, name, remain);
-                               w[remain]='\0';
+                               xstrncpy(w, name, remain);
                        }
 
                        /* Check that exe_path is executable and not a
@@ -1442,10 +1413,11 @@ int pdump_load(const char *argv0)
 
        /* Save exe_path because pdump_file_try() modifies it */
        assert(strlen(exe_path) < sizeof(real_exe_path));
-       strcpy(real_exe_path, exe_path);
+       xstrncpy(real_exe_path, exe_path, sizeof(real_exe_path));
        if (pdump_file_try(exe_path,sizeof(exe_path))
            || (xrealpath(real_exe_path, real_exe_path)
-               && pdump_file_try(real_exe_path,sizeof(real_exe_path)))) {
+               && pdump_file_try(real_exe_path,sizeof(real_exe_path)))
+           || pdump_file_try(libarchdir_path,sizeof(libarchdir_path)) ) {
                pdump_load_finish();
                return 1;
        }