Unbuffered I/O using MicroSoft C 3.0

perry at omepd.UUCP perry at omepd.UUCP
Wed Jan 28 13:38:27 AEST 1987


In article <650 at uw-warp.UUCP> tom at 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 at inteloa.intel.com



More information about the Comp.lang.c mailing list