- Fix PeekNamedPipe. Now only 14 winetests fail for pipetest.
- Cleanup DisconnectNamedPipe and fix a small bug in it.
Modified: trunk/reactos/lib/kernel32/file/npipe.c

Modified: trunk/reactos/lib/kernel32/file/npipe.c
--- trunk/reactos/lib/kernel32/file/npipe.c	2005-11-09 04:29:46 UTC (rev 19087)
+++ trunk/reactos/lib/kernel32/file/npipe.c	2005-11-09 04:32:44 UTC (rev 19088)
@@ -760,43 +760,42 @@
 /*
  * @implemented
  */
-BOOL STDCALL
+BOOL 
+WINAPI
 DisconnectNamedPipe(HANDLE hNamedPipe)
 {
-  IO_STATUS_BLOCK Iosb;
-  NTSTATUS Status;
+    IO_STATUS_BLOCK Iosb;
+    NTSTATUS Status;
 
-  Status = NtFsControlFile(hNamedPipe,
-			   NULL,
-			   NULL,
-			   NULL,
-			   &Iosb,
-			   FSCTL_PIPE_DISCONNECT,
-			   NULL,
-			   0,
-			   NULL,
-			   0);
-  if (Status == STATUS_PENDING)
+    /* Send the FSCTL to the driver */
+    Status = NtFsControlFile(hNamedPipe,
+                             NULL,
+                             NULL,
+                             NULL,
+                             &Iosb,
+                             FSCTL_PIPE_DISCONNECT,
+                             NULL,
+                             0,
+                             NULL,
+                             0);
+    if (Status == STATUS_PENDING)
     {
-      Status = NtWaitForSingleObject(hNamedPipe,
-				     FALSE,
-				     NULL);
-      if (!NT_SUCCESS(Status))
-	{
-	  SetLastErrorByStatus(Status);
-	  return(FALSE);
-	}
+        /* Wait on NPFS to finish and get updated status */
+        Status = NtWaitForSingleObject(hNamedPipe, FALSE, NULL);
+        if (NT_SUCCESS(Status)) Status = Iosb.Status;
     }
 
-  if (!NT_SUCCESS(Status))
+    /* Check for error */
+    if (!NT_SUCCESS(Status))
     {
-      SetLastErrorByStatus(Status);
-      return(FALSE);
-    }
-  return(TRUE);
+        /* Fail */
+        SetLastErrorByStatus(Status);
+        return FALSE;
+	}
+    
+    return TRUE;
 }
 
-
 /*
  * @unimplemented
  */
@@ -989,91 +988,92 @@
   return(TRUE);
 }
 
-
 /*
  * @implemented
  */
-BOOL STDCALL
+BOOL
+WINAPI
 PeekNamedPipe(HANDLE hNamedPipe,
-	      LPVOID lpBuffer,
-	      DWORD nBufferSize,
-	      LPDWORD lpBytesRead,
-	      LPDWORD lpTotalBytesAvail,
-	      LPDWORD lpBytesLeftThisMessage)
+              LPVOID lpBuffer,
+              DWORD nBufferSize,
+              LPDWORD lpBytesRead,
+              LPDWORD lpTotalBytesAvail,
+              LPDWORD lpBytesLeftThisMessage)
 {
-  PFILE_PIPE_PEEK_BUFFER Buffer;
-  IO_STATUS_BLOCK Iosb;
-  ULONG BufferSize;
-  NTSTATUS Status;
+    PFILE_PIPE_PEEK_BUFFER Buffer;
+    IO_STATUS_BLOCK Iosb;
+    ULONG BufferSize;
+    NTSTATUS Status;
 
-  BufferSize = nBufferSize + sizeof(FILE_PIPE_PEEK_BUFFER);
-  Buffer = RtlAllocateHeap(RtlGetProcessHeap(),
-			   0,
-			   BufferSize);
+    /* Calculate the buffer space that we'll need and allocate it */
+    BufferSize = nBufferSize + FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER, Data[0]);
+    Buffer = RtlAllocateHeap(RtlGetProcessHeap(), 0, BufferSize);
 
-  Status = NtFsControlFile(hNamedPipe,
-			   NULL,
-			   NULL,
-			   NULL,
-			   &Iosb,
-			   FSCTL_PIPE_PEEK,
-			   NULL,
-			   0,
-			   Buffer,
-			   BufferSize);
-  if (Status == STATUS_PENDING)
+    /* Tell the driver to seek */
+    Status = NtFsControlFile(hNamedPipe,
+                             NULL,
+                             NULL,
+                             NULL,
+                             &Iosb,
+                             FSCTL_PIPE_PEEK,
+                             NULL,
+                             0,
+                             Buffer,
+                             BufferSize);
+    if (Status == STATUS_PENDING)
     {
-      Status = NtWaitForSingleObject(hNamedPipe,
-				     FALSE,
-				     NULL);
-      if (NT_SUCCESS(Status))
-	Status = Iosb.Status;
+        /* Wait for npfs to be done, and update the status */
+        Status = NtWaitForSingleObject(hNamedPipe, FALSE, NULL);
+        if (NT_SUCCESS(Status)) Status = Iosb.Status;
     }
 
-  if (Status == STATUS_BUFFER_OVERFLOW)
-    {
-      Status = STATUS_SUCCESS;
-    }
+    /* Overflow is success for us */
+    if (Status == STATUS_BUFFER_OVERFLOW) Status = STATUS_SUCCESS;
 
-  if (!NT_SUCCESS(Status))
+    /* If we failed */
+    if (!NT_SUCCESS(Status))
     {
-      RtlFreeHeap(RtlGetProcessHeap(),
-		  0,
-		  Buffer);
-      SetLastErrorByStatus(Status);
-      return(FALSE);
+        /* Free the buffer and return failure */
+        RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer);
+        SetLastErrorByStatus(Status);
+        return FALSE;
     }
 
-  if (lpTotalBytesAvail != NULL)
-    {
-      *lpTotalBytesAvail = Buffer->ReadDataAvailable;
-    }
+    /* Check if caller requested bytes available */
+    if (lpTotalBytesAvail) *lpTotalBytesAvail = Buffer->ReadDataAvailable;
 
-  if (lpBytesRead != NULL)
+    /* Check if caller requested bytes read */
+    if (lpBytesRead)
     {
-      *lpBytesRead = Iosb.Information - sizeof(FILE_PIPE_PEEK_BUFFER);
+        /* Calculate the bytes returned, minus our structure overhead */
+        *lpBytesRead = (ULONG)(Iosb.Information -
+                               FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER, Data[0]));
     }
 
-  if (lpBytesLeftThisMessage != NULL)
+    /* Check if caller requested bytes left */
+    if (lpBytesLeftThisMessage)
     {
-      *lpBytesLeftThisMessage = Buffer->MessageLength -
-	(Iosb.Information - sizeof(FILE_PIPE_PEEK_BUFFER));
+        /* Calculate total minus what we returned and our structure overhead */
+        *lpBytesLeftThisMessage = Buffer->MessageLength -
+                                  (ULONG)(Iosb.Information -
+                                          FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER, Data[0]));
     }
 
-  if (lpBuffer != NULL)
+    /* Check if the caller wanted to see the actual data */
+    if (lpBuffer)
     {
-      memcpy(lpBuffer, Buffer->Data,
-	     min(nBufferSize, Iosb.Information - sizeof(FILE_PIPE_PEEK_BUFFER)));
+        /* Give him what he wants */
+        RtlCopyMemory(lpBuffer,
+                      Buffer->Data,
+	                  Iosb.Information -
+                      FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER, Data[0]));
     }
 
-  RtlFreeHeap(RtlGetProcessHeap(),
-	      0,
-	      Buffer);
-
-  return(TRUE);
+    /* Free the buffer and return success */
+    RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer);
+    return TRUE;
 }
 
-
 /*
  * @implemented
  */