/************************************************************************
*   ﾃﾞｨﾚｸﾄﾘ移動ﾌﾟﾛｸﾞﾗﾑ
*   使用法 : AD
*************************************************************************
*/
#include <direct.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dos.h>
#include "vramput.h"
void far int24h(void);

#define ERR     ( -1 )                  /* ｴﾗｰ 時の ﾘﾀｰﾝ･ｺｰﾄﾞ   */
#define NOERR   0
#define PATHLEN     ( 3 + 64 )         /* ﾊﾟｽ の長さ ( "d:\" + ﾊﾟｽ )   */

union   REGS inregs,outregs;
struct  SREGS segregs;

char    dirname[768][PATHLEN];
int     dircount=0;

int main(int,char **);
void srch_dir( char *);

int attr = _A_SUBDIR;                  /* 検索属性     */

int main( argc, argv )
int  argc;
char *argv[];
{
	static char rootdir[] = "?:\\";
	char schdir[ PATHLEN ];            /* 検索ﾃﾞｨﾚｸﾄﾘ          */
	int ch;

	_harderr( int24h );                /* int 24h ﾊﾝﾄﾞﾗ の設定     */

	title();
	printv("**** 少々お待ち下さい。****",47,27,12);

	for (ch = 'A'; ch <= 'P'; ch++ ) {
	    rootdir[ 0 ] = ( char )ch;
	    strcpy( schdir, rootdir );
		srch_dir( schdir );           /* ﾃﾞｨﾚｸﾄﾘ ﾃｰﾌﾞﾙ作成 */
	}
	change_dir();                     /* ﾃﾞｨﾚｸﾄﾘ変更 */
}

void srch_dir(register char schdir[])
{
	struct find_t dta;
    char schpath[ PATHLEN ];

	strcpy( schpath, schdir );         /* 検索ﾃﾞｨﾚｸﾄﾘ ｾｯﾄ */
    strcat( schpath, "*.*" );
	if ( _dos_findfirst( schpath, _A_SUBDIR, &dta ) == 0 ) {
	if ( strlen(schdir) == 3 ){
		strcpy(dirname[dircount++],schdir);
	}
	do {
		if ( dta.attrib & _A_SUBDIR ) {
		if ( dta.name[ 0 ] == '.' )
			continue;
		strcpy( schpath, schdir );
		strcat( schpath, dta.name );
		strcpy(dirname[dircount++],schpath );
		strcat( schpath, "\\" );
		srch_dir( schpath );
		}
	} while ( _dos_findnext( &dta ) == 0 );
    }
}

void far int24h()
{
	_hardresume( _HARDERR_FAIL );
}

dsp(int gyou,int iro,char *moji)
{
	printv(moji,iro,0,gyou+1);
}
title()
{
	cls(1);
	printv("--ﾃﾞｨﾚｸﾄﾘ変更 (AD)  V1.0 -- Copyright(c) 1991 H/NAKAMURA -----------------------",15,0,0);
	printv("-- \x1E/\x1Fで選択 ｽﾍﾟｰｽで内容表示 ﾘﾀ-ﾝで決定 ESCで中止 ------------------------------",15,0,23);
}
change_dir()
{
	int keyin,i,top=0,act=0,bot=0,acto=0,actd=0;
	char buf[80];

	color(7);
	title();
	csr(0);

	dircount--;
	bot = dircount>21 ? 21:dircount;

	for(i=0;i<=bot;i++){
		dsp(i,7,dirname[top+i]);
	}
	dsp(0,42,dirname[top]);
	act=0;acto=-1;

	for(;;){
		if(acto != act && acto > -1)
			dsp(acto,7,dirname[actd]);

		acto = act;actd = top+act;
		dsp(act,42,dirname[top+act]);

		keyin = getch();

		switch(keyin){
			case 0x1F: if ((act < 21) && top+act < dircount){
							 acto=act;actd=top+act;act++;break;
						 }
						 else if ((act == 21) && (top+act < dircount)){
							 dsp(act,7,dirname[top+act]);
							 acto=21;actd=top+act;top++;upscrol();break;
						 }
						 else if (top+act == dircount){
							 putch('\x07');
							 dsp(act,42,dirname[dircount]);
							 break;
						 }
						 else break;
			case 0x1E:   if (act > 0 && top+act < dircount){
							 acto=act;actd=top+act;act--;break;
						 }
						 else if (act > 0 && top+act == dircount){
							 dsp(act,7,dirname[dircount]);acto=act;actd=top+act;act--;break;
						 }
						 else if (act == 0  && top > 0){
							 dsp(act,7,dirname[top+act]);
							 acto=0;actd=top+act;top--;dnscrol();break;
						 }
						 else putch('\x07');break;
			case 0x20: back_color(0);color(7);cls(1);locate(1,1);sprintf(buf,"DIR %s /P",dirname[top+act]);system(buf);
					   color(1);puts("どれかキーを押して下さい");getch();
					   title();
					   for(i=0;i<=bot;i++){
						   dsp(i,7,dirname[top+i]);
					   }
					   acto=-1;
					   break;
			case 0x0D: move_dir(dirname[top+act]);back_color(0);color(7);cls(1);csr(1);exit(0);
			case 0x1B: back_color(0);color(7);cls(1);csr(1);exit(-1);
			default  : break;
		}
	}
}

upscrol()
{
	inregs.h.ah = 0x1A;
	inregs.h.al = 0x00;
	inregs.x.cx = 1;
	inregs.h.dh = 2;
	inregs.h.dl = 1;
	inregs.h.bh = 23;
	inregs.h.bl = 80;
	int86(0x91,&inregs,&outregs);
	locate(1,23);printf("\x1B[K");
}

dnscrol()
{
	inregs.h.ah = 0x1A;
	inregs.h.al = 0x01;
	inregs.x.cx = 1;
	inregs.h.dh = 2;
	inregs.h.dl = 1;
	inregs.h.bh = 23;
	inregs.h.bl = 80;
	int86(0x91,&inregs,&outregs);
	locate(1,2);printf("\x1B[K");
}

int move_dir( register char dir[] )        /* changeﾃﾞｨﾚｸﾄﾘ */
{
	unsigned dstdrv;
	unsigned num;
	if(chdir( dir ) != 0) printf("%s err",dir);
	dstdrv = toupper( dir[ 0 ] ) - 'A' + 1;
	_dos_setdrive( dstdrv, &num );
}

