[comp.lang.c] Defeating redirection

dan@srs.UUCP (02/01/87)

> 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?)