[net.micro.atari16] Megamax I/O redirection fix

gert@nikhefh.uucp (Gert Poletiek) (11/03/86)

Here is a fix for the megamax library, for those who want I/O redirection
from within a shell (command line interpreter). Megamax programs could not be 
IOredirected since Megamax' ideas about standard IO are a bit weird.

The problem is caused by the idiotic file desciptor -32000 in the first
three slots of the stdio-stream table '_iob'.

When a character is written to (or read from) these
filedescriptors the MM lib uses probably a low numbered GemDos call 
(between 0 and 0x20) to do the IO. But these GemDos calls cause bombs to 
appear when the standard GemDos file descriptors 0 or 1 are redirected to 
another file by means of the Fdup and Fforce system calls. 
So a solution is to overwrite the values of _iob[0]._fd to _iob[2]._fd with 
real GemDos file descriptor numbers. 
The only drawback is that the lf to crlf mapping by the library is
lost. But this mapping *IS* done when you declare your program as
 
 main ( argc, argv )
 char **argv;

Is that documented anywhere ? How does MM-lib work ???

This causes inclusion of the _initargcv module from syslib during linking.
When _initargcv is not included cr to crlf mapping is not done.


The overwriting is done in the routine 'initstdio' which should be called from
'main' before any IO is done.
The 'exitstdio' routine puts the old values back, otherwise the bombs still
appear.

Do not use this trick when your programs are to be IO redirected from the
desktop.

Hope this solves the problem,

    Gert Poletiek
    Dutch National Institute for High Energy Physics
    Amsterdam
    The Netherlands

Uucp:
    .... { seismo | philabs } !mcvax!gert@nikhefh.uucp



------------------------------------------------------------------------------
#include <stdio.h>
/*
 *	Test program 
 */

main ()
{
	int	i;

/*
 *	initialize stdio library to use standard GemDos file descriptors 0 and 1.
 */

	initstdio();

	for ( i=0; i<3; i++ )  {
		printf ( "stream %d file descriptor %d\n", i, _iob[i]._fd );
		printf ( "flags = %x\n", _iob[i]._flag );
	}
	fprintf ( stdout, "test\ntest\ntest\n\n" );

/* 
 *	reset the stdio library to use the idiot Megamax descriptors again,
 *	otherwise it's possible that the application exits with three bombs.
 */
	exitstdio();
}
	


initstdio()
{
	_iob[0]._fd = 0;
	_iob[1]._fd = _iob[2]._fd = 1;
}



exitstdio()
{
	_iob[0]._fd = _iob[1]._fd = _iob[2]._fd = -32000;
}