#include <alloc.h>
#include <dos.h>
struct Dir_Item {
  char fname[11];
  unsigned char attr;
  unsigned char reserved[10];
  unsigned int time;
  unsigned int date;
  unsigned int cl; 
  unsigned long size;
  } *dir;
  
 struct RootRec {
  unsigned char jmp[3], ident[8]; unsigned int SectSize; unsigned char ClustSize;
  unsigned int ResSect;  unsigned char FatCnt;
  unsigned int RootSize, TotSecs; unsigned char Media;
  unsigned int FatSize, TrkSecs, HeadCnt, HidnSecL, HidnSecH;
  unsigned long LongTotSecs;  unsigned char Drive;
  unsigned char reserved1;  unsigned char DOS4_flag;
  unsigned long VolNum; char VolLabel[11], FatForm[8];
  } *rt;

 union REGS rr;
 struct SREGS sr;
char *attrs[]={"READ_ONLY","HIDDEN_FILE",
  "SYSTEM_FILE","LABEL","SUBDIRECTORY"};
  
int main() {
	unsigned char *buff;
	unsigned char drive = 'c';
	int i, j, k, m;
	unsigned char ms;   /* маска атрибутов */
	unsigned int RootSect,  /* номер сектора */
    RootCnt,   /* число элементов */
    RootSects;  /* число секторов */

	system("cls");
	buff=(unsigned char *)malloc(512);
	
	rr.h.ah=2;	
	rr.h.dl=0x80;
	rr.h.dh=1;
	rr.x.cx=1; 
	rr.h.al=1;
	sr.ds=FP_SEG(buff); 
	rr.x.bx=FP_OFF(buff);
	int86x(0x13,&rr,&rr,&sr);
	if(rr.x.cflag) {
		printf("\nerror");
		system("pause");
		return(-1);
	}
	
	rt=(struct RootRec *)buff;

	RootCnt=rt->RootSize;
	RootSect=rt->ResSect+rt->FatSize*rt->FatCnt;
	RootSects=rt->RootSize/16;

	buff=(unsigned char *)realloc(buff,RootSects*512);
	if (buff==NULL) { 
		printf("Out of memory\n"); 
		exit(); 
	}
	dir=(struct Dir_Item *)buff;


	rr.h.ah=2;
	rr.h.dl = 0x80;
	rr.x.cx=RootSects; 
	rr.x.dx=RootSect;
	
	sr.ds=FP_SEG(buff); 
	rr.x.bx=FP_OFF(buff);
	
	int86x(0x13,&rr,&rr,&sr);
	if(rr.x.cflag) {
		printf("\nerror");
		system("pause");
		return(-1);
	}
	

	for (j=0, k=0; j<RootCnt; j++) {
		printf("#%3d  ",j);
			if (dir[j].fname[0]!=0) {
				for (i=0; i<11; i++) {
					if (i==8) 
						printf(".");
					if ((i==0)&&(dir[j].fname[i]==0xe5)) 
						printf("?");
					else 
						printf("%c",dir[j].fname[i]);
				}
				if (dir[j].fname[0]==0xE5)
					printf(" ******DELETED****** ");
				printf(" attribute=%02x (",dir[j].attr);
				if (!(dir[j].attr&0x01f)) 
					printf("READ/WRITE");
				else 
					for(ms=1, m=0; m<5; m++,ms<<=1)
						if (dir[j].attr&ms) 
							printf("%s ",attrs[m]);
				printf(")\n   ");
				printf(" time=%02d/%02d/%02d",
					(dir[j].time>>11)&0x001f,
					(dir[j].time>>5)&0x003f,
					(dir[j].time&0x001f)*2);
				printf(" date=%02d/%02d/%04d",
					((dir[j].date>>9)&0x007f)+1980,
					(dir[j].date>>5)&0x000f,
					dir[j].date&0x001f);
				printf(" Beg.cluster=%03x",dir[j].cl);
				printf(" size=%lu\n",dir[j].size);
			} else 
				printf(" free\n");
		if (++k>7) { 
			k=0; 
			if (getch()==27) 
				goto EOJ; 
		}
  }
EOJ:
	free(buff);
	system("pause");
	return 0;
}