From fe6dfb052285ccc1fae3d17bab5d44f57e73020e Mon Sep 17 00:00:00 2001
From: Andreas Mohr <100.30936@germany.net>
Date: Sun, 11 Oct 1998 17:36:06 +0000
Subject: [PATCH] Fixed severe bug: SetCurrentDirectory32A didn't set
 pTask->curdir correctly due to current drive being set too late.

---
 files/drive.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/files/drive.c b/files/drive.c
index 9f3e0e8161..8afe419999 100644
--- a/files/drive.c
+++ b/files/drive.c
@@ -894,7 +894,7 @@ BOOL16 WINAPI SetCurrentDirectory16( LPCSTR dir )
  */
 BOOL32 WINAPI SetCurrentDirectory32A( LPCSTR dir )
 {
-    int drive = DRIVE_GetCurrentDrive();
+    int olddrive, drive = DRIVE_GetCurrentDrive();
 
     if (!dir) {
     	ERR(file,"(NULL)!\n");
@@ -903,17 +903,20 @@ BOOL32 WINAPI SetCurrentDirectory32A( LPCSTR dir )
     if (dir[0] && (dir[1]==':'))
     {
         drive = tolower( *dir ) - 'a';
-        if (!DRIVE_IsValid( drive ))
-        {
-            DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
-            return FALSE;
-        }
         dir += 2;
     }
+
+    /* WARNING: we need to set the drive before the dir, as DRIVE_Chdir
+       sets pTask->curdir only if pTask->curdrive is drive */
+    olddrive = drive; /* in case DRIVE_Chdir fails */
+    if (!(DRIVE_SetCurrentDrive( drive )))
+	return FALSE;
     /* FIXME: what about empty strings? Add a \\ ? */
-    if (!DRIVE_Chdir( drive, dir )) return FALSE;
-    if (drive == DRIVE_GetCurrentDrive()) return TRUE;
-    return DRIVE_SetCurrentDrive( drive );
+    if (!DRIVE_Chdir( drive, dir )) {
+	DRIVE_SetCurrentDrive(olddrive);
+	return FALSE;
+    }
+    return TRUE;
 }