xref: /reactos/sdk/lib/crt/conio/getch.c (revision 1734f297)
1 /*
2  * COPYRIGHT:   See COPYING in the top level directory
3  * PROJECT:     ReactOS system libraries
4  * FILE:        lib/sdk/crt/conio/getch.c
5  * PURPOSE:     Writes a character to stdout
6  * PROGRAMER:   Ariadne
7  * UPDATE HISTORY:
8  *              28/12/98: Created
9  */
10 
11 #include <precomp.h>
12 
13 /*
14  * @implemented
15  */
16 int _getch(void)
17 {
18     DWORD NumberOfCharsRead = 0;
19     char c;
20     HANDLE ConsoleHandle;
21     BOOL RestoreMode;
22     DWORD ConsoleMode;
23 
24     if (char_avail) {
25         c = ungot_char;
26         char_avail = 0;
27     } else {
28         /*
29          * _getch() is documented to NOT echo characters. Testing shows it
30          * doesn't wait for a CR either. So we need to switch off
31          * ENABLE_ECHO_INPUT and ENABLE_LINE_INPUT if they're currently
32          * switched on.
33          */
34         ConsoleHandle = (HANDLE) _get_osfhandle(stdin->_file);
35         RestoreMode = GetConsoleMode(ConsoleHandle, &ConsoleMode) &&
36                       (0 != (ConsoleMode &
37                              (ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT)));
38         if (RestoreMode) {
39             SetConsoleMode(ConsoleHandle,
40                            ConsoleMode & (~ (ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT)));
41         }
42         ReadConsoleA((HANDLE)_get_osfhandle(stdin->_file),
43 		             &c,
44 		             1,
45 		             &NumberOfCharsRead,
46 		             NULL);
47         if (RestoreMode) {
48             SetConsoleMode(ConsoleHandle, ConsoleMode);
49         }
50     }
51     if (c == 10)
52         c = 13;
53     return c;
54 }
55 
56