2006-10-15  James Youngman <jay@gnu.org>

	* lib/buildcmd.c, 
	  find/testsuite/find.gnu/sv-bug-17782.exp, 
	  find/testsuite/find.gnu/sv-bug-17782.xo:
	Fixed Savannah bug #17782, in which "find -execdir echo "foo {}"
	runs 'echo ./foo filename' instead of the correct command 
	'echo foo ./filename'.
	

Index: lib/buildcmd.c
===================================================================
RCS file: /cvsroot/findutils/findutils/lib/buildcmd.c,v
retrieving revision 1.9.4.5
diff -u -r1.9.4.5 buildcmd.c
--- lib/buildcmd.c	5 Aug 2006 18:03:21 -0000	1.9.4.5
+++ lib/buildcmd.c	16 Oct 2006 09:44:48 -0000
@@ -134,14 +134,12 @@
     insertbuf = (char *) xmalloc (ctl->arg_max + 1);
   p = insertbuf;
 
-  need_prefix = 0;
   do
     {
       size_t len;               /* Length in ARG before `replace_pat'.  */
       char *s = mbstrstr (arg, ctl->replace_pat);
       if (s)
         {
-          need_prefix = 1;
           len = s - arg;
         }
       else
@@ -161,31 +159,31 @@
 
       if (s)
         {
-	  if (bytes_left <= lblen)
+	  if (bytes_left <= (lblen + pfxlen))
 	    break;
 	  else
-	    bytes_left -= lblen;
-
+	    bytes_left -= (lblen + pfxlen);
+	  
+	  if (prefix)
+	    {
+	      strcpy (p, prefix);
+	      p += pfxlen;
+	    }
           strcpy (p, linebuf);
+          p += lblen;
+
           arg += ctl->rplen;
           arglen -= ctl->rplen;
-          p += lblen;
         }
     }
   while (*arg);
   if (*arg)
     error (1, 0, _("command too long"));
   *p++ = '\0';
-
-  if (!need_prefix)
-    {
-      prefix = NULL;
-      pfxlen = 0;
-    }
   
   bc_push_arg (ctl, state,
                insertbuf, p - insertbuf,
-               prefix, pfxlen,
+               NULL, 0,
                initial_args);
 }
 
