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