Defeating redirection (was: Unbuffered I/O)

dan at srs.UUCP dan at srs.UUCP
Mon Feb 2 06:16:57 AEST 1987


> There is a wondrously simple way to ensure that I/O doesn't get redirected 
> without mucking about with DOS interrupts! Like so:
> 	FILE *fopen(), *console;
> 	console = fopen("\\dev\\con", "r");

There's an even less DOS-specific way:
	FILE *fdopen(), *console;
	/* Open a stream for input from whatever stderr is connected to */
	console = fdopen(dup(fileno(stderr)), "r");
The dup is there so you can close console without closing stderr.
This has the advantage that it works even when the console is not \dev\con
(for instance, if you CTTY COM1: and are working over a serial line).

Programs that use any of these tricks should be sure to document the fact.

- Dan Kegel
	(__@/  \@__ Hey! Don't I know you from somewhere?)



More information about the Comp.lang.c mailing list