#ifdef FILE_CODING
#include "mule/file-coding.h"
#endif
+#include "ent/ent.h"
#ifdef HAVE_LIBGEN_H /* Must come before sysfile.h */
#include <libgen.h>
return Ffile_name_directory(filename);
}
\f
-static char *file_name_as_directory(char *out, char *in)
+static char *file_name_as_directory(char *out, const char *in, size_t len)
{
/* This function cannot GC */
int size = strlen(in);
if (size == 0) {
+ assert(len >= 3);
out[0] = '.';
out[1] = DIRECTORY_SEP;
out[2] = '\0';
} else {
- strcpy(out, in);
+ xstrncpy(out, in, len);
/* Append a slash if necessary */
if (!IS_ANY_SEP(out[size - 1])) {
out[size] = DIRECTORY_SEP;
(filename))
{
/* This function can GC. GC checked 2000-07-28 ben */
- char *buf;
- Lisp_Object handler;
+ size_t len = 0;
+ char *buf = NULL;
+ Lisp_Object handler = Qnil;
CHECK_STRING(filename);
return call2_check_string(handler, Qfile_name_as_directory,
filename);
- buf = (char *)alloca(XSTRING_LENGTH(filename) + 10);
+ len = XSTRING_LENGTH(filename) + 10;
+ buf = (char *)alloca(len);
return build_string(file_name_as_directory
- (buf, (char *)XSTRING_DATA(filename)));
+ (buf, (char*)XSTRING_DATA(filename), len));
}
\f
/*
* Value is nonzero if the string output is different from the input.
*/
-static int directory_file_name(const char *src, char *dst)
+static int directory_file_name(const char *src, char *dst, size_t len)
{
/* This function cannot GC */
long slen = strlen(src);
/* Process as Unix format: just remove any final slash.
But leave "/" unchanged; do not change it to "". */
- strcpy(dst, src);
+ xstrncpy(dst, src, len);
if (slen > 1 && IS_DIRECTORY_SEP(dst[slen - 1])
)
dst[slen - 1] = 0;
(directory))
{
/* This function can GC. GC checked 2000-07-28 ben */
- char *buf;
+ size_t len = 0;
+ char *buf = NULL;
Lisp_Object handler;
CHECK_STRING(directory);
if (!NILP(handler))
return call2_check_string(handler, Qdirectory_file_name,
directory);
- buf = (char *)alloca(XSTRING_LENGTH(directory) + 20);
- directory_file_name((char *)XSTRING_DATA(directory), buf);
+ len = XSTRING_LENGTH(directory) + 20;
+ buf = (char *)alloca(len);
+ directory_file_name((char *)XSTRING_DATA(directory), buf, len);
return build_string(buf);
}
\f
if (newdir) {
if (nm[0] == 0 || IS_DIRECTORY_SEP(nm[0]))
- strcpy((char *)target, (char *)newdir);
+ xstrncpy((char *)target, (char *)newdir, tlen);
else
- file_name_as_directory((char *)target, (char *)newdir);
+ file_name_as_directory((char *)target, (char *)newdir, tlen);
}
- strcat((char *)target, (char *)nm);
+ xstrncat((char *)target, (char *)nm, tlen-1);
/* ASSERT (IS_DIRECTORY_SEP (target[0])) if not VMS */
Bufbyte *target = 0;
int total = 0;
int substituted = 0;
+ size_t avail = 0;
Bufbyte *xnm;
Lisp_Object handler;
/* If substitution required, recopy the filename and do it */
/* Make space in stack frame for the new copy */
- xnm = (Bufbyte *) alloca(XSTRING_LENGTH(filename) + total + 1);
+ avail = XSTRING_LENGTH(filename) + total + 1;
+ xnm = (Bufbyte *) alloca(avail);
x = xnm;
/* Copy the rest of the name through, replacing $ constructs with values */
for (p = nm; *p;)
- if (*p != '$')
+ if (*p != '$') {
*x++ = *p++;
- else {
+ avail--;
+ } else {
p++;
if (p == endp)
goto badsubst;
else if (*p == '$') {
*x++ = *p++;
+ avail--;
continue;
} else if (*p == '{') {
o = ++p;
if (!o)
goto badvar;
- strcpy((char *)x, (char *)o);
+ xstrncpy((char *)x, (char *)o, avail);
x += strlen((char *)o);
+ avail -= strlen((char *)o);
}
*x = 0;
O_RDONLY | OPEN_BINARY, 0);
if (ifd < 0) {
report_file_error("Opening input file", list1(filename));
+ goto end;
}
record_unwind_protect(close_file_unwind, make_int(ifd));
report_file_error("I/O error", list1(newname));
}
+ if(close(ifd) <0)
+ report_file_error("I/O error", list1(filename));
/* Closing the output clobbers the file times on some systems. */
if (close(ofd) < 0)
report_file_error("I/O error", list1(newname));
unbind_to(speccount, Qnil);
}
+end:
UNGCPRO;
return Qnil;
}
if (listdesc < 0 && !auto_saved && STRINGP(listfile))
unlink((char *)XSTRING_DATA(listfile));
+ if (listdesc >= 0)
+ close(listdesc);
+
/* Show "...done" only if the echo area would otherwise be empty. */
if (auto_saved && NILP(no_message)
&& NILP(clear_echo_area(selected_frame(), Qauto_saving, 0))) {