2 * msvcrt.dll console functions
4 * Copyright 2000 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Note: init and free don't need MT locking since they are called at DLL
21 * (de)attachment time, which is syncronised for us
26 #include "msvcrt/conio.h"
27 #include "msvcrt/malloc.h"
28 #include "msvcrt/stdio.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
38 #define LOCK_CONSOLE _mlock(_CONIO_LOCK)
39 #define UNLOCK_CONSOLE _munlock(_CONIO_LOCK)
41 static HANDLE MSVCRT_console_in = INVALID_HANDLE_VALUE;
42 static HANDLE MSVCRT_console_out= INVALID_HANDLE_VALUE;
43 static int __MSVCRT_console_buffer = MSVCRT_EOF;
45 /* INTERNAL: Initialise console handles */
46 void msvcrt_init_console(void)
48 TRACE(":Opening console handles\n");
50 MSVCRT_console_in = GetStdHandle(STD_INPUT_HANDLE);
52 /* FIXME: Should be initialised with:
53 * CreateFileA("CONIN$", GENERIC_READ, FILE_SHARE_READ,
54 * NULL, OPEN_EXISTING, 0, NULL);
57 MSVCRT_console_out= CreateFileA("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE,
58 NULL, OPEN_EXISTING, 0, NULL);
60 if ((MSVCRT_console_in == INVALID_HANDLE_VALUE) ||
61 (MSVCRT_console_out== INVALID_HANDLE_VALUE))
62 WARN(":Console handle Initialisation FAILED!\n");
65 /* INTERNAL: Free console handles */
66 void msvcrt_free_console(void)
68 TRACE(":Closing console handles\n");
69 CloseHandle(MSVCRT_console_in);
70 CloseHandle(MSVCRT_console_out);
73 /*********************************************************************
76 int _cputs(const char* str)
79 int retval = MSVCRT_EOF;
82 if (WriteConsoleA(MSVCRT_console_out, str, strlen(str), &count, NULL)
89 /*********************************************************************
94 int retval = MSVCRT_EOF;
97 if (__MSVCRT_console_buffer != MSVCRT_EOF)
99 retval = __MSVCRT_console_buffer;
100 __MSVCRT_console_buffer = MSVCRT_EOF;
108 GetConsoleMode(MSVCRT_console_in, &mode);
110 SetConsoleMode(MSVCRT_console_in, 0);
113 if (ReadConsoleInputA(MSVCRT_console_in, &ir, 1, &count))
115 /* Only interested in ASCII chars */
116 if (ir.EventType == KEY_EVENT &&
117 ir.Event.KeyEvent.bKeyDown &&
118 ir.Event.KeyEvent.uChar.AsciiChar)
120 retval = ir.Event.KeyEvent.uChar.AsciiChar;
128 SetConsoleMode(MSVCRT_console_in, mode);
134 /*********************************************************************
139 int retval = MSVCRT_EOF;
142 if (WriteConsoleA(MSVCRT_console_out, &c, 1, &count, NULL) && count == 1)
148 /*********************************************************************
156 if (retval != MSVCRT_EOF)
157 retval = _putch(retval);
162 /*********************************************************************
165 char* _cgets(char* str)
169 str[1] = 0; /* Length */
170 /* FIXME: No editing of string supported */
174 if (str[1] >= str[0] || (str[1]++, c = _getche()) == MSVCRT_EOF || c == '\n')
183 /*********************************************************************
184 * _ungetch (MSVCRT.@)
188 int retval = MSVCRT_EOF;
190 if (c != MSVCRT_EOF && __MSVCRT_console_buffer == MSVCRT_EOF)
191 retval = __MSVCRT_console_buffer = c;
196 /*********************************************************************
204 if (__MSVCRT_console_buffer != MSVCRT_EOF)
208 /* FIXME: There has to be a faster way than this in Win32.. */
209 INPUT_RECORD *ir = NULL;
212 GetNumberOfConsoleInputEvents(MSVCRT_console_in, &count);
214 if (count && (ir = MSVCRT_malloc(count * sizeof(INPUT_RECORD))) &&
215 PeekConsoleInputA(MSVCRT_console_in, ir, count, &count))
216 for(i = 0; i < count - 1; i++)
218 if (ir[i].EventType == KEY_EVENT &&
219 ir[i].Event.KeyEvent.bKeyDown &&
220 ir[i].Event.KeyEvent.uChar.AsciiChar)
234 /*********************************************************************
235 * _cprintf (MSVCRT.@)
237 int _cprintf(const char* format, ...)
239 char buf[2048], *mem = buf;
240 int written, resize = sizeof(buf), retval;
243 va_start( valist, format );
244 /* There are two conventions for snprintf failing:
245 * Return -1 if we truncated, or
246 * Return the number of bytes that would have been written
247 * The code below handles both cases
249 while ((written = _snprintf( mem, resize, format, valist )) == -1 ||
252 resize = (written == -1 ? resize * 2 : written + 1);
255 if (!(mem = (char *)MSVCRT_malloc(resize)))
257 va_start( valist, format );
261 retval = _cputs( mem );