
#include "fcntl.h"

#define STDOUT  1
#define STDERR  2

#define SEEK_SET 0                      /* allocate top position */
#define SEEK_CUR 1                      /* allocate current position */
#define SEEK_END 2                      /* allocate bottom position */


#define BUFFER_SIZE  (32 * 1024)
#define NUMBER_OF_TRANSFERS   150

char acOutBuffer[BUFFER_SIZE];
char acInBuffer[BUFFER_SIZE];

char *pFilename = "Output.jnk";

/*
 *  Test the low level Read functions
 */
void ReadTest(void)
{
	int nFileID;
	int nCnt;
	int nBytesRead;

	/* File Open / Read test */
	nFileID = open(pFilename, O_RDONLY);
	if (nFileID != -1)
	{
		write(STDOUT, "Start Read of data    ", 22);

		for (nCnt = 0; nCnt < NUMBER_OF_TRANSFERS; nCnt++)
			nBytesRead = read(nFileID, acInBuffer, BUFFER_SIZE);

		write(STDOUT, "Completed Read of data", 22);
		close(nFileID);
	}
}


/*
 *  Test the low level Write functions
 */
void WriteTest(void)
{
	int nFileID;
	int nCnt;

	/* File Open / Write test */
	nFileID = open(pFilename, O_WRONLY | O_CREAT);
	if (nFileID != -1)
	{
		write(STDOUT, "Start Write of data    ", 23);

		for (nCnt = 0; nCnt < NUMBER_OF_TRANSFERS; nCnt++)
			write(nFileID, acOutBuffer,BUFFER_SIZE);

		write(STDOUT, "Completed Write of data", 23);
		close(nFileID);
	}
}


/*
 *  Test the low level Seek functions
 */
void SeekTest(void)
{
	int nFileID, nPos;

	nFileID = open(pFilename, O_RDONLY);
	if (nFileID != -1)
	{
		write(STDOUT, "Start Seek of data    ", 22);

		nPos = lseek(nFileID, 6, SEEK_SET);
		read(nFileID, acInBuffer, 10);
		write(STDOUT, acInBuffer, 10);

		nPos = lseek(nFileID, 6, SEEK_CUR);
		read(nFileID, acInBuffer, 10);
		write(STDOUT, acInBuffer, 10);

		nPos = lseek(nFileID, -10, SEEK_END);
		read(nFileID, acInBuffer, 10);
		write(STDOUT, acInBuffer, 10);

		write(STDOUT, "Completed Seek of data", 22);
		close(nFileID);
	}
}


/*
 *
 */
void InitialiseOutBuffer(void)
{
	int nCnt;
	/* initialise the buffer */
	for (nCnt = 0; nCnt < BUFFER_SIZE; nCnt++)
	{
		acOutBuffer[nCnt] = (char)('0' + nCnt%10);
	}
}


main()
{
	write(STDOUT, "Start of Test ", 14);

	InitialiseOutBuffer();

	/* Check default Write ports */
	write(STDOUT, "Data to stdout", 14);
	write(STDERR, "Data to stderr", 14);

	/* File Open / Write test */
	WriteTest();

	/* File Open / Read test */
	ReadTest();

	/* File Open / Seek test */
	SeekTest();


	for ( ; ; ) ;
}
