Remove Netware and OS/2
[openssl.git] / crypto / ui / ui_openssl.c
1 /*
2  * Written by Richard Levitte (richard@levitte.org) and others for the
3  * OpenSSL project 2001.
4  */
5 /* ====================================================================
6  * Copyright (c) 2001 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    openssl-core@openssl.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 /*-
60  * The lowest level part of this file was previously in crypto/des/read_pwd.c,
61  * Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
62  * All rights reserved.
63  *
64  * This package is an SSL implementation written
65  * by Eric Young (eay@cryptsoft.com).
66  * The implementation was written so as to conform with Netscapes SSL.
67  *
68  * This library is free for commercial and non-commercial use as long as
69  * the following conditions are aheared to.  The following conditions
70  * apply to all code found in this distribution, be it the RC4, RSA,
71  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
72  * included with this distribution is covered by the same copyright terms
73  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
74  *
75  * Copyright remains Eric Young's, and as such any Copyright notices in
76  * the code are not to be removed.
77  * If this package is used in a product, Eric Young should be given attribution
78  * as the author of the parts of the library used.
79  * This can be in the form of a textual message at program startup or
80  * in documentation (online or textual) provided with the package.
81  *
82  * Redistribution and use in source and binary forms, with or without
83  * modification, are permitted provided that the following conditions
84  * are met:
85  * 1. Redistributions of source code must retain the copyright
86  *    notice, this list of conditions and the following disclaimer.
87  * 2. Redistributions in binary form must reproduce the above copyright
88  *    notice, this list of conditions and the following disclaimer in the
89  *    documentation and/or other materials provided with the distribution.
90  * 3. All advertising materials mentioning features or use of this software
91  *    must display the following acknowledgement:
92  *    "This product includes cryptographic software written by
93  *     Eric Young (eay@cryptsoft.com)"
94  *    The word 'cryptographic' can be left out if the rouines from the library
95  *    being used are not cryptographic related :-).
96  * 4. If you include any Windows specific code (or a derivative thereof) from
97  *    the apps directory (application code) you must include an acknowledgement:
98  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
99  *
100  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
101  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
102  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
103  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
104  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
105  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
106  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
107  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
108  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
109  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
110  * SUCH DAMAGE.
111  *
112  * The licence and distribution terms for any publically available version or
113  * derivative of this code cannot be changed.  i.e. this code cannot simply be
114  * copied and put under another distribution licence
115  * [including the GNU Public Licence.]
116  */
117
118 #include <openssl/e_os2.h>
119
120 /*
121  * need for #define _POSIX_C_SOURCE arises whenever you pass -ansi to gcc
122  * [maybe others?], because it masks interfaces not discussed in standard,
123  * sigaction and fileno included. -pedantic would be more appropriate for the
124  * intended purposes, but we can't prevent users from adding -ansi.
125  */
126 #if defined(OPENSSL_SYS_VXWORKS)
127 # include <sys/types.h>
128 #endif
129
130 #if !defined(_POSIX_C_SOURCE) && defined(OPENSSL_SYS_VMS)
131 # ifndef _POSIX_C_SOURCE
132 #  define _POSIX_C_SOURCE 2
133 # endif
134 #endif
135 #include <signal.h>
136 #include <stdio.h>
137 #include <string.h>
138 #include <errno.h>
139
140 #if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
141 # ifdef OPENSSL_UNISTD
142 #  include OPENSSL_UNISTD
143 # else
144 #  include <unistd.h>
145 # endif
146 /*
147  * If unistd.h defines _POSIX_VERSION, we conclude that we are on a POSIX
148  * system and have sigaction and termios.
149  */
150 # if defined(_POSIX_VERSION)
151
152 #  define SIGACTION
153 #  if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
154 #   define TERMIOS
155 #  endif
156
157 # endif
158 #endif
159
160 /* 06-Apr-92 Luke Brennan    Support for VMS */
161 #include "ui_locl.h"
162 #include "internal/cryptlib.h"
163
164 #ifdef OPENSSL_SYS_VMS          /* prototypes for sys$whatever */
165 # include <starlet.h>
166 # ifdef __DECC
167 #  pragma message disable DOLLARID
168 # endif
169 #endif
170
171 #ifdef WIN_CONSOLE_BUG
172 # include <windows.h>
173 # ifndef OPENSSL_SYS_WINCE
174 #  include <wincon.h>
175 # endif
176 #endif
177
178 /*
179  * There are 5 types of terminal interface supported, TERMIO, TERMIOS, VMS,
180  * MSDOS and SGTTY.
181  *
182  * If someone defines one of the macros TERMIO, TERMIOS or SGTTY, it will
183  * remain respected.  Otherwise, we default to TERMIOS except for a few
184  * systems that require something different.
185  *
186  * Note: we do not use SGTTY unless it's defined by the configuration.  We
187  * may eventually opt to remove it's use entirely.
188  */
189
190 #if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
191
192 # if defined(_LIBC)
193 #  undef  TERMIOS
194 #  define TERMIO
195 #  undef  SGTTY
196 /*
197  * We know that VMS, MSDOS, VXWORKS, use entirely other mechanisms.
198  * MAC_OS_GUSI_SOURCE should probably go away, but that needs to be confirmed.
199  */
200 # elif !defined(OPENSSL_SYS_VMS) \
201         && !defined(OPENSSL_SYS_MSDOS) \
202         && !defined(MAC_OS_GUSI_SOURCE) \
203         && !defined(OPENSSL_SYS_VXWORKS) \
204 #  define TERMIOS
205 #  undef  TERMIO
206 #  undef  SGTTY
207 # endif
208
209 #endif
210
211 #ifdef TERMIOS
212 # include <termios.h>
213 # define TTY_STRUCT             struct termios
214 # define TTY_FLAGS              c_lflag
215 # define TTY_get(tty,data)      tcgetattr(tty,data)
216 # define TTY_set(tty,data)      tcsetattr(tty,TCSANOW,data)
217 #endif
218
219 #ifdef TERMIO
220 # include <termio.h>
221 # define TTY_STRUCT             struct termio
222 # define TTY_FLAGS              c_lflag
223 # define TTY_get(tty,data)      ioctl(tty,TCGETA,data)
224 # define TTY_set(tty,data)      ioctl(tty,TCSETA,data)
225 #endif
226
227 #ifdef SGTTY
228 # include <sgtty.h>
229 # define TTY_STRUCT             struct sgttyb
230 # define TTY_FLAGS              sg_flags
231 # define TTY_get(tty,data)      ioctl(tty,TIOCGETP,data)
232 # define TTY_set(tty,data)      ioctl(tty,TIOCSETP,data)
233 #endif
234
235 #if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
236 # include <sys/ioctl.h>
237 #endif
238
239 #ifdef OPENSSL_SYS_MSDOS
240 # include <conio.h>
241 #endif
242
243 #ifdef OPENSSL_SYS_VMS
244 # include <ssdef.h>
245 # include <iodef.h>
246 # include <ttdef.h>
247 # include <descrip.h>
248 struct IOSB {
249     short iosb$w_value;
250     short iosb$w_count;
251     long iosb$l_info;
252 };
253 #endif
254
255 #if defined(MAC_OS_GUSI_SOURCE)
256 /*
257  * This one needs work. As a matter of fact the code is unoperational
258  * and this is only a trick to get it compiled.
259  *                                      <appro@fy.chalmers.se>
260  */
261 # define TTY_STRUCT int
262 #endif
263
264 #ifndef NX509_SIG
265 # define NX509_SIG 32
266 #endif
267
268 /* Define globals.  They are protected by a lock */
269 #ifdef SIGACTION
270 static struct sigaction savsig[NX509_SIG];
271 #else
272 static void (*savsig[NX509_SIG]) (int);
273 #endif
274
275 #ifdef OPENSSL_SYS_VMS
276 static struct IOSB iosb;
277 static $DESCRIPTOR(terminal, "TT");
278 static long tty_orig[3], tty_new[3]; /* XXX Is there any guarantee that this
279                                       * will always suffice for the actual
280                                       * structures? */
281 static long status;
282 static unsigned short channel = 0;
283 #else
284 # if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
285 static TTY_STRUCT tty_orig, tty_new;
286 # endif
287 #endif
288 static FILE *tty_in, *tty_out;
289 static int is_a_tty;
290
291 /* Declare static functions */
292 #if !defined(OPENSSL_SYS_WINCE)
293 static int read_till_nl(FILE *);
294 static void recsig(int);
295 static void pushsig(void);
296 static void popsig(void);
297 #endif
298 #if defined(OPENSSL_SYS_MSDOS)
299 static int noecho_fgets(char *buf, int size, FILE *tty);
300 #endif
301 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl);
302
303 static int read_string(UI *ui, UI_STRING *uis);
304 static int write_string(UI *ui, UI_STRING *uis);
305
306 static int open_console(UI *ui);
307 static int echo_console(UI *ui);
308 static int noecho_console(UI *ui);
309 static int close_console(UI *ui);
310
311 static UI_METHOD ui_openssl = {
312     "OpenSSL default user interface",
313     open_console,
314     write_string,
315     NULL,                       /* No flusher is needed for command lines */
316     read_string,
317     close_console,
318     NULL
319 };
320
321 /* The method with all the built-in thingies */
322 UI_METHOD *UI_OpenSSL(void)
323 {
324     return &ui_openssl;
325 }
326
327 /*
328  * The following function makes sure that info and error strings are printed
329  * before any prompt.
330  */
331 static int write_string(UI *ui, UI_STRING *uis)
332 {
333     switch (UI_get_string_type(uis)) {
334     case UIT_ERROR:
335     case UIT_INFO:
336         fputs(UI_get0_output_string(uis), tty_out);
337         fflush(tty_out);
338         break;
339     default:
340         break;
341     }
342     return 1;
343 }
344
345 static int read_string(UI *ui, UI_STRING *uis)
346 {
347     int ok = 0;
348
349     switch (UI_get_string_type(uis)) {
350     case UIT_BOOLEAN:
351         fputs(UI_get0_output_string(uis), tty_out);
352         fputs(UI_get0_action_string(uis), tty_out);
353         fflush(tty_out);
354         return read_string_inner(ui, uis,
355                                  UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
356                                  0);
357     case UIT_PROMPT:
358         fputs(UI_get0_output_string(uis), tty_out);
359         fflush(tty_out);
360         return read_string_inner(ui, uis,
361                                  UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
362                                  1);
363     case UIT_VERIFY:
364         fprintf(tty_out, "Verifying - %s", UI_get0_output_string(uis));
365         fflush(tty_out);
366         if ((ok = read_string_inner(ui, uis,
367                                     UI_get_input_flags(uis) &
368                                     UI_INPUT_FLAG_ECHO, 1)) <= 0)
369             return ok;
370         if (strcmp(UI_get0_result_string(uis), UI_get0_test_string(uis)) != 0) {
371             fprintf(tty_out, "Verify failure\n");
372             fflush(tty_out);
373             return 0;
374         }
375         break;
376     default:
377         break;
378     }
379     return 1;
380 }
381
382 #if !defined(OPENSSL_SYS_WINCE)
383 /* Internal functions to read a string without echoing */
384 static int read_till_nl(FILE *in)
385 {
386 # define SIZE 4
387     char buf[SIZE + 1];
388
389     do {
390         if (!fgets(buf, SIZE, in))
391             return 0;
392     } while (strchr(buf, '\n') == NULL);
393     return 1;
394 }
395
396 static volatile sig_atomic_t intr_signal;
397 #endif
398
399 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
400 {
401     static int ps;
402     int ok;
403     char result[BUFSIZ];
404     int maxsize = BUFSIZ - 1;
405 #if !defined(OPENSSL_SYS_WINCE)
406     char *p;
407
408     intr_signal = 0;
409     ok = 0;
410     ps = 0;
411
412     pushsig();
413     ps = 1;
414
415     if (!echo && !noecho_console(ui))
416         goto error;
417     ps = 2;
418
419     result[0] = '\0';
420 # ifdef OPENSSL_SYS_MSDOS
421     if (!echo) {
422         noecho_fgets(result, maxsize, tty_in);
423         p = result;             /* FIXME: noecho_fgets doesn't return errors */
424     } else
425         p = fgets(result, maxsize, tty_in);
426 # else
427     p = fgets(result, maxsize, tty_in);
428 # endif
429     if (!p)
430         goto error;
431     if (feof(tty_in))
432         goto error;
433     if (ferror(tty_in))
434         goto error;
435     if ((p = (char *)strchr(result, '\n')) != NULL) {
436         if (strip_nl)
437             *p = '\0';
438     } else if (!read_till_nl(tty_in))
439         goto error;
440     if (UI_set_result(ui, uis, result) >= 0)
441         ok = 1;
442
443  error:
444     if (intr_signal == SIGINT)
445         ok = -1;
446     if (!echo)
447         fprintf(tty_out, "\n");
448     if (ps >= 2 && !echo && !echo_console(ui))
449         ok = 0;
450
451     if (ps >= 1)
452         popsig();
453 #else
454     ok = 1;
455 #endif
456
457     OPENSSL_cleanse(result, BUFSIZ);
458     return ok;
459 }
460
461 /* Internal functions to open, handle and close a channel to the console.  */
462 static int open_console(UI *ui)
463 {
464     CRYPTO_THREAD_write_lock(ui->lock);
465     is_a_tty = 1;
466
467 #if defined(OPENSSL_SYS_VXWORKS)
468     tty_in = stdin;
469     tty_out = stderr;
470 #else
471 # ifdef OPENSSL_SYS_MSDOS
472 #  define DEV_TTY "con"
473 # else
474 #  define DEV_TTY "/dev/tty"
475 # endif
476     if ((tty_in = fopen(DEV_TTY, "r")) == NULL)
477         tty_in = stdin;
478     if ((tty_out = fopen(DEV_TTY, "w")) == NULL)
479         tty_out = stderr;
480 #endif
481
482 #if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
483     if (TTY_get(fileno(tty_in), &tty_orig) == -1) {
484 # ifdef ENOTTY
485         if (errno == ENOTTY)
486             is_a_tty = 0;
487         else
488 # endif
489 # ifdef EINVAL
490             /*
491              * Ariel Glenn ariel@columbia.edu reports that solaris can return
492              * EINVAL instead.  This should be ok
493              */
494         if (errno == EINVAL)
495             is_a_tty = 0;
496         else
497 # endif
498             return 0;
499     }
500 #endif
501 #ifdef OPENSSL_SYS_VMS
502     status = sys$assign(&terminal, &channel, 0, 0);
503     if (status != SS$_NORMAL)
504         return 0;
505     status =
506         sys$qiow(0, channel, IO$_SENSEMODE, &iosb, 0, 0, tty_orig, 12, 0, 0,
507                  0, 0);
508     if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
509         return 0;
510 #endif
511     return 1;
512 }
513
514 static int noecho_console(UI *ui)
515 {
516 #ifdef TTY_FLAGS
517     memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
518     tty_new.TTY_FLAGS &= ~ECHO;
519 #endif
520
521 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
522     if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
523         return 0;
524 #endif
525 #ifdef OPENSSL_SYS_VMS
526     tty_new[0] = tty_orig[0];
527     tty_new[1] = tty_orig[1] | TT$M_NOECHO;
528     tty_new[2] = tty_orig[2];
529     status =
530         sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12, 0, 0, 0,
531                  0);
532     if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
533         return 0;
534 #endif
535     return 1;
536 }
537
538 static int echo_console(UI *ui)
539 {
540 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
541     memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
542     tty_new.TTY_FLAGS |= ECHO;
543 #endif
544
545 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
546     if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
547         return 0;
548 #endif
549 #ifdef OPENSSL_SYS_VMS
550     tty_new[0] = tty_orig[0];
551     tty_new[1] = tty_orig[1] & ~TT$M_NOECHO;
552     tty_new[2] = tty_orig[2];
553     status =
554         sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12, 0, 0, 0,
555                  0);
556     if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
557         return 0;
558 #endif
559     return 1;
560 }
561
562 static int close_console(UI *ui)
563 {
564     if (tty_in != stdin)
565         fclose(tty_in);
566     if (tty_out != stderr)
567         fclose(tty_out);
568 #ifdef OPENSSL_SYS_VMS
569     status = sys$dassgn(channel);
570 #endif
571     CRYPTO_THREAD_unlock(ui->lock);
572
573     return 1;
574 }
575
576 #if !defined(OPENSSL_SYS_WINCE)
577 /* Internal functions to handle signals and act on them */
578 static void pushsig(void)
579 {
580 # ifndef OPENSSL_SYS_WIN32
581     int i;
582 # endif
583 # ifdef SIGACTION
584     struct sigaction sa;
585
586     memset(&sa, 0, sizeof(sa));
587     sa.sa_handler = recsig;
588 # endif
589
590 # ifdef OPENSSL_SYS_WIN32
591     savsig[SIGABRT] = signal(SIGABRT, recsig);
592     savsig[SIGFPE] = signal(SIGFPE, recsig);
593     savsig[SIGILL] = signal(SIGILL, recsig);
594     savsig[SIGINT] = signal(SIGINT, recsig);
595     savsig[SIGSEGV] = signal(SIGSEGV, recsig);
596     savsig[SIGTERM] = signal(SIGTERM, recsig);
597 # else
598     for (i = 1; i < NX509_SIG; i++) {
599 #  ifdef SIGUSR1
600         if (i == SIGUSR1)
601             continue;
602 #  endif
603 #  ifdef SIGUSR2
604         if (i == SIGUSR2)
605             continue;
606 #  endif
607 #  ifdef SIGKILL
608         if (i == SIGKILL)       /* We can't make any action on that. */
609             continue;
610 #  endif
611 #  ifdef SIGACTION
612         sigaction(i, &sa, &savsig[i]);
613 #  else
614         savsig[i] = signal(i, recsig);
615 #  endif
616     }
617 # endif
618
619 # ifdef SIGWINCH
620     signal(SIGWINCH, SIG_DFL);
621 # endif
622 }
623
624 static void popsig(void)
625 {
626 # ifdef OPENSSL_SYS_WIN32
627     signal(SIGABRT, savsig[SIGABRT]);
628     signal(SIGFPE, savsig[SIGFPE]);
629     signal(SIGILL, savsig[SIGILL]);
630     signal(SIGINT, savsig[SIGINT]);
631     signal(SIGSEGV, savsig[SIGSEGV]);
632     signal(SIGTERM, savsig[SIGTERM]);
633 # else
634     int i;
635     for (i = 1; i < NX509_SIG; i++) {
636 #  ifdef SIGUSR1
637         if (i == SIGUSR1)
638             continue;
639 #  endif
640 #  ifdef SIGUSR2
641         if (i == SIGUSR2)
642             continue;
643 #  endif
644 #  ifdef SIGACTION
645         sigaction(i, &savsig[i], NULL);
646 #  else
647         signal(i, savsig[i]);
648 #  endif
649     }
650 # endif
651 }
652
653 static void recsig(int i)
654 {
655     intr_signal = i;
656 }
657 #endif
658
659 /* Internal functions specific for Windows */
660 #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WINCE)
661 static int noecho_fgets(char *buf, int size, FILE *tty)
662 {
663     int i;
664     char *p;
665
666     p = buf;
667     for (;;) {
668         if (size == 0) {
669             *p = '\0';
670             break;
671         }
672         size--;
673 # if defined(_WIN32)
674         i = _getch();
675 # else
676         i = getch();
677 # endif
678         if (i == '\r')
679             i = '\n';
680         *(p++) = i;
681         if (i == '\n') {
682             *p = '\0';
683             break;
684         }
685     }
686 # ifdef WIN_CONSOLE_BUG
687     /*
688      * Win95 has several evil console bugs: one of these is that the last
689      * character read using getch() is passed to the next read: this is
690      * usually a CR so this can be trouble. No STDIO fix seems to work but
691      * flushing the console appears to do the trick.
692      */
693     {
694         HANDLE inh;
695         inh = GetStdHandle(STD_INPUT_HANDLE);
696         FlushConsoleInputBuffer(inh);
697     }
698 # endif
699     return (strlen(buf));
700 }
701 #endif