/* Created 11/08/87 by -=+SDB+=- from file provided by MANX */
/* Copyright (C) 1987 by Scott Ballantyne */
/* May be freely used by ARP supporters/users */
/* If somebody were to modify the libraries to use ARP tracking, this would
 * be even shorter (and so would the libraries)
 */

extern int _argc;
extern char **_argv;
extern struct WBStartup *WBenchMsg;
extern void *MathBase, *MathTransBase,*DOSBase;
extern void *MathIeeeDoubBasBase, *MathIeeeDoubTransBase;
extern void *_oldtrap, **_trapaddr;

void (*_cln)();

void	_exit(int code);
void	_arpexit(int code,ULONG fail);

void
_exit(int code)
{
	_arpexit(code,0);
}

void
_arpexit(int code,ULONG fail)
{
	if(_devtab)
	{
		int fd;

		for(fd = 0 ; fd < _numdev ; fd++)
			close(fd);
	}

	if(_cln)			/* needed to block loading 2 instances of _exit() if c.lib/malloc() gets loaded instead */
		(*_cln)();

	if(_trapaddr)			/* clean up signal handling */
		*_trapaddr = _oldtrap;

	if(MathTransBase)
		CloseLibrary((struct Library *)MathTransBase);

	if(MathBase)
		CloseLibrary((struct Library *)MathBase);

	if(MathIeeeDoubBasBase)
		CloseLibrary((struct Library *)MathIeeeDoubBasBase);

	if(MathIeeeDoubTransBase)
		CloseLibrary((struct Library *)MathIeeeDoubTransBase);

	if(DOSBase)
		CloseLibrary((struct Library *)DOSBase);

	{
#asm
	mc68881
	move.l	(4).w,a6	;get ExecBase
	btst.b	#4,$129(a6)     ;check for 68881 flag in AttnFlags
	beq	1$		;skip if not
	move.l	a5,-(sp)
	lea	2$,a5
	jsr	-30(a6)         ;do it in supervisor mode
	move.l	(sp)+,a5
	bra	1$
2$
	clr.l	-(sp)
	frestore (sp)+          ;reset the ffp stuff
	rte			;and return
1$
#endasm
	}

	if(WBenchMsg)
	{
		Forbid();
		ReplyMsg((struct Message *)WBenchMsg);
	}

	ArpExit((long)code,fail);
}
