/*--------------------------------------------------------------------------*/
/* Include files															*/
/*--------------------------------------------------------------------------*/

#include	<ctype.h>
#include	<errno.h>

#include	<stdlib.h>
#include	<fcntl.h>
#include	<sys\types.h>
#include	<sys\stat.h>
#include	<io.h>

#define 	INCL_DOSPROCESS
#include	<os2.h>

#include	<string.h>
#include	<dos.h>
#include	<time.h>

/*--------------------------------------------------------------------------*/
/* Static function declarations 											*/
/*--------------------------------------------------------------------------*/

/*	... NONE ...  */

/*--------------------------------------------------------------------------*/
/* Static variable definitions												*/
/*--------------------------------------------------------------------------*/

struct FILEINFO {
   char rsvd[21];
   char attrib;
   unsigned wr_date;
   unsigned wr_time;
   long size;
   char name[13];
   char nill;
   };

struct FileFindBuf {
		unsigned create_date;			/* date of file creation */
		unsigned create_time;			/* time of file creation */
		unsigned access_date;			/* date of last access */
		unsigned access_time;			/* time of last access */
		unsigned wr_date;				/* date of last write */
		unsigned wr_time;				/* time of last write */
		unsigned long size; 			/* file size (end of data) */
		unsigned long falloc_size;		/* file allocated size */
		unsigned attrib;				/* attributes of the file */
		unsigned char string_len;		/* returned length of ascii name str. */
										/* length does not include null byte */
		char name[13];					/* name string */
		};

static struct FileFindBuf InfoBuf;
static struct FileFindBuf InfoBufA;

/*--------------------------------------------------------------------------*/
/* External variable declarations											*/
/*--------------------------------------------------------------------------*/

/*	... NONE ...  */

/*--------------------------------------------------------------------------*/
/* Locally defined globals													*/
/*--------------------------------------------------------------------------*/

HDIR					hDir;
HDIR					hDirA;
USHORT					cSearch;
USHORT					usAttrib;

/*--------------------------------------------------------------------------*/
/* Local constants															*/
/*--------------------------------------------------------------------------*/

#define FILENAMELEN 13

/****************************************************************************/


/*--------------------------------------------------------------------------*/
/* DIR_FINDFIRST									 FOR PORTABILITY		*/
/*--------------------------------------------------------------------------*/

int dir_findfirst(char far * filename, int attribute, struct FILEINFO * dta)
{
	hDir	 = 0x0001;
	usAttrib = attribute;
	cSearch  = 1;

	if ((DosFindFirst( filename
					 , &hDir
					 , usAttrib
					 , (PFILEFINDBUF) &InfoBuf
					 , (USHORT) (sizeof(InfoBuf) * cSearch)
					 , &cSearch
					 , (ULONG) NULL) != 0) || cSearch != 1)
	{
		(void) DosFindClose( hDir );
		errno = ENOENT;
		return (1);
	} else {
		dta->wr_date   = InfoBuf.wr_date;
		dta->wr_time   = InfoBuf.wr_time;
		dta->attrib    = (char) InfoBuf.attrib;
		dta->size	   = InfoBuf.size;
		strcpy( dta->name, InfoBuf.name);
		errno = 0;
		return (0);
	}
}

int dir_findfirsta(char far * filename, int attribute, struct FILEINFO * dta)
{
	hDirA	 = 0xffff;
	usAttrib = attribute;
	cSearch  = 1;

	if ((DosFindFirst( filename
					 , &hDirA
					 , usAttrib
					 , (PFILEFINDBUF) &InfoBufA
					 , (USHORT) (sizeof(InfoBufA) * cSearch)
					 , &cSearch
					 , (ULONG) NULL) != 0) || cSearch != 1)
	{
		(void) DosFindClose( hDirA );
		errno = ENOENT;
		return (1);
	} else {
		dta->wr_date   = InfoBufA.wr_date;
		dta->wr_time   = InfoBufA.wr_time;
		dta->attrib    = (char) InfoBufA.attrib;
		dta->size	   = InfoBufA.size;
		strcpy( dta->name, InfoBufA.name);
		errno = 0;
		return (0);
	}
}

/*--------------------------------------------------------------------------*/
/* DIR_FINDNEXT 									 FOR PORTABILITY		*/
/*--------------------------------------------------------------------------*/

int dir_findnext(struct FILEINFO * dta)
{

	cSearch  = 1;

	if ((DosFindNext( hDir
					, (PFILEFINDBUF) &InfoBuf
					, (USHORT) (sizeof(InfoBuf) * cSearch)
					, &cSearch) != 0) || (cSearch < 1))
	{
		(void) DosFindClose( hDir );
		errno = ENOENT;
		return (1);
	} else {
		dta->wr_date   = InfoBuf.wr_date;
		dta->wr_time   = InfoBuf.wr_time;
		dta->attrib    = (char) InfoBuf.attrib;
		dta->size	   = InfoBuf.size;
		strcpy( dta->name, InfoBuf.name);
		errno = 0;
		return (0);
	}
}

int dir_findnexta(struct FILEINFO * dta)
{

	cSearch  = 1;

	if ((DosFindNext( hDirA
					, (PFILEFINDBUF) &InfoBufA
					, (USHORT) (sizeof(InfoBufA) * cSearch)
					, &cSearch) != 0) || (cSearch < 1))
	{
		(void) DosFindClose( hDirA );
		errno = ENOENT;
		return (1);
	} else {
		dta->wr_date   = InfoBufA.wr_date;
		dta->wr_time   = InfoBufA.wr_time;
		dta->attrib    = (char) InfoBufA.attrib;
		dta->size	   = InfoBufA.size;
		strcpy( dta->name, InfoBufA.name);
		errno = 0;
		return (0);
	}
}

/*--------------------------------------------------------------------------*/
/*								  END OF FILE								*/
/*--------------------------------------------------------------------------*/
