[PATCH] msvcrt: gets() should handle EOF on stdin gracefully

James Woodcock james_woodcock at yahoo.co.uk
Thu Mar 1 11:08:37 CST 2018


I was asked to send the patch for Bug 44550 here.

ReactOS ftp.exe enters an endless loop when the user closes stdin by entering CTRL-Z. This patch makes gets() and wgets() return EOF if the input buffer is empty and stdin is closed.


diff --git a/dlls/msvcrt/file.c b/dlls/msvcrt/file.c
index 6d0c1ae60c..136f9e68be 100644
--- a/dlls/msvcrt/file.c
+++ b/dlls/msvcrt/file.c
@@ -4717,6 +4717,13 @@ char * CDECL MSVCRT_gets(char *buf)
cc = MSVCRT__fgetc_nolock(MSVCRT_stdin))
if(cc != '\r') *buf++ = (char)cc;

+  if ((cc == MSVCRT_EOF) && (buf_start == buf)) /* If nothing read, return 0*/
+  {
+    TRACE(":nothing read\n");
+    MSVCRT__unlock_file(MSVCRT_stdin);
+    return NULL;
+  }
+
*buf = '\0';

TRACE("got '%s'\n", buf_start);
@@ -4739,6 +4746,14 @@ MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
if (cc != '\r')
*buf++ = (MSVCRT_wchar_t)cc;
}
+
+    if ((cc == MSVCRT_WEOF) && (ws == buf)) /* If nothing read, return 0*/
+    {
+      TRACE(":nothing read\n");
+      MSVCRT__unlock_file(MSVCRT_stdin);
+      return NULL;
+    }
+
*buf = '\0';

TRACE("got %s\n", debugstr_w(ws));



More information about the wine-devel mailing list