=?UTF-8?Q?Fr=C3=A9d=C3=A9ric=20Delanoy=20?=: cmd: Improve handle type recognition in WCMD_ReadFile.

Alexandre Julliard julliard at winehq.org
Wed Oct 5 18:03:59 CDT 2011


Module: wine
Branch: master
Commit: 63e11558b67f25e9c8d253cf8effb7590948fba8
URL:    http://source.winehq.org/git/wine.git/?a=commit;h=63e11558b67f25e9c8d253cf8effb7590948fba8

Author: Frédéric Delanoy <frederic.delanoy at gmail.com>
Date:   Wed Oct  5 14:02:43 2011 +0200

cmd: Improve handle type recognition in WCMD_ReadFile.

---

 programs/cmd/wcmdmain.c |   36 +++++++++++++++++-------------------
 1 files changed, 17 insertions(+), 19 deletions(-)

diff --git a/programs/cmd/wcmdmain.c b/programs/cmd/wcmdmain.c
index eb57928..5611642 100644
--- a/programs/cmd/wcmdmain.c
+++ b/programs/cmd/wcmdmain.c
@@ -233,6 +233,11 @@ void WCMD_leave_paged_mode(void)
   pagedMessage = NULL;
 }
 
+static inline BOOL is_console_handle(HANDLE h)
+{
+    return (((DWORD_PTR)h) & 3) == 3;
+}
+
 /***************************************************************************
  * WCMD_Readfile
  *
@@ -240,30 +245,23 @@ void WCMD_leave_paged_mode(void)
  */
 BOOL WCMD_ReadFile(const HANDLE hIn, WCHAR *intoBuf, const DWORD maxChars, LPDWORD charsRead)
 {
-    BOOL   res;
-
-    /* Try to read from console as Unicode */
-    res = ReadConsoleW(hIn, intoBuf, maxChars, charsRead, NULL);
-
-    /* If reading from console has failed we assume its file
-       i/o so read in and convert from OEM codepage               */
-    if (!res) {
+    DWORD numRead;
+    char *buffer;
 
-        DWORD numRead;
-        char *buffer;
+    if (is_console_handle(hIn))
+        /* Try to read from console as Unicode */
+        return ReadConsoleW(hIn, intoBuf, maxChars, charsRead, NULL);
 
-        if (!(buffer = get_file_buffer()))
-            return FALSE;
+    /* We assume it's a file handle and read then convert from assumed OEM codepage */
+    if (!(buffer = get_file_buffer()))
+        return FALSE;
 
-        /* Read from file (assume OEM codepage) */
-        res = ReadFile(hIn, buffer, maxChars, &numRead, NULL);
+    if (!ReadFile(hIn, buffer, maxChars, &numRead, NULL))
+        return FALSE;
 
-        /* Convert from OEM */
-        *charsRead = MultiByteToWideChar(GetConsoleCP(), 0, buffer, numRead,
-                         intoBuf, maxChars);
+    *charsRead = MultiByteToWideChar(GetConsoleCP(), 0, buffer, numRead, intoBuf, maxChars);
 
-    }
-    return res;
+    return TRUE;
 }
 
 /*******************************************************************




More information about the wine-cvs mailing list