/*
 *	Compatibility routines needed by relaynews.
 *	The sleep() routine is here 'cuz in the UUPC code
 *		it would generate "symbol redefined" if put in
 *		the libcnews library.
 */

#include <stdio.h>
#include <libraries/dos.h>
#include "news.h"

#ifdef AZTEC_C
#  include <functions.h>
#else
#  include <proto/exec.h>
#endif

void sleep(sec)
unsigned long sec;
{
	Delay( TICKS_PER_SECOND * sec );
}

int fork()
{
	return( -1 );
}

int system(s)
char *s;
{
	warning("attempting system() call: '%s'", s);
	return( -1 );
}

FILE *popen(name, mode)
char *name, *mode;
{
	if (index(mode, '+'))
		return( fopen(name, mode) );
	else {
		char buf[16];

		strcpy(buf, mode);
		strcat(buf, "+");
		return( fopen(name, buf) );
	}
}

int pclose(fp)
FILE *fp;
{
	return( fclose(fp) );
}

static void GetCurrentPath( path )
register char *path;
{
	char	s1[ 108 ];
	char	*name;
	struct FileInfoBlock fib;
	register struct FileLock *locka, *lockb = 0;

	locka = Lock("", ACCESS_READ );
	*path = s1[0] = '\0';

	while (locka != NULL) {
		Examine( locka, &fib );
		name = fib.fib_FileName;
		if ( *name == '\0' )
			strcpy( path, "RAM" ); /* Patch for Ram disk bug */
		else
			strcpy( path, name );
		lockb = ParentDir( locka );
		UnLock( locka );

		if (lockb == NULL)
			strcat( path, ":");
		else if (s1[0] != '\0')
			strcat( path, "/");
		strcat( path, s1 );
		strcpy( s1, path );
		locka = lockb;
	}
}

/*--------------------------------------------------------------*/
/*	getcwd: return the path name of the current directory	*/
/*--------------------------------------------------------------*/

char *getcwd( path, size )
char *path;
int size;
{
	if (path == (char *)NULL) {
		if ((path = malloc(108)) == NULL)
			return NULL;
	}
	GetCurrentPath( path );
	return path;
}

int	chdir(path)
char *path;
{
	register struct FileLock *lock, *oldLock;

	lock = Lock(path, ACCESS_READ);
	if (!lock)
		return (int) IoErr();
	oldLock = CurrentDir(lock);
	if (oldLock)
		UnLock(oldLock);
	return 0;
}

int mkdir( name )
char *name;
{
	extern int errno;
	register struct FileLock *lock;

	lock = CreateDir( name );
	if ( !lock )
	   return errno;
	UnLock( lock );
	return 0;
}

#if 0
/*
 *	If using Manx, should link with `heapmem.o'
 */
char *realloc(ptr, size)
char *ptr;
unsigned size;
{
	register char *tmp;
	register unsigned len;

	tmp = (char *) malloc( size );
	if (ptr) {
		len = * (long *) (ptr-4);
		movmem(ptr, tmp, (unsigned) min(len, size));
		free( ptr );
	}
	return( tmp );
}
#endif
