/*	:ts=4
 *	This is an Amiga version of the Unix link() system call
 *
 *	$Id$
 *
 *	$Log$
 */

#include <stdio.h>					/* For file_i/o stuff */
#include <exec/types.h>
#include <dos/dos.h>
#include <clib/exec_protos.h>		/* For FindTask() */
#include <clib/dos_protos.h>		/* For *Lock*(), MakeLink() */

#if INCLUDE_VERSION >= 36
# include <pragma/dos.h>
#endif

FILE **Link_a;						/* The address of art->a_artf  */

link(char *from, char *to)
{
	register int ret = 0;
#if INCLUDE_VERSION >= 36
	static short flag;
	BPTR lock;
	fpos_t pos;

	if (flag == 0) {
		extern struct DosLibrary *SysBase;
		flag = (short) SysBase->dl_lib.lib_Version;
	}
	if (flag > 35) {
		pos = (fpos_t) -1;
		if (*Link_a) {
			if (!fgetpos(*Link_a, &pos))
				fclose(*Link_a);
		}
		if (lock = Lock((UBYTE *)from, ACCESS_READ)) {
			if (!MakeLink((UBYTE *)to, lock, LINK_HARD)) {
				ret = IoErr();
#if 0
				warning("Couldn't make link (%s) from `%s' to `%s'",
					strerror(IoErr()), from, to);
#endif
			}
			UnLock(lock);
		} else {
			ret = IoErr();
#if 0
			warning("Couldn't obtain lock (%s) on `%s' during link to `%s'",
				strerror(IoErr()), from, to);
#endif
		}
		if (pos != (fpos_t) -1) {
			*Link_a = fopen(from, "a+");
			fsetpos(*Link_a, &pos);
		}
	}
#endif /* INCLUDE_VERSION */
	return( ret );
}
