Index: trunk/src/emx/src/lib/libc.def
===================================================================
--- trunk/src/emx/src/lib/libc.def	(revision 1679)
+++ trunk/src/emx/src/lib/libc.def	(revision 1680)
@@ -1275,2 +1275,3 @@
     "DosLoadModuleEx" @1292
     "DosFreeModuleEx" @1293
+    "___libc_Back_fsRename" @1294
Index: trunk/src/emx/src/lib/sys/b_fsRename.c
===================================================================
--- trunk/src/emx/src/lib/sys/b_fsRename.c	(revision 1679)
+++ trunk/src/emx/src/lib/sys/b_fsRename.c	(revision 1680)
@@ -103,5 +103,5 @@
                 if (!strcmp(&szNativePathOld[0], &szNativePathNew[0]))
                     rc = 0;
-                else if (rc != ERROR_SHARING_VIOLATION)
+                else
                 {
                     /*
@@ -110,6 +110,6 @@
                      */
                     FILESTATUS3     fsts3Old;
-                    rc = DosQueryPathInfo((PCSZ)&szNativePathOld[0], FIL_STANDARD, &fsts3Old, sizeof(fsts3Old));
-                    if (rc)
+                    int rc2 = DosQueryPathInfo((PCSZ)&szNativePathOld[0], FIL_STANDARD, &fsts3Old, sizeof(fsts3Old));
+                    if (rc2)
                     {
                         /*
@@ -117,67 +117,74 @@
                          * Anyway, we're done.
                          */
-                        rc = -__libc_native2errno(rc);
+                        rc = -__libc_native2errno(rc2);
                     }
                     else
                     {
-                        FILESTATUS3     fsts3New;
-                        rc = DosQueryPathInfo((PCSZ)&szNativePathNew[0], FIL_STANDARD, &fsts3New, sizeof(fsts3New));
-                        if (rc)
-                        {
-                            /*
-                             * No target, source must be busy doing something.
-                             */
+                        /*
+                         * This is where we stop with ERROR_SHARING_VIOLATION.
+                         */
+                        if (rc == ERROR_SHARING_VIOLATION)
                             rc = fsts3Old.attrFile & FILE_DIRECTORY ? -EBUSY : -ETXTBSY;
-                        }
                         else
                         {
-                            if (    (fsts3New.attrFile & FILE_DIRECTORY)
-                                && !(fsts3Old.attrFile & FILE_DIRECTORY))
+                            FILESTATUS3     fsts3New;
+                            rc2 = DosQueryPathInfo((PCSZ)&szNativePathNew[0], FIL_STANDARD, &fsts3New, sizeof(fsts3New));
+                            if (rc2)
                             {
                                 /*
-                                 * The target is a directory while the source is not.
+                                 * No target, source must be busy doing something.
                                  */
-                                rc = -EISDIR;
+                                rc = fsts3Old.attrFile & FILE_DIRECTORY ? -EBUSY : -ETXTBSY;
                             }
                             else
                             {
-                                /*
-                                 * Ok, now we start cooking.
-                                 *
-                                 * This should've been done atomically, but non-atomically is
-                                 * better than not doing anything (usually). We use the loop
-                                 * to retry the move operation and handle races with other
-                                 * processes somewhat ok...
-                                 */
-                                if (fsts3New.attrFile & FILE_DIRECTORY)
+                                if (    (fsts3New.attrFile & FILE_DIRECTORY)
+                                    && !(fsts3Old.attrFile & FILE_DIRECTORY))
                                 {
-                                    rc = DosDeleteDir((PCSZ)&szNativePathNew[0]);
-                                    LIBCLOG_MSG("DosDeleteDir('%s') -> %d\n", szNativePathNew, rc);
-                                    if (!rc)
-                                        continue;
-                                    else if (rc == ERROR_ACCESS_DENIED)
-                                        rc = -ENOTEMPTY;
-                                    else
-                                        rc = -__libc_native2errno(rc);
+                                    /*
+                                     * The target is a directory while the source is not.
+                                     */
+                                    rc = -EISDIR;
                                 }
                                 else
                                 {
-                                    rc = DosDelete((PCSZ)&szNativePathNew[0]);
-                                    LIBCLOG_MSG("DosDelete('%s') -> %d\n", szNativePathNew, rc);
-                                    if (!rc)
-                                        continue;
+                                    /*
+                                     * Ok, now we start cooking.
+                                     *
+                                     * This should've been done atomically, but non-atomically is
+                                     * better than not doing anything (usually). We use the loop
+                                     * to retry the move operation and handle races with other
+                                     * processes somewhat ok...
+                                     */
+                                    if (fsts3New.attrFile & FILE_DIRECTORY)
+                                    {
+                                        rc2 = DosDeleteDir((PCSZ)&szNativePathNew[0]);
+                                        LIBCLOG_MSG("DosDeleteDir('%s') -> %d\n", szNativePathNew, rc2);
+                                        if (!rc2)
+                                            continue;
+                                        else if (rc2 == ERROR_ACCESS_DENIED)
+                                            rc = -ENOTEMPTY;
+                                        else
+                                            rc = -__libc_native2errno(rc2);
+                                    }
                                     else
-                                        rc = -__libc_native2errno(rc);
+                                    {
+                                        rc2 = DosDelete((PCSZ)&szNativePathNew[0]);
+                                        LIBCLOG_MSG("DosDelete('%s') -> %d\n", szNativePathNew, rc2);
+                                        if (!rc2)
+                                            continue;
+                                        else
+                                            rc = -__libc_native2errno(rc2);
+                                    }
                                 }
-
                             }
-                        }
+                        } /* !ERROR_SHARING_VIOLATION */
                     }
                 }
-                else if (rc == ERROR_DIRECTORY_IN_CDS || rc == ERROR_CIRCULARITY_REQUESTED)
-                    rc = -EINVAL;
-                else
-                    rc = -__libc_native2errno(rc);
             }
+            else if (rc == ERROR_DIRECTORY_IN_CDS || rc == ERROR_CIRCULARITY_REQUESTED)
+                rc = -EINVAL;
+            else
+                rc = -__libc_native2errno(rc);
             break;
         }
