Partially sync files.el from XEmacs 21.5 for wildcard support.
[sxemacs] / src / emodules-ng.c
index 5bd1f9c..6b3c851 100644 (file)
@@ -150,6 +150,7 @@ __emodng_open_append_exts(const char *filename)
        char name[nname + /*length of longest extension:*/24];
        /* note to myself: better check that length! */
        char *p = xstpncpy(name, filename, nname+1);
+       size_t remain = name + sizeof(name) - p;
 
        for (Lisp_Object ext = Vmodule_extensions;
             CONSP(ext); ext = XCDR(ext)) {
@@ -157,8 +158,8 @@ __emodng_open_append_exts(const char *filename)
                        continue;
                }
 
-               strncpy(p, (const char*)XSTRING_DATA(XCAR(ext)),
-                       (size_t)XSTRING_LENGTH(XCAR(ext))+1);
+               xstrncpy(p, (const char*)XSTRING_DATA(XCAR(ext)),
+                        remain);
 
                if (__file_exists_p(name)) {
                        EMOD_DEBUG_LOADER("trying %s\n", name);
@@ -189,9 +190,10 @@ __emodng_open_prepend_paths(const char *filename)
                        char *p;
                        p = xstpncpy(name,
                                     (const char*)XSTRING_DATA(XCAR(path)),
-                                    (size_t)XSTRING_LENGTH(XCAR(path)));
+                                    sizeof(name)-2);
                        *p++ = '/';
-                       strncpy(p, filename, nfilename+1);
+                       *p = '\0';
+                       xstrncpy(p, filename, name + sizeof(name) - p);
 
                        if (__file_exists_p(name)) {
                                EMOD_DEBUG_LOADER("trying %s\n", name);
@@ -222,14 +224,17 @@ __emodng_open_prepend_paths_append_exts(const char *filename)
                         * carries the correct extension
                         * see __emodng_open_prepend_paths_append_exts for
                         * cope with all the combinations */
-                       char *p = xstpncpy(
-                               name,
+                       char *p = xstpncpy(name,
                                (const char*)XSTRING_DATA(XCAR(path)),
-                               (size_t)XSTRING_LENGTH(XCAR(path)));
+                               sizeof(name)-2);
+                       int remain;
                        if (*(p-1) != '/') {
                                *p++ = '/';
+                               *p = '\0';
                        }
-                       p = xstpncpy(p, filename, nfilename);
+                       remain = name+sizeof(name)-p;
+                       p = xstpncpy(p, filename, remain);
+                       remain = name+sizeof(name)-p;
 
                        /* append all extensions now */
                        for (Lisp_Object ext = Vmodule_extensions;
@@ -238,9 +243,8 @@ __emodng_open_prepend_paths_append_exts(const char *filename)
                                        continue;
                                }
 
-                               strncpy(p,
-                                       (const char*)XSTRING_DATA(XCAR(ext)),
-                                       (size_t)XSTRING_LENGTH(XCAR(ext))+1);
+                               xstrncpy(p,(const char*)XSTRING_DATA(XCAR(ext)),
+                                        remain);
 
                                if (__file_exists_p(name)) {
                                        EMOD_DEBUG_LOADER("trying \"%s\"\n",
@@ -844,7 +848,7 @@ _adapt_load_path(Lisp_Object sym, Lisp_Object *val,
                        if (LIKELY((STRINGP(lse)))) {
                                p = xstpncpy(p,
                                             (const char*)XSTRING_DATA(lse),
-                                            XSTRING_LENGTH(lse));
+                                            sp+sizeof(sp)-p-1);
                                *p++ = ':';
                        }
                }