Commit c293e30c authored by Dmitry Antipov's avatar Dmitry Antipov

Avoid calls to strlen in path processing functions.

* fileio.c (file_name_as_directory): Add comment.  Change to add
srclen argument and return the length of result.  Adjust users
accordingly.
(directory_file_name): Fix comment.  Change to add srclen argument,
swap 1nd and 2st arguments to obey the common convention.  Adjust
users accordingly.
* filelock.c (fill_in_lock_file_name): Avoid calls to strlen.
parent af6e839f
2012-07-10 Dmitry Antipov <dmantipov@yandex.ru>
Avoid calls to strlen in path processing functions.
* fileio.c (file_name_as_directory): Add comment. Change to add
srclen argument and return the length of result. Adjust users
accordingly.
(directory_file_name): Fix comment. Change to add srclen argument,
swap 1nd and 2st arguments to obey the common convention. Adjust
users accordingly.
* filelock.c (fill_in_lock_file_name): Avoid calls to strlen.
2012-07-10 Glenn Morris <rgm@gnu.org> 2012-07-10 Glenn Morris <rgm@gnu.org>
* s/irix6-5.h (SETUP_SLAVE_PTY, PTY_NAME_SPRINTF): Drop ifdef guards. * s/irix6-5.h (SETUP_SLAVE_PTY, PTY_NAME_SPRINTF): Drop ifdef guards.
......
...@@ -453,32 +453,33 @@ get a current directory to run processes in. */) ...@@ -453,32 +453,33 @@ get a current directory to run processes in. */)
return Ffile_name_directory (filename); return Ffile_name_directory (filename);
} }
/* Convert from file name SRC of length SRCLEN to directory name
static char * in DST. On UNIX, just make sure there is a terminating /.
file_name_as_directory (char *out, const char *in) Return the length of DST. */
{
ptrdiff_t len = strlen (in);
if (len == 0) static ptrdiff_t
file_name_as_directory (char *dst, const char *src, ptrdiff_t srclen)
{
if (srclen == 0)
{ {
out[0] = '.'; dst[0] = '.';
out[1] = '/'; dst[1] = '/';
out[2] = 0; dst[2] = '\0';
return out; return 2;
} }
strcpy (out, in); strcpy (dst, src);
/* For Unix syntax, Append a slash if necessary */ if (!IS_DIRECTORY_SEP (dst[srclen - 1]))
if (!IS_DIRECTORY_SEP (out[len - 1]))
{ {
out[len] = DIRECTORY_SEP; dst[srclen] = DIRECTORY_SEP;
out[len + 1] = '\0'; dst[srclen + 1] = '\0';
srclen++;
} }
#ifdef DOS_NT #ifdef DOS_NT
dostounix_filename (out); dostounix_filename (dst);
#endif #endif
return out; return srclen;
} }
DEFUN ("file-name-as-directory", Ffile_name_as_directory, DEFUN ("file-name-as-directory", Ffile_name_as_directory,
...@@ -492,6 +493,7 @@ For a Unix-syntax file name, just appends a slash. */) ...@@ -492,6 +493,7 @@ For a Unix-syntax file name, just appends a slash. */)
(Lisp_Object file) (Lisp_Object file)
{ {
char *buf; char *buf;
ptrdiff_t length;
Lisp_Object handler; Lisp_Object handler;
CHECK_STRING (file); CHECK_STRING (file);
...@@ -511,39 +513,34 @@ For a Unix-syntax file name, just appends a slash. */) ...@@ -511,39 +513,34 @@ For a Unix-syntax file name, just appends a slash. */)
} }
buf = alloca (SBYTES (file) + 10); buf = alloca (SBYTES (file) + 10);
file_name_as_directory (buf, SSDATA (file)); length = file_name_as_directory (buf, SSDATA (file), SBYTES (file));
return make_specified_string (buf, -1, strlen (buf), return make_specified_string (buf, -1, length, STRING_MULTIBYTE (file));
STRING_MULTIBYTE (file));
} }
/* /* Convert from directory name SRC of length SRCLEN to
* Convert from directory name to filename. file name in DST. On UNIX, just make sure there isn't
* On UNIX, it's simple: just make sure there isn't a terminating / a terminating /. Return the length of DST. */
* Value is nonzero if the string output is different from the input. static ptrdiff_t
*/ directory_file_name (char *dst, char *src, ptrdiff_t srclen)
static int
directory_file_name (char *src, char *dst)
{ {
ptrdiff_t slen;
slen = strlen (src);
/* Process as Unix format: just remove any final slash. /* Process as Unix format: just remove any final slash.
But leave "/" unchanged; do not change it to "". */ But leave "/" unchanged; do not change it to "". */
strcpy (dst, src); strcpy (dst, src);
if (slen > 1 if (srclen > 1
&& IS_DIRECTORY_SEP (dst[slen - 1]) && IS_DIRECTORY_SEP (dst[srclen - 1])
#ifdef DOS_NT #ifdef DOS_NT
&& !IS_ANY_SEP (dst[slen - 2]) && !IS_ANY_SEP (dst[srclen - 2])
#endif #endif
) )
dst[slen - 1] = 0; {
dst[srclen - 1] = 0;
srclen--;
}
#ifdef DOS_NT #ifdef DOS_NT
dostounix_filename (dst); dostounix_filename (dst);
#endif #endif
return 1; return srclen;
} }
DEFUN ("directory-file-name", Fdirectory_file_name, Sdirectory_file_name, DEFUN ("directory-file-name", Fdirectory_file_name, Sdirectory_file_name,
...@@ -556,6 +553,7 @@ In Unix-syntax, this function just removes the final slash. */) ...@@ -556,6 +553,7 @@ In Unix-syntax, this function just removes the final slash. */)
(Lisp_Object directory) (Lisp_Object directory)
{ {
char *buf; char *buf;
ptrdiff_t length;
Lisp_Object handler; Lisp_Object handler;
CHECK_STRING (directory); CHECK_STRING (directory);
...@@ -576,9 +574,8 @@ In Unix-syntax, this function just removes the final slash. */) ...@@ -576,9 +574,8 @@ In Unix-syntax, this function just removes the final slash. */)
} }
buf = alloca (SBYTES (directory) + 20); buf = alloca (SBYTES (directory) + 20);
directory_file_name (SSDATA (directory), buf); length = directory_file_name (buf, SSDATA (directory), SBYTES (directory));
return make_specified_string (buf, -1, strlen (buf), return make_specified_string (buf, -1, length, STRING_MULTIBYTE (directory));
STRING_MULTIBYTE (directory));
} }
static const char make_temp_name_tbl[64] = static const char make_temp_name_tbl[64] =
...@@ -1130,8 +1127,9 @@ filesystem tree, not (expand-file-name ".." dirname). */) ...@@ -1130,8 +1127,9 @@ filesystem tree, not (expand-file-name ".." dirname). */)
} }
if (!IS_DIRECTORY_SEP (nm[0])) if (!IS_DIRECTORY_SEP (nm[0]))
{ {
char * tmp = alloca (strlen (newdir) + strlen (nm) + 2); ptrdiff_t newlen = strlen (newdir);
file_name_as_directory (tmp, newdir); char *tmp = alloca (newlen + strlen (nm) + 2);
file_name_as_directory (tmp, newdir, newlen);
strcat (tmp, nm); strcat (tmp, nm);
nm = tmp; nm = tmp;
} }
...@@ -1180,6 +1178,7 @@ filesystem tree, not (expand-file-name ".." dirname). */) ...@@ -1180,6 +1178,7 @@ filesystem tree, not (expand-file-name ".." dirname). */)
/* Get rid of any slash at the end of newdir, unless newdir is /* Get rid of any slash at the end of newdir, unless newdir is
just / or // (an incomplete UNC name). */ just / or // (an incomplete UNC name). */
length = strlen (newdir); length = strlen (newdir);
tlen = length + 1;
if (length > 1 && IS_DIRECTORY_SEP (newdir[length - 1]) if (length > 1 && IS_DIRECTORY_SEP (newdir[length - 1])
#ifdef WINDOWSNT #ifdef WINDOWSNT
&& !(length == 2 && IS_DIRECTORY_SEP (newdir[0])) && !(length == 2 && IS_DIRECTORY_SEP (newdir[0]))
...@@ -1189,12 +1188,15 @@ filesystem tree, not (expand-file-name ".." dirname). */) ...@@ -1189,12 +1188,15 @@ filesystem tree, not (expand-file-name ".." dirname). */)
char *temp = alloca (length); char *temp = alloca (length);
memcpy (temp, newdir, length - 1); memcpy (temp, newdir, length - 1);
temp[length - 1] = 0; temp[length - 1] = 0;
length--;
newdir = temp; newdir = temp;
} }
tlen = length + 1;
} }
else else
tlen = 0; {
length = 0;
tlen = 0;
}
/* Now concatenate the directory and name to new space in the stack frame. */ /* Now concatenate the directory and name to new space in the stack frame. */
tlen += strlen (nm) + 1; tlen += strlen (nm) + 1;
...@@ -1225,7 +1227,7 @@ filesystem tree, not (expand-file-name ".." dirname). */) ...@@ -1225,7 +1227,7 @@ filesystem tree, not (expand-file-name ".." dirname). */)
strcpy (target, newdir); strcpy (target, newdir);
} }
else else
file_name_as_directory (target, newdir); file_name_as_directory (target, newdir, length);
} }
strcat (target, nm); strcat (target, nm);
......
...@@ -300,6 +300,7 @@ typedef struct ...@@ -300,6 +300,7 @@ typedef struct
static void static void
fill_in_lock_file_name (register char *lockfile, register Lisp_Object fn) fill_in_lock_file_name (register char *lockfile, register Lisp_Object fn)
{ {
ptrdiff_t length = SBYTES (fn);
register char *p; register char *p;
struct stat st; struct stat st;
int count = 0; int count = 0;
...@@ -309,14 +310,14 @@ fill_in_lock_file_name (register char *lockfile, register Lisp_Object fn) ...@@ -309,14 +310,14 @@ fill_in_lock_file_name (register char *lockfile, register Lisp_Object fn)
/* Shift the nondirectory part of the file name (including the null) /* Shift the nondirectory part of the file name (including the null)
right two characters. Here is one of the places where we'd have to right two characters. Here is one of the places where we'd have to
do something to support 14-character-max file names. */ do something to support 14-character-max file names. */
for (p = lockfile + strlen (lockfile); p != lockfile && *p != '/'; p--) for (p = lockfile + length; p != lockfile && *p != '/'; p--)
p[2] = *p; p[2] = *p;
/* Insert the `.#'. */ /* Insert the `.#'. */
p[1] = '.'; p[1] = '.';
p[2] = '#'; p[2] = '#';
p = p + strlen (p); p = p + length + 2;
while (lstat (lockfile, &st) == 0 && !S_ISLNK (st.st_mode)) while (lstat (lockfile, &st) == 0 && !S_ISLNK (st.st_mode))
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment