Remove /* foo.c */ comments
[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, NETWARE 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         && !defined(OPENSSL_SYS_NETWARE)
205 #  define TERMIOS
206 #  undef  TERMIO
207 #  undef  SGTTY
208 # endif
209
210 #endif
211
212 #ifdef TERMIOS
213 # include <termios.h>
214 # define TTY_STRUCT             struct termios
215 # define TTY_FLAGS              c_lflag
216 # define TTY_get(tty,data)      tcgetattr(tty,data)
217 # define TTY_set(tty,data)      tcsetattr(tty,TCSANOW,data)
218 #endif
219
220 #ifdef TERMIO
221 # include <termio.h>
222 # define TTY_STRUCT             struct termio
223 # define TTY_FLAGS              c_lflag
224 # define TTY_get(tty,data)      ioctl(tty,TCGETA,data)
225 # define TTY_set(tty,data)      ioctl(tty,TCSETA,data)
226 #endif
227
228 #ifdef SGTTY
229 # include <sgtty.h>
230 # define TTY_STRUCT             struct sgttyb
231 # define TTY_FLAGS              sg_flags
232 # define TTY_get(tty,data)      ioctl(tty,TIOCGETP,data)
233 # define TTY_set(tty,data)      ioctl(tty,TIOCSETP,data)
234 #endif
235
236 #if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
237 # include <sys/ioctl.h>
238 #endif
239
240 #ifdef OPENSSL_SYS_MSDOS
241 # include <conio.h>
242 #endif
243
244 #ifdef OPENSSL_SYS_VMS
245 # include <ssdef.h>
246 # include <iodef.h>
247 # include <ttdef.h>
248 # include <descrip.h>
249 struct IOSB {
250     short iosb$w_value;
251     short iosb$w_count;
252     long iosb$l_info;
253 };
254 #endif
255
256 #if defined(MAC_OS_GUSI_SOURCE) || defined(OPENSSL_SYS_NETWARE)
257 /*
258  * This one needs work. As a matter of fact the code is unoperational
259  * and this is only a trick to get it compiled.
260  *                                      <appro@fy.chalmers.se>
261  */
262 # define TTY_STRUCT int
263 #endif
264
265 #ifndef NX509_SIG
266 # define NX509_SIG 32
267 #endif
268
269 /* Define globals.  They are protected by a lock */
270 #ifdef SIGACTION
271 static struct sigaction savsig[NX509_SIG];
272 #else
273 static void (*savsig[NX509_SIG]) (int);
274 #endif
275
276 #ifdef OPENSSL_SYS_VMS
277 static struct IOSB iosb;
278 static $DESCRIPTOR(terminal, "TT");
279 static long tty_orig[3], tty_new[3]; /* XXX Is there any guarantee that this
280                                       * will always suffice for the actual
281                                       * structures? */
282 static long status;
283 static unsigned short channel = 0;
284 #else
285 # if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
286 static TTY_STRUCT tty_orig, tty_new;
287 # endif
288 #endif
289 static FILE *tty_in, *tty_out;
290 static int is_a_tty;
291
292 /* Declare static functions */
293 #if !defined(OPENSSL_SYS_WINCE)
294 static int read_till_nl(FILE *);
295 static void recsig(int);
296 static void pushsig(void);
297 static void popsig(void);
298 #endif
299 #if defined(OPENSSL_SYS_MSDOS)
300 static int noecho_fgets(char *buf, int size, FILE *tty);
301 #endif
302 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl);
303
304 static int read_string(UI *ui, UI_STRING *uis);
305 static int write_string(UI *ui, UI_STRING *uis);
306
307 static int open_console(UI *ui);
308 static int echo_console(UI *ui);
309 static int noecho_console(UI *ui);
310 static int close_console(UI *ui);
311
312 static UI_METHOD ui_openssl = {
313     "OpenSSL default user interface",
314     open_console,
315     write_string,
316     NULL,                       /* No flusher is needed for command lines */
317     read_string,
318     close_console,
319     NULL
320 };
321
322 /* The method with all the built-in thingies */
323 UI_METHOD *UI_OpenSSL(void)
324 {
325     return &ui_openssl;
326 }
327
328 /*
329  * The following function makes sure that info and error strings are printed
330  * before any prompt.
331  */
332 static int write_string(UI *ui, UI_STRING *uis)
333 {
334     switch (UI_get_string_type(uis)) {
335     case UIT_ERROR:
336     case UIT_INFO:
337         fputs(UI_get0_output_string(uis), tty_out);
338         fflush(tty_out);
339         break;
340     default:
341         break;
342     }
343     return 1;
344 }
345
346 static int read_string(UI *ui, UI_STRING *uis)
347 {
348     int ok = 0;
349
350     switch (UI_get_string_type(uis)) {
351     case UIT_BOOLEAN:
352         fputs(UI_get0_output_string(uis), tty_out);
353         fputs(UI_get0_action_string(uis), tty_out);
354         fflush(tty_out);
355         return read_string_inner(ui, uis,
356                                  UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
357                                  0);
358     case UIT_PROMPT:
359         fputs(UI_get0_output_string(uis), tty_out);
360         fflush(tty_out);
361         return read_string_inner(ui, uis,
362                                  UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
363                                  1);
364     case UIT_VERIFY:
365         fprintf(tty_out, "Verifying - %s", UI_get0_output_string(uis));
366         fflush(tty_out);
367         if ((ok = read_string_inner(ui, uis,
368                                     UI_get_input_flags(uis) &
369                                     UI_INPUT_FLAG_ECHO, 1)) <= 0)
370             return ok;
371         if (strcmp(UI_get0_result_string(uis), UI_get0_test_string(uis)) != 0) {
372             fprintf(tty_out, "Verify failure\n");
373             fflush(tty_out);
374             return 0;
375         }
376         break;
377     default:
378         break;
379     }
380     return 1;
381 }
382
383 #if !defined(OPENSSL_SYS_WINCE)
384 /* Internal functions to read a string without echoing */
385 static int read_till_nl(FILE *in)
386 {
387 # define SIZE 4
388     char buf[SIZE + 1];
389
390     do {
391         if (!fgets(buf, SIZE, in))
392             return 0;
393     } while (strchr(buf, '\n') == NULL);
394     return 1;
395 }
396
397 static volatile sig_atomic_t intr_signal;
398 #endif
399
400 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
401 {
402     static int ps;
403     int ok;
404     char result[BUFSIZ];
405     int maxsize = BUFSIZ - 1;
406 #if !defined(OPENSSL_SYS_WINCE)
407     char *p;
408
409     intr_signal = 0;
410     ok = 0;
411     ps = 0;
412
413     pushsig();
414     ps = 1;
415
416     if (!echo && !noecho_console(ui))
417         goto error;
418     ps = 2;
419
420     result[0] = '\0';
421 # ifdef OPENSSL_SYS_MSDOS
422     if (!echo) {
423         noecho_fgets(result, maxsize, tty_in);
424         p = result;             /* FIXME: noecho_fgets doesn't return errors */
425     } else
426         p = fgets(result, maxsize, tty_in);
427 # else
428     p = fgets(result, maxsize, tty_in);
429 # endif
430     if (!p)
431         goto error;
432     if (feof(tty_in))
433         goto error;
434     if (ferror(tty_in))
435         goto error;
436     if ((p = (char *)strchr(result, '\n')) != NULL) {
437         if (strip_nl)
438             *p = '\0';
439     } else if (!read_till_nl(tty_in))
440         goto error;
441     if (UI_set_result(ui, uis, result) >= 0)
442         ok = 1;
443
444  error:
445     if (intr_signal == SIGINT)
446         ok = -1;
447     if (!echo)
448         fprintf(tty_out, "\n");
449     if (ps >= 2 && !echo && !echo_console(ui))
450         ok = 0;
451
452     if (ps >= 1)
453         popsig();
454 #else
455     ok = 1;
456 #endif
457
458     OPENSSL_cleanse(result, BUFSIZ);
459     return ok;
460 }
461
462 /* Internal functions to open, handle and close a channel to the console.  */
463 static int open_console(UI *ui)
464 {
465     CRYPTO_w_lock(CRYPTO_LOCK_UI);
466     is_a_tty = 1;
467
468 #if defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_NETWARE)
469     tty_in = stdin;
470     tty_out = stderr;
471 #else
472 # ifdef OPENSSL_SYS_MSDOS
473 #  define DEV_TTY "con"
474 # else
475 #  define DEV_TTY "/dev/tty"
476 # endif
477     if ((tty_in = fopen(DEV_TTY, "r")) == NULL)
478         tty_in = stdin;
479     if ((tty_out = fopen(DEV_TTY, "w")) == NULL)
480         tty_out = stderr;
481 #endif
482
483 #if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
484     if (TTY_get(fileno(tty_in), &tty_orig) == -1) {
485 # ifdef ENOTTY
486         if (errno == ENOTTY)
487             is_a_tty = 0;
488         else
489 # endif
490 # ifdef EINVAL
491             /*
492              * Ariel Glenn ariel@columbia.edu reports that solaris can return
493              * EINVAL instead.  This should be ok
494              */
495         if (errno == EINVAL)
496             is_a_tty = 0;
497         else
498 # endif
499             return 0;
500     }
501 #endif
502 #ifdef OPENSSL_SYS_VMS
503     status = sys$assign(&terminal, &channel, 0, 0);
504     if (status != SS$_NORMAL)
505         return 0;
506     status =
507         sys$qiow(0, channel, IO$_SENSEMODE, &iosb, 0, 0, tty_orig, 12, 0, 0,
508                  0, 0);
509     if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
510         return 0;
511 #endif
512     return 1;
513 }
514
515 static int noecho_console(UI *ui)
516 {
517 #ifdef TTY_FLAGS
518     memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
519     tty_new.TTY_FLAGS &= ~ECHO;
520 #endif
521
522 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
523     if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
524         return 0;
525 #endif
526 #ifdef OPENSSL_SYS_VMS
527     tty_new[0] = tty_orig[0];
528     tty_new[1] = tty_orig[1] | TT$M_NOECHO;
529     tty_new[2] = tty_orig[2];
530     status =
531         sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12, 0, 0, 0,
532                  0);
533     if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
534         return 0;
535 #endif
536     return 1;
537 }
538
539 static int echo_console(UI *ui)
540 {
541 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
542     memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
543     tty_new.TTY_FLAGS |= ECHO;
544 #endif
545
546 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
547     if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
548         return 0;
549 #endif
550 #ifdef OPENSSL_SYS_VMS
551     tty_new[0] = tty_orig[0];
552     tty_new[1] = tty_orig[1] & ~TT$M_NOECHO;
553     tty_new[2] = tty_orig[2];
554     status =
555         sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12, 0, 0, 0,
556                  0);
557     if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
558         return 0;
559 #endif
560     return 1;
561 }
562
563 static int close_console(UI *ui)
564 {
565     if (tty_in != stdin)
566         fclose(tty_in);
567     if (tty_out != stderr)
568         fclose(tty_out);
569 #ifdef OPENSSL_SYS_VMS
570     status = sys$dassgn(channel);
571 #endif
572     CRYPTO_w_unlock(CRYPTO_LOCK_UI);
573
574     return 1;
575 }
576
577 #if !defined(OPENSSL_SYS_WINCE)
578 /* Internal functions to handle signals and act on them */
579 static void pushsig(void)
580 {
581 # ifndef OPENSSL_SYS_WIN32
582     int i;
583 # endif
584 # ifdef SIGACTION
585     struct sigaction sa;
586
587     memset(&sa, 0, sizeof(sa));
588     sa.sa_handler = recsig;
589 # endif
590
591 # ifdef OPENSSL_SYS_WIN32
592     savsig[SIGABRT] = signal(SIGABRT, recsig);
593     savsig[SIGFPE] = signal(SIGFPE, recsig);
594     savsig[SIGILL] = signal(SIGILL, recsig);
595     savsig[SIGINT] = signal(SIGINT, recsig);
596     savsig[SIGSEGV] = signal(SIGSEGV, recsig);
597     savsig[SIGTERM] = signal(SIGTERM, recsig);
598 # else
599     for (i = 1; i < NX509_SIG; i++) {
600 #  ifdef SIGUSR1
601         if (i == SIGUSR1)
602             continue;
603 #  endif
604 #  ifdef SIGUSR2
605         if (i == SIGUSR2)
606             continue;
607 #  endif
608 #  ifdef SIGKILL
609         if (i == SIGKILL)       /* We can't make any action on that. */
610             continue;
611 #  endif
612 #  ifdef SIGACTION
613         sigaction(i, &sa, &savsig[i]);
614 #  else
615         savsig[i] = signal(i, recsig);
616 #  endif
617     }
618 # endif
619
620 # ifdef SIGWINCH
621     signal(SIGWINCH, SIG_DFL);
622 # endif
623 }
624
625 static void popsig(void)
626 {
627 # ifdef OPENSSL_SYS_WIN32
628     signal(SIGABRT, savsig[SIGABRT]);
629     signal(SIGFPE, savsig[SIGFPE]);
630     signal(SIGILL, savsig[SIGILL]);
631     signal(SIGINT, savsig[SIGINT]);
632     signal(SIGSEGV, savsig[SIGSEGV]);
633     signal(SIGTERM, savsig[SIGTERM]);
634 # else
635     int i;
636     for (i = 1; i < NX509_SIG; i++) {
637 #  ifdef SIGUSR1
638         if (i == SIGUSR1)
639             continue;
640 #  endif
641 #  ifdef SIGUSR2
642         if (i == SIGUSR2)
643             continue;
644 #  endif
645 #  ifdef SIGACTION
646         sigaction(i, &savsig[i], NULL);
647 #  else
648         signal(i, savsig[i]);
649 #  endif
650     }
651 # endif
652 }
653
654 static void recsig(int i)
655 {
656     intr_signal = i;
657 }
658 #endif
659
660 /* Internal functions specific for Windows */
661 #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WINCE)
662 static int noecho_fgets(char *buf, int size, FILE *tty)
663 {
664     int i;
665     char *p;
666
667     p = buf;
668     for (;;) {
669         if (size == 0) {
670             *p = '\0';
671             break;
672         }
673         size--;
674 # if defined(_WIN32)
675         i = _getch();
676 # else
677         i = getch();
678 # endif
679         if (i == '\r')
680             i = '\n';
681         *(p++) = i;
682         if (i == '\n') {
683             *p = '\0';
684             break;
685         }
686     }
687 # ifdef WIN_CONSOLE_BUG
688     /*
689      * Win95 has several evil console bugs: one of these is that the last
690      * character read using getch() is passed to the next read: this is
691      * usually a CR so this can be trouble. No STDIO fix seems to work but
692      * flushing the console appears to do the trick.
693      */
694     {
695         HANDLE inh;
696         inh = GetStdHandle(STD_INPUT_HANDLE);
697         FlushConsoleInputBuffer(inh);
698     }
699 # endif
700     return (strlen(buf));
701 }
702 #endif