Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!rutgers!sri-spam!sri-unix!hplabs!cae780!amdcad!amd!intelca!mipos3!omepd!perry From: perry@omepd.UUCP Newsgroups: comp.lang.c,comp.sys.ibm.pc Subject: Re: Unbuffered I/O using MicroSoft C 3.0 Message-ID: <338@omepd> Date: Tue, 27-Jan-87 22:38:27 EST Article-I.D.: omepd.338 Posted: Tue Jan 27 22:38:27 1987 Date-Received: Thu, 29-Jan-87 06:16:19 EST References: <1867@sunybcs.UUCP> <645@uw-warp.UUCP> <650@uw-warp.UUCP> Sender: news@omepd Reply-To: perry@inteloa.intel.com (Perry The Cynic) Organization: Intel Corp., Hillsboro Lines: 41 Keywords: direct console input, keyboard Summary: How to make sure that *direct console input* reads from the keyboard Xref: watmath comp.lang.c:870 comp.sys.ibm.pc:1213 In article <650@uw-warp.UUCP> tom@uw-warp.UUCP (Tom May) writes: > [Describing a way to call the MS-DOS INT 21 direct console input function] > ... >It turns out that the statement about reading the keyboard, not stdin, was >incorrect. Although the function is called Direct Console Input or something >like that (my DOS machine + manuals are 15 miles away from my USENET machine), >further inspection of the DOS Technical Reference manual reveals that all those >so-called console routines actually read stdin. Even the Keyboard Input >function reads stdin! (At least the doc says it does, I haven't tried it.) > >Also, the MSC library routines getch() and getche() use DOS functions which >read characters from stdin, not con as the manual seems to imply. Right! This one got me too, first time I tried it. My MS-DOS programmer's reference says *standard input* all the way, but with a name like *console input*... There is a quite simple way to make sure that stdin is really the keyboard. The (MSC) call sequence close(0); dup(2); effectively duplicates the stderr channel onto stdin. As you can't redirect stderr in MSDOS (at least from COMMAND.COM), this is sure to be the keyboard. That sequence loses whatever was redirected into stdin, of course. If you need it, you can use code like this: FILE *stin; stin=fopen("NUL","r"); /* grab a free file slot */ close(fileno(stin)); dup(0); /* duplicate stdin into stin */ close(0); dup(2); /* duplicate stderr into stdin */ From then on, *stdin* is the keyboard and *stin* is what was piped into your program. It seems that the theoretically different I/O modes (stderr writing, stdin reading) don't make any difference to MS-DOS. Btw, this code should work in any language that accesses MS-DOS file handles directly; it uses a MS-DOS feature, not a MSC speciality. Hope this help you... ------------------------------------------------------------------------ << Perry The Cynic >> ...!tektronix!ogcvax!omepd!inteloa!perry ...!verdix!omepd!inteloa!perry (Peter Kiehtreiber) -or try- perry@inteloa.intel.com