/*
 *	This is an Amiga version of the Unix system calls that need
 *	to be implemented in order to link the C-News package...
 */

#include <stdio.h>				/* For file_i/o stuff */
#include <stdlib.h>				/* Declarations of ctime(), etc */
#ifdef MCH_AMIGA
#  include <time.h>				/* For time_t */
#else
#  include <sys/types.h>		/* ditto */
#endif
#include <libraries/dos.h>		/* For TICKS */

extern struct Process *FindTask();

static FILE *syslogf = 0;

/* VARARGS1 */
sysfile(fmt, args)
char *fmt;
int args;
{
	static char first_time = TRUE;

	if (first_time) {
		first_time = 0;
		syslogf = fopen("ram:syscall_log", "w");
	}
	if (syslogf) {
		vfprintf(syslogf, fmt, &args);
		fflush(syslogf);
	}
	return( 0 );
}

short getpid()
{
	return( (short) ((long) FindTask(0L) & 0x7FFFL) );
}

static short	old_uid  = 0, old_gid  = 0;
static short	old_euid = 0, old_egid = 0;

short getuid()
{
	return( old_uid );
}

short getgid()
{
	return( old_gid );
}

short geteuid()
{
	return( old_euid );
}

short getegid()
{
	return( old_egid );
}

short setuid(new_uid)
short new_uid;
{
	short xxx;

	xxx = old_uid;
	old_uid = new_uid;
	return( xxx );
}

short setgid(new_gid)
short new_gid;
{
	short xxx;

	xxx = old_gid;
	old_gid = new_gid;
	return( xxx );
}

chown(fname, uid)
char *fname;
short uid;
{
	sysfile("Supposed to chown on `%s' to `%hd'\n", fname, uid);
	return( 0 );
}

link(from, to)
char *from, *to;
{
	sysfile("Supposed to link from `%s' to `%s'\n", from, to);
	return( 0 );
}

#ifndef MCH_AMIGA		/* If not on Manx, ... */
sleep(sec)
long sec;
{
	Delay( sec * TICKS_PER_SECOND );
	return( 0 );
}
#endif /* MCH_AMIGA */

short umask(msk)
short msk;
{
	static short old_msk = 0;
	short xxx;

	xxx = old_msk;
	old_msk = msk;
	return( xxx );
}

int alarm(secs)
int secs;
{
	if (secs) {
		time_t tm;
		(void) time(&tm);
		sysfile("Alarm(%d) has been called at %s", secs, ctime(&tm));
	}
	return( 0 );
}

#ifndef MCH_AMIGA
void mktemp(p)
register char *p;
{
	register char *tmp;
	register int count = 0;
	FILE *fp;

	tmp = p + strlen(p);
	while (*--tmp == 'X')
		count++;
	sprintf(tmp+1, "%0*.*ld", count, count, (long) getpid());
	if (fp = fopen( p, "w" ))
		fclose(fp);
}
#endif /* MCH_AMIGA */
