/* from Dale Schumacher's dLibs library */

/* these will have to be adjusted at some time ++jrb */
/* use these with caution, TOS 1.4 still has double re-direction bug! */

#include <stddef.h>
#include <fcntl.h>
#include <osbind.h>
#include <errno.h>
#include <mintbind.h>
#include <unistd.h>

extern int __mint;

int dup(handle)
	int handle;
{
	register int rv;

	if (__mint)
		rv = Fcntl(handle, (long)0, F_DUPFD);
	else
		rv = Fdup(handle);

	if(rv < (__SMALLEST_VALID_HANDLE)) {
		errno = -rv; rv = -1;
	} else if (__OPEN_INDEX(rv) < __NHANDLES) {
	    __open_stat[__OPEN_INDEX(rv)] =
		__open_stat[__OPEN_INDEX(handle)];
	}
	return(rv);
}

int dup2(handle1, handle2)
	int handle1, handle2;
{
	int rv;

	close(handle2);
	if ((rv = Fforce(handle2, handle1)) < 0)
		errno = -rv;
	else if (__OPEN_INDEX(handle2) < __NHANDLES)
	    __open_stat[__OPEN_INDEX(handle2)] =
		__open_stat[__OPEN_INDEX(handle1)];
	return (rv < 0) ? -1 : handle2;
}
