CLIENT_WaitReply: don't clear last error on success; fixed callers
accordingly (based on a patch by Juergen Schmied).

diff --git a/files/file.c b/files/file.c
index e093777..8936215 100644
--- a/files/file.c
+++ b/files/file.c
@@ -344,6 +344,7 @@
     CLIENT_SendRequest( REQ_CREATE_FILE, -1, 2,
                         &req, sizeof(req),
                         filename, strlen(filename) + 1 );
+    SetLastError(0);
     CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
 
     /* If write access failed, retry without GENERIC_WRITE */
@@ -359,6 +360,7 @@
 	    CLIENT_SendRequest( REQ_CREATE_FILE, -1, 2,
 				&req, sizeof(req),
 				filename, strlen(filename) + 1 );
+            SetLastError(0);
 	    CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
 	}
     }
@@ -380,6 +382,7 @@
     req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
     req.id      = client_id;
     CLIENT_SendRequest( REQ_CREATE_DEVICE, -1, 1, &req, sizeof(req) );
+    SetLastError(0);
     CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
     return reply.handle;
 }
diff --git a/scheduler/client.c b/scheduler/client.c
index ae9a1c0..2a9229d 100644
--- a/scheduler/client.c
+++ b/scheduler/client.c
@@ -210,7 +210,7 @@
         remaining -= addlen;
     }
 
-    SetLastError( head.type );
+    if (head.type) SetLastError( head.type );
     return head.type;  /* error code */
 }
 
diff --git a/scheduler/event.c b/scheduler/event.c
index fbdd4b8..04f5778 100644
--- a/scheduler/event.c
+++ b/scheduler/event.c
@@ -27,6 +27,7 @@
     req.initial_state = initial_state;
     req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
     CLIENT_SendRequest( REQ_CREATE_EVENT, -1, 2, &req, sizeof(req), name, len );
+    SetLastError(0);
     CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
     if (reply.handle == -1) return 0;
     return reply.handle;
diff --git a/scheduler/mutex.c b/scheduler/mutex.c
index 180d28f..7bbe6a8 100644
--- a/scheduler/mutex.c
+++ b/scheduler/mutex.c
@@ -15,8 +15,7 @@
 /***********************************************************************
  *           CreateMutex32A   (KERNEL32.166)
  */
-HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner,
-                                LPCSTR name )
+HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
 {
     struct create_mutex_request req;
     struct create_mutex_reply reply;
@@ -25,6 +24,7 @@
     req.owned   = owner;
     req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
     CLIENT_SendRequest( REQ_CREATE_MUTEX, -1, 2, &req, sizeof(req), name, len );
+    SetLastError(0);
     CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
     if (reply.handle == -1) return 0;
     return reply.handle;
diff --git a/scheduler/semaphore.c b/scheduler/semaphore.c
index b321331..298df02 100644
--- a/scheduler/semaphore.c
+++ b/scheduler/semaphore.c
@@ -35,6 +35,7 @@
     req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
 
     CLIENT_SendRequest( REQ_CREATE_SEMAPHORE, -1, 2, &req, sizeof(req), name, len );
+    SetLastError(0);
     CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
     if (reply.handle == -1) return 0;
     return reply.handle;
diff --git a/server/file.c b/server/file.c
index 3bb6168..6d992bd 100644
--- a/server/file.c
+++ b/server/file.c
@@ -140,6 +140,7 @@
         case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
         }
 
+        /* FIXME: should set error to ERROR_ALREADY_EXISTS if file existed before */
         if ((fd = open( name, flags | O_NONBLOCK,
                         (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666 )) == -1)
         {
diff --git a/win32/console.c b/win32/console.c
index aeb6844..414a40a 100644
--- a/win32/console.c
+++ b/win32/console.c
@@ -444,6 +444,7 @@
     req.access  = access;
     req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
     CLIENT_SendRequest( REQ_OPEN_CONSOLE, -1, 1, &req, sizeof(req) );
+    SetLastError(0);
     CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
     return reply.handle;
 }