Cleanup some copyright stuff
[openssl.git] / crypto / ui / ui_openssl.c
1 /*
2  * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/e_os2.h>
11 #include <openssl/err.h>
12
13 /*
14  * need for #define _POSIX_C_SOURCE arises whenever you pass -ansi to gcc
15  * [maybe others?], because it masks interfaces not discussed in standard,
16  * sigaction and fileno included. -pedantic would be more appropriate for the
17  * intended purposes, but we can't prevent users from adding -ansi.
18  */
19 #if defined(OPENSSL_SYS_VXWORKS)
20 # include <sys/types.h>
21 #endif
22
23 #if !defined(_POSIX_C_SOURCE) && defined(OPENSSL_SYS_VMS)
24 # ifndef _POSIX_C_SOURCE
25 #  define _POSIX_C_SOURCE 2
26 # endif
27 #endif
28 #include <signal.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <errno.h>
32
33 #if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
34 # ifdef OPENSSL_UNISTD
35 #  include OPENSSL_UNISTD
36 # else
37 #  include <unistd.h>
38 # endif
39 /*
40  * If unistd.h defines _POSIX_VERSION, we conclude that we are on a POSIX
41  * system and have sigaction and termios.
42  */
43 # if defined(_POSIX_VERSION)
44
45 #  define SIGACTION
46 #  if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
47 #   define TERMIOS
48 #  endif
49
50 # endif
51 #endif
52
53 /* 06-Apr-92 Luke Brennan    Support for VMS */
54 #include "ui_locl.h"
55 #include "internal/cryptlib.h"
56
57 #ifdef OPENSSL_SYS_VMS          /* prototypes for sys$whatever */
58 # include <starlet.h>
59 # ifdef __DECC
60 #  pragma message disable DOLLARID
61 # endif
62 #endif
63
64 #ifdef WIN_CONSOLE_BUG
65 # include <windows.h>
66 # ifndef OPENSSL_SYS_WINCE
67 #  include <wincon.h>
68 # endif
69 #endif
70
71 /*
72  * There are 6 types of terminal interface supported, TERMIO, TERMIOS, VMS,
73  * MSDOS, WIN32 Console and SGTTY.
74  *
75  * If someone defines one of the macros TERMIO, TERMIOS or SGTTY, it will
76  * remain respected.  Otherwise, we default to TERMIOS except for a few
77  * systems that require something different.
78  *
79  * Note: we do not use SGTTY unless it's defined by the configuration.  We
80  * may eventually opt to remove it's use entirely.
81  */
82
83 #if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
84
85 # if defined(_LIBC)
86 #  undef  TERMIOS
87 #  define TERMIO
88 #  undef  SGTTY
89 /*
90  * We know that VMS, MSDOS, VXWORKS, use entirely other mechanisms.
91  */
92 # elif !defined(OPENSSL_SYS_VMS) \
93         && !defined(OPENSSL_SYS_MSDOS) \
94         && !defined(OPENSSL_SYS_VXWORKS)
95 #  define TERMIOS
96 #  undef  TERMIO
97 #  undef  SGTTY
98 # endif
99
100 #endif
101
102 #ifdef TERMIOS
103 # include <termios.h>
104 # define TTY_STRUCT             struct termios
105 # define TTY_FLAGS              c_lflag
106 # define TTY_get(tty,data)      tcgetattr(tty,data)
107 # define TTY_set(tty,data)      tcsetattr(tty,TCSANOW,data)
108 #endif
109
110 #ifdef TERMIO
111 # include <termio.h>
112 # define TTY_STRUCT             struct termio
113 # define TTY_FLAGS              c_lflag
114 # define TTY_get(tty,data)      ioctl(tty,TCGETA,data)
115 # define TTY_set(tty,data)      ioctl(tty,TCSETA,data)
116 #endif
117
118 #ifdef SGTTY
119 # include <sgtty.h>
120 # define TTY_STRUCT             struct sgttyb
121 # define TTY_FLAGS              sg_flags
122 # define TTY_get(tty,data)      ioctl(tty,TIOCGETP,data)
123 # define TTY_set(tty,data)      ioctl(tty,TIOCSETP,data)
124 #endif
125
126 #if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
127 # include <sys/ioctl.h>
128 #endif
129
130 #ifdef OPENSSL_SYS_MSDOS
131 # include <conio.h>
132 #endif
133
134 #ifdef OPENSSL_SYS_VMS
135 # include <ssdef.h>
136 # include <iodef.h>
137 # include <ttdef.h>
138 # include <descrip.h>
139 struct IOSB {
140     short iosb$w_value;
141     short iosb$w_count;
142     long iosb$l_info;
143 };
144 #endif
145
146 #ifndef NX509_SIG
147 # define NX509_SIG 32
148 #endif
149
150 /* Define globals.  They are protected by a lock */
151 #ifdef SIGACTION
152 static struct sigaction savsig[NX509_SIG];
153 #else
154 static void (*savsig[NX509_SIG]) (int);
155 #endif
156
157 #ifdef OPENSSL_SYS_VMS
158 static struct IOSB iosb;
159 static $DESCRIPTOR(terminal, "TT");
160 static long tty_orig[3], tty_new[3]; /* XXX Is there any guarantee that this
161                                       * will always suffice for the actual
162                                       * structures? */
163 static long status;
164 static unsigned short channel = 0;
165 #elif defined(_WIN32) && !defined(_WIN32_WCE)
166 static DWORD tty_orig, tty_new;
167 #else
168 # if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
169 static TTY_STRUCT tty_orig, tty_new;
170 # endif
171 #endif
172 static FILE *tty_in, *tty_out;
173 static int is_a_tty;
174
175 /* Declare static functions */
176 #if !defined(OPENSSL_SYS_WINCE)
177 static int read_till_nl(FILE *);
178 static void recsig(int);
179 static void pushsig(void);
180 static void popsig(void);
181 #endif
182 #if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32)
183 static int noecho_fgets(char *buf, int size, FILE *tty);
184 #endif
185 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl);
186
187 static int read_string(UI *ui, UI_STRING *uis);
188 static int write_string(UI *ui, UI_STRING *uis);
189
190 static int open_console(UI *ui);
191 static int echo_console(UI *ui);
192 static int noecho_console(UI *ui);
193 static int close_console(UI *ui);
194
195 static UI_METHOD ui_openssl = {
196     "OpenSSL default user interface",
197     open_console,
198     write_string,
199     NULL,                       /* No flusher is needed for command lines */
200     read_string,
201     close_console,
202     NULL
203 };
204
205 static const UI_METHOD *default_UI_meth = &ui_openssl;
206
207 void UI_set_default_method(const UI_METHOD *meth)
208 {
209     default_UI_meth = meth;
210 }
211
212 const UI_METHOD *UI_get_default_method(void)
213 {
214     return default_UI_meth;
215 }
216
217 /* The method with all the built-in thingies */
218 UI_METHOD *UI_OpenSSL(void)
219 {
220     return &ui_openssl;
221 }
222
223 /*
224  * The following function makes sure that info and error strings are printed
225  * before any prompt.
226  */
227 static int write_string(UI *ui, UI_STRING *uis)
228 {
229     switch (UI_get_string_type(uis)) {
230     case UIT_ERROR:
231     case UIT_INFO:
232         fputs(UI_get0_output_string(uis), tty_out);
233         fflush(tty_out);
234         break;
235     case UIT_NONE:
236     case UIT_PROMPT:
237     case UIT_VERIFY:
238     case UIT_BOOLEAN:
239         break;
240     }
241     return 1;
242 }
243
244 static int read_string(UI *ui, UI_STRING *uis)
245 {
246     int ok = 0;
247
248     switch (UI_get_string_type(uis)) {
249     case UIT_BOOLEAN:
250         fputs(UI_get0_output_string(uis), tty_out);
251         fputs(UI_get0_action_string(uis), tty_out);
252         fflush(tty_out);
253         return read_string_inner(ui, uis,
254                                  UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
255                                  0);
256     case UIT_PROMPT:
257         fputs(UI_get0_output_string(uis), tty_out);
258         fflush(tty_out);
259         return read_string_inner(ui, uis,
260                                  UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
261                                  1);
262     case UIT_VERIFY:
263         fprintf(tty_out, "Verifying - %s", UI_get0_output_string(uis));
264         fflush(tty_out);
265         if ((ok = read_string_inner(ui, uis,
266                                     UI_get_input_flags(uis) &
267                                     UI_INPUT_FLAG_ECHO, 1)) <= 0)
268             return ok;
269         if (strcmp(UI_get0_result_string(uis), UI_get0_test_string(uis)) != 0) {
270             fprintf(tty_out, "Verify failure\n");
271             fflush(tty_out);
272             return 0;
273         }
274         break;
275     case UIT_NONE:
276     case UIT_INFO:
277     case UIT_ERROR:
278         break;
279     }
280     return 1;
281 }
282
283 #if !defined(OPENSSL_SYS_WINCE)
284 /* Internal functions to read a string without echoing */
285 static int read_till_nl(FILE *in)
286 {
287 # define SIZE 4
288     char buf[SIZE + 1];
289
290     do {
291         if (!fgets(buf, SIZE, in))
292             return 0;
293     } while (strchr(buf, '\n') == NULL);
294     return 1;
295 }
296
297 static volatile sig_atomic_t intr_signal;
298 #endif
299
300 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
301 {
302     static int ps;
303     int ok;
304     char result[BUFSIZ];
305     int maxsize = BUFSIZ - 1;
306 #if !defined(OPENSSL_SYS_WINCE)
307     char *p = NULL;
308     int echo_eol = !echo;
309
310     intr_signal = 0;
311     ok = 0;
312     ps = 0;
313
314     pushsig();
315     ps = 1;
316
317     if (!echo && !noecho_console(ui))
318         goto error;
319     ps = 2;
320
321     result[0] = '\0';
322 # if defined(_WIN32)
323     if (is_a_tty) {
324         DWORD numread;
325 #  if defined(CP_UTF8)
326         if (GetEnvironmentVariableW(L"OPENSSL_WIN32_UTF8", NULL, 0) != 0) {
327             WCHAR wresult[BUFSIZ];
328
329             if (ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE),
330                          wresult, maxsize, &numread, NULL)) {
331                 if (numread >= 2 &&
332                     wresult[numread-2] == L'\r' &&
333                     wresult[numread-1] == L'\n') {
334                     wresult[numread-2] = L'\n';
335                     numread--;
336                 }
337                 wresult[numread] = '\0';
338                 if (WideCharToMultiByte(CP_UTF8, 0, wresult, -1,
339                                         result, sizeof(result), NULL, 0) > 0)
340                     p = result;
341
342                 OPENSSL_cleanse(wresult, sizeof(wresult));
343             }
344         } else
345 #  endif
346         if (ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE),
347                          result, maxsize, &numread, NULL)) {
348             if (numread >= 2 &&
349                 result[numread-2] == '\r' && result[numread-1] == '\n') {
350                 result[numread-2] = '\n';
351                 numread--;
352             }
353             result[numread] = '\0';
354             p = result;
355         }
356     } else
357 # elif defined(OPENSSL_SYS_MSDOS)
358     if (!echo) {
359         noecho_fgets(result, maxsize, tty_in);
360         p = result;             /* FIXME: noecho_fgets doesn't return errors */
361     } else
362 # endif
363     p = fgets(result, maxsize, tty_in);
364     if (p == NULL)
365         goto error;
366     if (feof(tty_in))
367         goto error;
368     if (ferror(tty_in))
369         goto error;
370     if ((p = (char *)strchr(result, '\n')) != NULL) {
371         if (strip_nl)
372             *p = '\0';
373     } else if (!read_till_nl(tty_in))
374         goto error;
375     if (UI_set_result(ui, uis, result) >= 0)
376         ok = 1;
377
378  error:
379     if (intr_signal == SIGINT)
380         ok = -1;
381     if (echo_eol)
382         fprintf(tty_out, "\n");
383     if (ps >= 2 && !echo && !echo_console(ui))
384         ok = 0;
385
386     if (ps >= 1)
387         popsig();
388 #else
389     ok = 1;
390 #endif
391
392     OPENSSL_cleanse(result, BUFSIZ);
393     return ok;
394 }
395
396 /* Internal functions to open, handle and close a channel to the console.  */
397 static int open_console(UI *ui)
398 {
399     CRYPTO_THREAD_write_lock(ui->lock);
400     is_a_tty = 1;
401
402 #if defined(OPENSSL_SYS_VXWORKS)
403     tty_in = stdin;
404     tty_out = stderr;
405 #elif defined(_WIN32) && !defined(_WIN32_WCE)
406     if ((tty_out = fopen("conout$", "w")) == NULL)
407         tty_out = stderr;
408
409     if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &tty_orig)) {
410         tty_in = stdin;
411     } else {
412         is_a_tty = 0;
413         if ((tty_in = fopen("conin$", "r")) == NULL)
414             tty_in = stdin;
415     }
416 #else
417 # ifdef OPENSSL_SYS_MSDOS
418 #  define DEV_TTY "con"
419 # else
420 #  define DEV_TTY "/dev/tty"
421 # endif
422     if ((tty_in = fopen(DEV_TTY, "r")) == NULL)
423         tty_in = stdin;
424     if ((tty_out = fopen(DEV_TTY, "w")) == NULL)
425         tty_out = stderr;
426 #endif
427
428 #if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
429     if (TTY_get(fileno(tty_in), &tty_orig) == -1) {
430 # ifdef ENOTTY
431         if (errno == ENOTTY)
432             is_a_tty = 0;
433         else
434 # endif
435 # ifdef EINVAL
436             /*
437              * Ariel Glenn ariel@columbia.edu reports that solaris can return
438              * EINVAL instead.  This should be ok
439              */
440         if (errno == EINVAL)
441             is_a_tty = 0;
442         else
443 # endif
444 # ifdef ENODEV
445             /*
446              * MacOS X returns ENODEV (Operation not supported by device),
447              * which seems appropriate.
448              */
449         if (errno == ENODEV)
450             is_a_tty = 0;
451         else
452 # endif
453             {
454                 char tmp_num[10];
455                 BIO_snprintf(tmp_num, sizeof(tmp_num) - 1, "%d", errno);
456                 UIerr(UI_F_OPEN_CONSOLE, UI_R_UNKNOWN_TTYGET_ERRNO_VALUE);
457                 ERR_add_error_data(2, "errno=", tmp_num);
458
459                 return 0;
460             }
461     }
462 #endif
463 #ifdef OPENSSL_SYS_VMS
464     status = sys$assign(&terminal, &channel, 0, 0);
465
466     /* if there isn't a TT device, something is very wrong */
467     if (status != SS$_NORMAL) {
468         char tmp_num[12];
469
470         BIO_snprintf(tmp_num, sizeof(tmp_num) - 1, "%%X%08X", status);
471         UIerr(UI_F_OPEN_CONSOLE, UI_R_SYSASSIGN_ERROR);
472         ERR_add_error_data(2, "status=", tmp_num);
473         return 0;
474     }
475
476     status = sys$qiow(0, channel, IO$_SENSEMODE, &iosb, 0, 0, tty_orig, 12,
477                       0, 0, 0, 0);
478
479     /* If IO$_SENSEMODE doesn't work, this is not a terminal device */
480     if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
481         is_a_tty = 0;
482 #endif
483     return 1;
484 }
485
486 static int noecho_console(UI *ui)
487 {
488 #ifdef TTY_FLAGS
489     memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
490     tty_new.TTY_FLAGS &= ~ECHO;
491 #endif
492
493 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
494     if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
495         return 0;
496 #endif
497 #ifdef OPENSSL_SYS_VMS
498     if (is_a_tty) {
499         tty_new[0] = tty_orig[0];
500         tty_new[1] = tty_orig[1] | TT$M_NOECHO;
501         tty_new[2] = tty_orig[2];
502         status = sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12,
503                           0, 0, 0, 0);
504         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) {
505             char tmp_num[2][12];
506
507             BIO_snprintf(tmp_num[0], sizeof(tmp_num[0]) - 1, "%%X%08X",
508                          status);
509             BIO_snprintf(tmp_num[1], sizeof(tmp_num[1]) - 1, "%%X%08X",
510                          iosb.iosb$w_value);
511             UIerr(UI_F_NOECHO_CONSOLE, UI_R_SYSQIOW_ERROR);
512             ERR_add_error_data(5, "status=", tmp_num[0],
513                                ",", "iosb.iosb$w_value=", tmp_num[1]);
514             return 0;
515         }
516     }
517 #endif
518 #if defined(_WIN32) && !defined(_WIN32_WCE)
519     if (is_a_tty) {
520         tty_new = tty_orig;
521         tty_new &= ~ENABLE_ECHO_INPUT;
522         SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), tty_new);
523     }
524 #endif
525     return 1;
526 }
527
528 static int echo_console(UI *ui)
529 {
530 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
531     memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
532     tty_new.TTY_FLAGS |= ECHO;
533 #endif
534
535 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
536     if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
537         return 0;
538 #endif
539 #ifdef OPENSSL_SYS_VMS
540     if (is_a_tty) {
541         tty_new[0] = tty_orig[0];
542         tty_new[1] = tty_orig[1] & ~TT$M_NOECHO;
543         tty_new[2] = tty_orig[2];
544         status = sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12,
545                           0, 0, 0, 0);
546         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) {
547             char tmp_num[2][12];
548
549             BIO_snprintf(tmp_num[0], sizeof(tmp_num[0]) - 1, "%%X%08X",
550                          status);
551             BIO_snprintf(tmp_num[1], sizeof(tmp_num[1]) - 1, "%%X%08X",
552                          iosb.iosb$w_value);
553             UIerr(UI_F_ECHO_CONSOLE, UI_R_SYSQIOW_ERROR);
554             ERR_add_error_data(5, "status=", tmp_num[0],
555                                ",", "iosb.iosb$w_value=", tmp_num[1]);
556             return 0;
557         }
558     }
559 #endif
560 #if defined(_WIN32) && !defined(_WIN32_WCE)
561     if (is_a_tty) {
562         tty_new = tty_orig;
563         tty_new |= ENABLE_ECHO_INPUT;
564         SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), tty_new);
565     }
566 #endif
567     return 1;
568 }
569
570 static int close_console(UI *ui)
571 {
572     if (tty_in != stdin)
573         fclose(tty_in);
574     if (tty_out != stderr)
575         fclose(tty_out);
576 #ifdef OPENSSL_SYS_VMS
577     status = sys$dassgn(channel);
578     if (status != SS$_NORMAL) {
579         char tmp_num[12];
580
581         BIO_snprintf(tmp_num, sizeof(tmp_num) - 1, "%%X%08X", status);
582         UIerr(UI_F_CLOSE_CONSOLE, UI_R_SYSDASSGN_ERROR);
583         ERR_add_error_data(2, "status=", tmp_num);
584         return 0;
585     }
586 #endif
587     CRYPTO_THREAD_unlock(ui->lock);
588
589     return 1;
590 }
591
592 #if !defined(OPENSSL_SYS_WINCE)
593 /* Internal functions to handle signals and act on them */
594 static void pushsig(void)
595 {
596 # ifndef OPENSSL_SYS_WIN32
597     int i;
598 # endif
599 # ifdef SIGACTION
600     struct sigaction sa;
601
602     memset(&sa, 0, sizeof(sa));
603     sa.sa_handler = recsig;
604 # endif
605
606 # ifdef OPENSSL_SYS_WIN32
607     savsig[SIGABRT] = signal(SIGABRT, recsig);
608     savsig[SIGFPE] = signal(SIGFPE, recsig);
609     savsig[SIGILL] = signal(SIGILL, recsig);
610     savsig[SIGINT] = signal(SIGINT, recsig);
611     savsig[SIGSEGV] = signal(SIGSEGV, recsig);
612     savsig[SIGTERM] = signal(SIGTERM, recsig);
613 # else
614     for (i = 1; i < NX509_SIG; i++) {
615 #  ifdef SIGUSR1
616         if (i == SIGUSR1)
617             continue;
618 #  endif
619 #  ifdef SIGUSR2
620         if (i == SIGUSR2)
621             continue;
622 #  endif
623 #  ifdef SIGKILL
624         if (i == SIGKILL)       /* We can't make any action on that. */
625             continue;
626 #  endif
627 #  ifdef SIGACTION
628         sigaction(i, &sa, &savsig[i]);
629 #  else
630         savsig[i] = signal(i, recsig);
631 #  endif
632     }
633 # endif
634
635 # ifdef SIGWINCH
636     signal(SIGWINCH, SIG_DFL);
637 # endif
638 }
639
640 static void popsig(void)
641 {
642 # ifdef OPENSSL_SYS_WIN32
643     signal(SIGABRT, savsig[SIGABRT]);
644     signal(SIGFPE, savsig[SIGFPE]);
645     signal(SIGILL, savsig[SIGILL]);
646     signal(SIGINT, savsig[SIGINT]);
647     signal(SIGSEGV, savsig[SIGSEGV]);
648     signal(SIGTERM, savsig[SIGTERM]);
649 # else
650     int i;
651     for (i = 1; i < NX509_SIG; i++) {
652 #  ifdef SIGUSR1
653         if (i == SIGUSR1)
654             continue;
655 #  endif
656 #  ifdef SIGUSR2
657         if (i == SIGUSR2)
658             continue;
659 #  endif
660 #  ifdef SIGACTION
661         sigaction(i, &savsig[i], NULL);
662 #  else
663         signal(i, savsig[i]);
664 #  endif
665     }
666 # endif
667 }
668
669 static void recsig(int i)
670 {
671     intr_signal = i;
672 }
673 #endif
674
675 /* Internal functions specific for Windows */
676 #if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32)
677 static int noecho_fgets(char *buf, int size, FILE *tty)
678 {
679     int i;
680     char *p;
681
682     p = buf;
683     for (;;) {
684         if (size == 0) {
685             *p = '\0';
686             break;
687         }
688         size--;
689 # if defined(_WIN32)
690         i = _getch();
691 # else
692         i = getch();
693 # endif
694         if (i == '\r')
695             i = '\n';
696         *(p++) = i;
697         if (i == '\n') {
698             *p = '\0';
699             break;
700         }
701     }
702 # ifdef WIN_CONSOLE_BUG
703     /*
704      * Win95 has several evil console bugs: one of these is that the last
705      * character read using getch() is passed to the next read: this is
706      * usually a CR so this can be trouble. No STDIO fix seems to work but
707      * flushing the console appears to do the trick.
708      */
709     {
710         HANDLE inh;
711         inh = GetStdHandle(STD_INPUT_HANDLE);
712         FlushConsoleInputBuffer(inh);
713     }
714 # endif
715     return (strlen(buf));
716 }
717 #endif