/* Header for some netware call  */

#include <stdio.h>
#include <dos.h>
#include <dir.h>
#include <mem.h>
#include <conio.h>
#include <string.h>
#include <process.h>

typedef unsigned char BYTE;
typedef unsigned int WORD;
typedef unsigned long LONG;

/* Request and Reply structures */
struct scanFileReq {
	WORD     Length;
	BYTE     Function;
	WORD     SequenceNumber;
	BYTE     DirectoryHandle;
	BYTE     SearchAttributes;
	BYTE     FilePathLength;
	BYTE     FilePath[255];
};

struct scanFileRep {
	WORD     Length;
	WORD     SequenceNumber;
	BYTE     FileName[14];
	BYTE     FileAttributes;
	BYTE     ExtendedFileAttributes;
	LONG     FileSize;
	WORD     CreationDate;
	WORD     LastAccessDate;
	LONG     LastUpdateDateAndTime;
	LONG     OwnerObjectID;
	LONG     LastArchiveDateAndTime;
	BYTE     Reserved[56];
};

struct scanDirReq {
	WORD    Length;
	BYTE    Function;
	BYTE    DirHandle;
	WORD    SubDirNumber;
	BYTE    DirPathLength;
	BYTE	DirPath[255];
};

struct scanDirRep {
	WORD    Length;
	BYTE    SubDirName[16];
	BYTE    CreationDateAndTime[4];
	LONG	OwnerObjectID;
	BYTE	RightsMask;
	BYTE	Unused;
	WORD	SubDirNumber;
};



/* Local Routines */
void	PrintFileInfo( struct scanFileRep *reply  );
int	GetFileInfo( struct scanFileReq *request, struct scanFileRep *reply);
void	ShowDate( void *date );
void	ShowDateAndTime( void *dateAndTime );
LONG	LongSwitch( void *before );
WORD	WordSwitch( void *before );
int	GetDirectoryInfo( struct scanDirReq *request, struct scanDirRep *reply);
void	PrintDirectoryInfo( struct scanDirRep *reply  );



/* body of function */
int GetFileInfo( struct scanFileReq *request, struct scanFileRep *reply) {

	_AH = 0xE3;
	_SI = (unsigned)request;
	_DI = (unsigned)reply;
	_ES = _DS;

	geninterrupt( 0x21 );

	return _AL;
}

void PrintFileInfo( struct scanFileRep *reply )
{
	printf("%-17s", reply->FileName);
	printf("File Size : %10ld %-20s %s\n", LongSwitch( &reply->FileSize),
	       (reply->ExtendedFileAttributes & 0x10)   ? "Transaction Bit Set/": " ",
	       (reply->ExtendedFileAttributes & 0x20)   ? "Index Bit Set/":" ");
	printf("Flag : [%-8s][%-8s][%-8s][%-8s][%-8s][%-8s][%-8s]\n",
	       (reply->FileAttributes   &0x01) ? "ReadOnly" : " ",
	       (reply->FileAttributes   &0x02) ? "Hidden  " : " ",
	       (reply->FileAttributes   &0x04) ? "System  " : " ",
	       (reply->FileAttributes   &0x08) ? "Execute " : " ",
	       (reply->FileAttributes   &0x10) ? "Subdir  " : " ",
	       (reply->FileAttributes   &0x20) ? "Archive " : " ",
	       (reply->FileAttributes   &0x80) ? "Sharable" : " " );
	printf( "Creation     Last Access       Last  Update           Last Archive\n");
	ShowDate( &reply->CreationDate );
	printf( "      ");
	ShowDate( &reply->LastAccessDate );
	printf( "       ");
	ShowDateAndTime( &reply->LastUpdateDateAndTime );
	printf( "      " );
	ShowDateAndTime(    &reply->LastArchiveDateAndTime );
	printf( "\n");
}

void ShowDate( void *date )
{

	int year, month, day;
	WORD newdate;

	newdate = WordSwitch( date );

	if ( !newdate ) {

	    printf( "  /  /   ");
	    return;
	}

	day = newdate & 0x1F;

	newdate >>= 5;
	month = newdate & 0xF;

	newdate >>= 4;
	year   =   newdate & 0x3F + 80;

	printf( "%02d/%02d/%02d", month, day, year);
}

void ShowDateAndTime( void *dateAndTime ) {

	int hours, minutes, seconds;
	WORD time;

	ShowDate( dateAndTime );

	time = WordSwitch( ((WORD*)dateAndTime) + 1);
	if ( !time ) {
		printf("  :  :  ");
		return;
	}

	seconds = ( time & 0x1F ) * 2;

	time >>= 5;
	minutes = time & 0x3F;

	time >>= 6;
	hours = time & 0x1F;

	printf( " %02d:%02d:%02d", hours, minutes, seconds);
}

LONG LongSwitch( void *before )
{

	BYTE *Bbefore=(BYTE *)before;
	BYTE after[4];

	after[0] = Bbefore[3];
	after[1] = Bbefore[2];
	after[2] = Bbefore[1];
	after[3] = Bbefore[0];

	return *( (LONG *)after );
}

WORD WordSwitch( void *before ) {

	BYTE *Bbefore=(BYTE *)before;
	BYTE after[2];

	after[0] = Bbefore[1];
	after[1] = Bbefore[0];

	return *( (WORD *) after );
}

int	GetDirectoryInfo( struct scanDirReq *request, struct scanDirRep *reply)
{
	_AH = 0xE2;
	_SI = (unsigned)request;
	_DI = (unsigned)reply;
	_ES = _DS;

	geninterrupt( 0x21 );

	return _AL;

}
void	PrintDirectoryInfo( struct scanDirRep *reply  )
{
	printf("%-17s", reply->SubDirName);
	gotoxy(40, wherey());
	cprintf( "Creation : ");
	ShowDateAndTime( &reply->CreationDateAndTime );
	printf( "\n" );
}

