#include "nw.h"

void PrintDirList(struct scanDirReq *request, struct scanDirRep *reply, int level);

void PrintDirList(struct scanDirReq *request, struct scanDirRep *reply, int level)
{
	static int line=0;
	WORD result, i;
	struct scanDirReq req;
	struct scanDirRep rep;

	/* Initialize Request & Reply structures */
	request->Length = sizeof( struct scanDirReq ) - 2;
	request->Function = 0X02;             /* Function F - Get file info */

	reply->Length = sizeof( struct scanDirRep ) - 2;

	/* Now cycle through calls to GetFileInfo until no more files are found */
	while (1) {
	   result = GetDirectoryInfo( request, reply );
	   if (result == 0x98) {
	      printf( "Volume does not exist"
		      "use Volume:Directory\\Directory\\File\n");
	      exit(1);
	   }
	   if (result == 0x9B) {
	      printf( "Bad directory handle\n");
	      exit(1);
	   }
	   if (result == 0x9C) {
	      return;
	   }

	   for(i=0; i<level; ++i) {
	      printf("   ");
	   }
	   PrintDirectoryInfo(reply);
	   if(++line>=23) {
	      printf("\t\t\t\tPress any key to continue");
	      if(getch()!=' ')
		 line=20;
	      else
		 line=0;
	      puts("");
	   }
	   memcpy(&req, request, sizeof(struct scanDirReq));
	   memcpy(&rep, reply, sizeof(struct scanDirRep));

	   {
	      WORD i;
	      i=(reply->SubDirNumber<<8)+(reply->SubDirNumber>>8)+1;
	      request->SubDirNumber = (i>>8)+(i<<8);
	   }

	   req.DirPath[req.DirPathLength-2]= (BYTE)'\0';
	   strcat((char*)req.DirPath, "\\");
	   strcat((char*)req.DirPath, (char*)rep.SubDirName);
	   strcat((char*)req.DirPath, "\\*");
	   req.DirPathLength = strlen((char*)req.DirPath);
	   req.DirHandle =  0;	     /* No handle, use full path from user */
	   req.SubDirNumber = 0;   /* Use 0 for first call	*/
	   PrintDirList(&req, &rep, level+1);
	}

}


main ( int argc, char *argv[] ) {

	struct scanDirReq request;
	struct scanDirRep reply;

	clrscr();
	if ( argc != 2 ) {

	       printf( "Usage: DIRINFO path(Volume:subdir\...\DirName)\n");
	       exit(1);
	}
	/* Initialize Request & Reply structures */
	request.Length = sizeof( request ) - 2;
	request.Function = 0X02;             /* Function F - Get file info */
	request.DirHandle =  0;	     /* No handle, use full path from user */
	request.SubDirNumber  =  0;   /* Use 0 for first call	*/
	request.DirPathLength = strlen( argv[1] );
	movmem( argv[1], request.DirPath, request.DirPathLength );
	request.DirPath[request.DirPathLength]='\0';
	strcat((char *)request.DirPath, "\\*");
	request.DirPathLength += 2;

	reply.Length = sizeof( reply ) - 2;

	PrintDirList(&request, &reply, 0);
}


