Changed DOS extended error handling to be based on SetLastError;
should be more thread-safe this way.

diff --git a/files/directory.c b/files/directory.c
index 8255579..3d81f69 100644
--- a/files/directory.c
+++ b/files/directory.c
@@ -294,7 +294,7 @@
     if (DOSFS_GetDevice( path ))
     {
         TRACE(file, "cannot use device '%s'!\n",path);
-        DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
+        SetLastError( ERROR_ACCESS_DENIED );
         return FALSE;
     }
     if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
@@ -362,7 +362,7 @@
     if (DOSFS_GetDevice( path ))
     {
         TRACE(file, "cannot remove device '%s'!\n", path);
-        DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
+        SetLastError( ERROR_FILE_NOT_FOUND );
         return FALSE;
     }
     if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
@@ -401,7 +401,7 @@
     if ((p_s >= full_name->short_name + sizeof(full_name->short_name) - 14) ||
         (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
     {
-        DOS_ERROR( ER_PathNotFound, EC_NotFound, SA_Abort, EL_Disk );
+        SetLastError( ERROR_PATH_NOT_FOUND );
         return FALSE;
     }
     if (!DOSFS_FindUnixName( dir->long_name, name, p_l,
diff --git a/files/dos_fs.c b/files/dos_fs.c
index 47d86e2..22c0ed2 100644
--- a/files/dos_fs.c
+++ b/files/dos_fs.c
@@ -72,12 +72,6 @@
 #define GET_DRIVE(path) \
     (((path)[1] == ':') ? toupper((path)[0]) - 'A' : DOSFS_CurDrive)
 
-    /* DOS extended error status */
-WORD DOS_ExtendedError;
-BYTE DOS_ErrorClass;
-BYTE DOS_ErrorAction;
-BYTE DOS_ErrorLocus;
-
 /* Directory info for DOSFS_ReadDir */
 typedef struct
 {
@@ -319,7 +313,7 @@
     DOS_DIR *dir = HeapAlloc( SystemHeap, 0, sizeof(*dir) );
     if (!dir)
     {
-        DOS_ERROR( ER_OutOfMemory, EC_OutOfResource, SA_Abort, EL_Memory );
+        SetLastError( ERROR_NOT_ENOUGH_MEMORY );
         return NULL;
     }
 
@@ -716,7 +710,7 @@
 
     if (!DRIVE_IsValid(drive))
     {
-        DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
+        SetLastError( ERROR_INVALID_DRIVE );
         return -1;
     }
     return drive;
@@ -800,7 +794,7 @@
         if ((p_s >= full->short_name + sizeof(full->short_name) - 14) ||
             (p_l >= full->long_name + sizeof(full->long_name) - 1))
         {
-            DOS_ERROR( ER_PathNotFound, EC_NotFound, SA_Abort, EL_Disk );
+            SetLastError( ERROR_PATH_NOT_FOUND );
             return FALSE;
         }
 
@@ -841,12 +835,12 @@
     {
         if (check_last)
         {
-            DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
+            SetLastError( ERROR_FILE_NOT_FOUND );
             return FALSE;
         }
         if (*name)  /* Not last */
         {
-            DOS_ERROR( ER_PathNotFound, EC_NotFound, SA_Abort, EL_Disk );
+            SetLastError( ERROR_PATH_NOT_FOUND );
             return FALSE;
         }
     }
@@ -1052,7 +1046,7 @@
             }
         }
         if ( *endchar )
-        {   DOS_ERROR( ER_PathNotFound, EC_NotFound, SA_Abort, EL_Disk );
+        {   SetLastError( ERROR_PATH_NOT_FOUND );
             return 0;
         }
         while (!IS_END_OF_NAME(*name) && (!*endchar) )
@@ -1330,7 +1324,7 @@
     if (!FindNextFile16( handle, data ))
     {
         FindClose16( handle );
-        DOS_ERROR( ER_NoMoreFiles, EC_MediaError, SA_Abort, EL_Disk );
+        SetLastError( ERROR_NO_MORE_FILES );
         return INVALID_HANDLE_VALUE16;
     }
     return handle;
@@ -1381,13 +1375,13 @@
 
     if (!(info = (FIND_FIRST_INFO *)GlobalLock16( handle )))
     {
-        DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
+        SetLastError( ERROR_INVALID_HANDLE );
         return FALSE;
     }
     GlobalUnlock16( handle );
     if (!info->path || !info->dir)
     {
-        DOS_ERROR( ER_NoMoreFiles, EC_MediaError, SA_Abort, EL_Disk );
+        SetLastError( ERROR_NO_MORE_FILES );
         return FALSE;
     }
     if (!DOSFS_FindNextEx( info, data ))
@@ -1395,7 +1389,7 @@
         DOSFS_CloseDir( info->dir ); info->dir = NULL;
         HeapFree( SystemHeap, 0, info->path );
         info->path = info->long_mask = NULL;
-        DOS_ERROR( ER_NoMoreFiles, EC_MediaError, SA_Abort, EL_Disk );
+        SetLastError( ERROR_NO_MORE_FILES );
         return FALSE;
     }
     return TRUE;
@@ -1440,7 +1434,7 @@
     if ((handle == INVALID_HANDLE_VALUE16) ||
         !(info = (FIND_FIRST_INFO *)GlobalLock16( handle )))
     {
-        DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
+        SetLastError( ERROR_INVALID_HANDLE );
         return FALSE;
     }
     if (info->dir) DOSFS_CloseDir( info->dir );
diff --git a/files/drive.c b/files/drive.c
index 2c69d0b..0c63a9c 100644
--- a/files/drive.c
+++ b/files/drive.c
@@ -34,6 +34,7 @@
 
 #include "windows.h"
 #include "winbase.h"
+#include "winerror.h"
 #include "drive.h"
 #include "file.h"
 #include "heap.h"
@@ -267,7 +268,7 @@
     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
     if (!DRIVE_IsValid( drive ))
     {
-        DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
+        SetLastError( ERROR_INVALID_DRIVE );
         return 0;
     }
     TRACE(dosfs, "%c:\n", 'A' + drive );
@@ -460,7 +461,7 @@
     if (!FILE_Stat( full_name.long_name, &info )) return 0;
     if (!(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
     {
-        DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
+        SetLastError( ERROR_FILE_NOT_FOUND );
         return 0;
     }
     unix_cwd = full_name.long_name + strlen( DOSDrives[drive].root );
@@ -493,7 +494,7 @@
 {
     if ((drive < 0) || (drive >= MAX_DOS_DRIVES) || !DOSDrives[drive].root)
     {
-        DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
+        SetLastError( ERROR_INVALID_DRIVE );
         return 0;
     }
     DOSDrives[drive].flags |= DRIVE_DISABLED;
@@ -508,7 +509,7 @@
 {
     if ((drive < 0) || (drive >= MAX_DOS_DRIVES) || !DOSDrives[drive].root)
     {
-        DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
+        SetLastError( ERROR_INVALID_DRIVE );
         return 0;
     }
     DOSDrives[drive].flags &= ~DRIVE_DISABLED;
@@ -533,7 +534,7 @@
         !old->root ||
 	(new_drive < 0) || (new_drive >= MAX_DOS_DRIVES))
     {
-        DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
+        SetLastError( ERROR_INVALID_DRIVE );
         return 0;
     }
 
@@ -644,7 +645,7 @@
 
     if (!DRIVE_IsValid(drive))
     {
-        DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
+        SetLastError( ERROR_INVALID_DRIVE );
         return 0;
     }
 
diff --git a/files/file.c b/files/file.c
index 9bef1e5..72b3df4 100644
--- a/files/file.c
+++ b/files/file.c
@@ -226,7 +226,7 @@
 fail_int24:
   FIXME(file,"generate INT24 missing\n");
   /* Is this the right error? */
-  DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
+  SetLastError( ERROR_ACCESS_DENIED );
   return TRUE;
   
 test_ro_err05:
@@ -235,7 +235,7 @@
   /* fall through */
 fail_error05:
   TRACE(file,"Access Denied, oldmode 0x%02x mode 0x%02x\n",oldmode,mode);
-  DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
+  SetLastError( ERROR_ACCESS_DENIED );
   return TRUE;
 }
 #endif
@@ -254,45 +254,45 @@
     switch (save_errno)
     {
     case EAGAIN:
-        DOS_ERROR( ER_ShareViolation, EC_Temporary, SA_Retry, EL_Disk );
+        SetLastError( ERROR_SHARING_VIOLATION );
         break;
     case EBADF:
-        DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
+        SetLastError( ERROR_INVALID_HANDLE );
         break;
     case ENOSPC:
-        DOS_ERROR( ER_DiskFull, EC_MediaError, SA_Abort, EL_Disk );
+        SetLastError( ERROR_HANDLE_DISK_FULL );
         break;
     case EACCES:
     case EPERM:
     case EROFS:
-        DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
+        SetLastError( ERROR_ACCESS_DENIED );
         break;
     case EBUSY:
-        DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Abort, EL_Disk );
+        SetLastError( ERROR_LOCK_VIOLATION );
         break;
     case ENOENT:
-        DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
+        SetLastError( ERROR_FILE_NOT_FOUND );
         break;
     case EISDIR:
-        DOS_ERROR( ER_CanNotMakeDir, EC_AccessDenied, SA_Abort, EL_Unknown );
+        SetLastError( ERROR_CANNOT_MAKE );
         break;
     case ENFILE:
     case EMFILE:
-        DOS_ERROR( ER_NoMoreFiles, EC_MediaError, SA_Abort, EL_Unknown );
+        SetLastError( ERROR_NO_MORE_FILES );
         break;
     case EEXIST:
-        DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk );
+        SetLastError( ERROR_FILE_EXISTS );
         break;
     case EINVAL:
     case ESPIPE:
-        DOS_ERROR( ER_SeekError, EC_NotFound, SA_Ignore, EL_Disk );
+        SetLastError( ERROR_SEEK );
         break;
     case ENOTEMPTY:
-        DOS_ERROR( ERROR_DIR_NOT_EMPTY, EC_Exists, SA_Ignore, EL_Disk );
+        SetLastError( ERROR_DIR_NOT_EMPTY );
         break;
     default:
         perror( "int21: unknown errno" );
-        DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort, EL_Unknown );
+        SetLastError( ERROR_GEN_FAILURE );
         break;
     }
     errno = save_errno;
@@ -329,8 +329,8 @@
 
     if (!(file = HeapAlloc( SystemHeap, 0, sizeof(FILE_OBJECT) )))
     {
-        DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError, SA_Abort, EL_Disk );
         CLIENT_CloseHandle( reply.handle );
+        SetLastError( ERROR_TOO_MANY_OPEN_FILES );
         return (HFILE32)NULL;
     }
     file->header.type = K32OBJ_FILE;
@@ -507,7 +507,7 @@
 
 	/* Do not silence this please. It is a critical error. -MM */
         ERR(file, "Couldn't open device '%s'!\n",filename);
-        DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
+        SetLastError( ERROR_FILE_NOT_FOUND );
         return HFILE_ERROR32;
     }
 
@@ -766,7 +766,7 @@
                 CloseHandle( handle );
                 break;
             }
-            if (DOS_ExtendedError != ER_FileExists)
+            if (GetLastError() != ERROR_FILE_EXISTS)
                 break;  /* No need to go on */
             num++;
             sprintf( p, "%04x.tmp", num );
@@ -932,7 +932,7 @@
             CloseHandle( hFileRet );
             WARN(file, "(%s): OF_VERIFY failed\n", name );
             /* FIXME: what error here? */
-            DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
+            SetLastError( ERROR_FILE_NOT_FOUND );
             goto error;
         }
     }
@@ -940,17 +940,27 @@
 
 success:  /* We get here if the open was successful */
     TRACE(file, "(%s): OK, return = %d\n", name, hFileRet );
-    if (mode & OF_EXIST) /* Return the handle, but close it first */
-        CloseHandle( hFileRet );
+    if (win32)
+    {
+        if (mode & OF_EXIST) /* Return the handle, but close it first */
+            CloseHandle( hFileRet );
+    }
+    else
+    {
+        hFileRet = FILE_AllocDosHandle( hFileRet );
+        if (hFileRet == HFILE_ERROR16) goto error;
+        if (mode & OF_EXIST) /* Return the handle, but close it first */
+            _lclose16( hFileRet );
+    }
     return hFileRet;
 
 not_found:  /* We get here if the file does not exist */
     WARN(file, "'%s' not found\n", name );
-    DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
+    SetLastError( ERROR_FILE_NOT_FOUND );
     /* fall through */
 
 error:  /* We get here if there was an error opening the file */
-    ofs->nErrCode = DOS_ExtendedError;
+    ofs->nErrCode = GetLastError();
     WARN(file, "(%s): return = HFILE_ERROR error= %d\n", 
 		  name,ofs->nErrCode );
     return HFILE_ERROR32;
@@ -962,8 +972,7 @@
  */
 HFILE16 WINAPI OpenFile16( LPCSTR name, OFSTRUCT *ofs, UINT16 mode )
 {
-    TRACE(file,"OpenFile16(%s,%i)\n", name, mode);
-    return FILE_AllocDosHandle( FILE_DoOpenFile( name, ofs, mode, FALSE ) );
+    return FILE_DoOpenFile( name, ofs, mode, FALSE );
 }
 
 
@@ -1025,8 +1034,8 @@
             return i;
         }
 error:
-    DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError, SA_Abort, EL_Disk );
     CloseHandle( handle );
+    SetLastError( ERROR_TOO_MANY_OPEN_FILES );
     return INVALID_HANDLE_VALUE16;
 }
 
@@ -1041,7 +1050,7 @@
     HANDLE32 *table = PROCESS_Current()->dos_handles;
     if ((hfile >= DOS_TABLE_SIZE) || !table || !table[hfile])
     {
-        DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
+        SetLastError( ERROR_INVALID_HANDLE );
         return INVALID_HANDLE_VALUE32;
     }
     return table[hfile];
@@ -1061,13 +1070,13 @@
     if ((hFile1 >= DOS_TABLE_SIZE) || (hFile2 >= DOS_TABLE_SIZE) ||
         !table || !table[hFile1])
     {
-        DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
+        SetLastError( ERROR_INVALID_HANDLE );
         return HFILE_ERROR16;
     }
     if (hFile2 < 5)
     {
         FIXME( file, "stdio handle closed, need proper conversion\n" );
-        DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
+        SetLastError( ERROR_INVALID_HANDLE );
         return HFILE_ERROR16;
     }
     if (!DuplicateHandle( GetCurrentProcess(), table[hFile1],
@@ -1090,12 +1099,12 @@
     if (hFile < 5)
     {
         FIXME( file, "stdio handle closed, need proper conversion\n" );
-        DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
+        SetLastError( ERROR_INVALID_HANDLE );
         return HFILE_ERROR16;
     }
     if ((hFile >= DOS_TABLE_SIZE) || !table || !table[hFile])
     {
-        DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
+        SetLastError( ERROR_INVALID_HANDLE );
         return HFILE_ERROR16;
     }
     TRACE( file, "%d (handle32=%d)\n", hFile, table[hFile] );
@@ -1443,7 +1452,7 @@
         HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
         if (!newhandle)
         {
-            DOS_ERROR( ER_OutOfMemory, EC_OutOfResource, SA_Abort, EL_Memory );
+            SetLastError( ERROR_NOT_ENOUGH_MEMORY );
             return pdb->nbFiles;
         }
         newfiles = (BYTE *)GlobalLock16( newhandle );
@@ -1528,7 +1537,7 @@
     if (DOSFS_GetDevice( path ))
     {
         WARN(file, "cannot remove DOS device '%s'!\n", path);
-        DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
+        SetLastError( ERROR_FILE_NOT_FOUND );
         return FALSE;
     }
 
@@ -1682,7 +1691,7 @@
 	/* use copy, if allowed */
 	if (!(flag & MOVEFILE_COPY_ALLOWED)) {
 	  /* FIXME: Use right error code */
-	  DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk );
+	  SetLastError( ERROR_FILE_EXISTS );
 	  return FALSE;
 	}
 	else mode =1;
@@ -1691,7 +1700,7 @@
 	/* target exists, check if we may overwrite */
 	if (!(flag & MOVEFILE_REPLACE_EXISTING)) {
 	  /* FIXME: Use right error code */
-	  DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
+	  SetLastError( ERROR_ACCESS_DENIED );
 	  return FALSE;
 	}
     }
@@ -1699,8 +1708,7 @@
       if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
 	if (flag & MOVEFILE_COPY_ALLOWED) {  
 	  WARN(file, "Illegal flag\n");
-	  DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort,
-		     EL_Unknown );
+	  SetLastError( ERROR_GEN_FAILURE );
 	  return FALSE;
 	}
 	/* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
@@ -1791,8 +1799,7 @@
       if (S_ISDIR(fstat.st_mode)) {
 	/* No Move for directories across file systems */
 	/* FIXME: Use right error code */
-	DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort,
-		   EL_Unknown );
+	SetLastError( ERROR_GEN_FAILURE );
 	return FALSE;
       }
       else
@@ -2126,7 +2133,7 @@
 
   /* shadow locks internally */
   if (!DOS_AddLock(file, &f)) {
-    DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Ignore, EL_Disk );
+    SetLastError( ERROR_LOCK_VIOLATION );
     return FALSE;
   }
 
@@ -2134,7 +2141,7 @@
 #ifdef USE_UNIX_LOCKS
   if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
     if (errno == EACCES || errno == EAGAIN) {
-      DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Ignore, EL_Disk );
+      SetLastError( ERROR_LOCK_VIOLATION );
     }
     else {
       FILE_SetDosError();