From f058719458fde505054035d2bc7c22a6758058aa Mon Sep 17 00:00:00 2001 From: Chris Hanson Date: Sun, 24 Aug 1997 04:05:55 +0000 Subject: [PATCH] Fix bug: low-level file reading code was occasionally signalling errors for no apparent reason. The cause: the ReadFile API can return an error indication when it is called at end-of-file. However, in ONLY this case, the returned "bytesRead" value is zero. --- v7/src/microcode/ntapi.h | 4 +- v7/src/microcode/ntio.c | 97 ++++++++++++++++++---------------------- 2 files changed, 46 insertions(+), 55 deletions(-) diff --git a/v7/src/microcode/ntapi.h b/v7/src/microcode/ntapi.h index 3e5a4403c..27d919e72 100644 --- a/v7/src/microcode/ntapi.h +++ b/v7/src/microcode/ntapi.h @@ -1,6 +1,6 @@ /* -*-C-*- -$Id: ntapi.h,v 1.3 1997/08/23 02:52:10 cph Exp $ +$Id: ntapi.h,v 1.4 1997/08/24 04:05:55 cph Exp $ Copyright (c) 1997 Massachusetts Institute of Technology @@ -51,6 +51,7 @@ enum syscall_names apicall_GetFileTime, apicall_MoveFile, apicall_MsgWaitForMultipleObjects, + apicall_ReadFile, apicall_RemoveDirectory, apicall_SetCurrentDirectory, apicall_SetFileAttributes, @@ -814,6 +815,7 @@ static char * syscall_names_table [] = "GET-FILE-TIME", "MOVE-FILE", "MSG-WAIT-FOR-MULTIPLE-OBJECTS", + "READ-FILE", "REMOVE-DIRECTORY", "SET-CURRENT-DIRECTORY", "SET-FILE-ATTRIBUTES", diff --git a/v7/src/microcode/ntio.c b/v7/src/microcode/ntio.c index 43c2a67cb..0ca9b03eb 100644 --- a/v7/src/microcode/ntio.c +++ b/v7/src/microcode/ntio.c @@ -1,6 +1,6 @@ /* -*-C-*- -$Id: ntio.c,v 1.15 1997/06/19 05:55:43 cph Exp $ +$Id: ntio.c,v 1.16 1997/08/24 04:05:49 cph Exp $ Copyright (c) 1992-97 Massachusetts Institute of Technology @@ -213,69 +213,58 @@ Relinquish_Timeslice (void) return; } -int -DEFUN (nt_channel_read, (channel, buffer, nbytes), - Tchannel channel AND PTR buffer AND size_t nbytes) +long +DEFUN (OS_channel_read, (channel, buffer, nbytes), + Tchannel channel AND + PTR buffer AND + size_t nbytes) { - DWORD bytesRead = 0; - if (nbytes == 0) return (0); if (Screen_IsScreenHandle (CHANNEL_HANDLE (channel))) - { - bytesRead = Screen_Read ((CHANNEL_HANDLE (channel)), - ((BOOL) (CHANNEL_BUFFERED (channel))), - buffer, nbytes); - if (bytesRead == 0xffffffff) - { - /* for pleasantness give up rest of this timeslice */ - Relinquish_Timeslice (); - errno = ERRNO_NONBLOCK; - } - return ((int) bytesRead); - } - - if (IsConsoleHandle (CHANNEL_HANDLE (channel))) - { - /* fake the console being a nonblocking channel that has nothing after - each alternate read - */ - - static int nonblock = 1; - nonblock = !nonblock; - if (nonblock) { - errno = ERRNO_NONBLOCK; - return (-1); + DWORD bytes_read + = (Screen_Read ((CHANNEL_HANDLE (channel)), + ((BOOL) (CHANNEL_BUFFERED (channel))), + buffer, + nbytes)); + if (bytes_read == 0xFFFFFFFF) + { + /* For pleasantness give up rest of this timeslice. */ + Relinquish_Timeslice (); + return (-1); + } + if (bytes_read > nbytes) + error_external_return (); + return (bytes_read); } - } - if (ReadFile (CHANNEL_HANDLE (channel), buffer, nbytes, &bytesRead, 0)) - return ((int) bytesRead); - else - return (-1); -} - -long -DEFUN (OS_channel_read, (channel, buffer, nbytes), - Tchannel channel AND PTR buffer AND size_t nbytes) -{ - while (1) - { - long scr = nt_channel_read (channel, buffer, nbytes); - if (scr < 0) + while (1) { - if (errno == ERRNO_NONBLOCK) - return (-1); - NT_prim_check_errno (syscall_read); - continue; + if (IsConsoleHandle (CHANNEL_HANDLE (channel))) + { + /* Fake the console being a nonblocking channel that has + nothing after each alternate read. */ + static int nonblock = 1; + nonblock = (!nonblock); + if (nonblock) + return (-1); + } + { + DWORD bytes_read; + if ((!ReadFile ((CHANNEL_HANDLE (channel)), + buffer, + nbytes, + (&bytes_read), + 0)) + && (bytes_read > 0)) + NT_error_api_call ((GetLastError ()), apicall_ReadFile); + if (bytes_read > nbytes) + error_external_return (); + return (bytes_read); + } } - else if (((size_t) scr) > nbytes) - error_external_return (); - else - return (scr); - } } static int -- 2.25.1