BAI6_4.CPP

 /*-----------------------------------------------------------------
  Name: TREE COMMAND SIMULATOR ON DRIVE A (REMOVABLE DRIVE)
  Description:The code is for creating tree directory printing...
  Date: 9-12-2002
  By: Pham Quang Hai
  Email: phquhai@yahoo.com

  Comliped on turboc 3.0
  Warnings     :   0
  Errors       :   0

  Side Effects: Will make you completely understand
  This code is copyrighted and has limited warranties.
  Please see http://www.Planet-Source-Code.com
http://www.k7online.vze.com

  Pham Quang Hai ,HCM City. Vietnam
  TOPIC  TREE COMMAND SIMULATOR ON DRIVE A (REMOVABLE DRIVE).
  The Basic Functions of tree();

  THE AUTHOR IS NOT RESPONSIBLE FOR ANY DAMAGE OR PLEASURE OR FUSS IN
  YOUR HEAD OR PC. THIS CODE IS FOR EDUCATIONAL PURPOSE ONLY AND WILL
  VOID WARRENTIES IF USED ILLEGELY OR ABUSED IN ANY FORM...



I tried level best to give effect like "tree" command in windows os. but now
I give up for this decoration.
uncomment the stuff and try it out...

-----------------------------------------------------------------*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <alloc.h>
#include <dos.h>

typedef struct EntryDir
{
char Name[8];
char Ext[3];
unsigned char Att;
unsigned char Reserved[10];
unsigned int Date;
unsigned int Time;
unsigned int FirstFatEntry;
unsigned long Size;
};

typedef struct DirStruct
{
int level;
char Name[9];
int next;
};

void TREE();
int CountSubDir(int FirstEntryDir,int level);
int GetValueFat(int k);

struct EntryDir *TableDir;
unsigned char *FAT;
struct DirStruct *Dir;

/*==========================================================================*/

int n = 0;
char *view[] = {"   ","³  ","ÃÄÄ","ÀÄÄ"};

void main()
{
clrscr();

TableDir = (struct EntryDir*)calloc(224,32);
if (TableDir == NULL) return;

FAT = (unsigned char*)malloc(9*512);
if (FAT == NULL) return;

Dir = (struct DirStruct*)malloc(224+(16*2848));
if (Dir == NULL) return;

if (absread(0,14,19,TableDir) != 0) return;
if (absread(0,9,1,FAT) != 0) return;

TREE();

free(TableDir);
free(FAT);
free(Dir);

getch();
}

/*==========================================================================*/

void TREE()
{
int i, j, k, DirTotal = 0, d = 0, flag = 1;

for (i = 0; i < 224; i++)
{
if (((TableDir[i].Att & 0x10) >> 4) && TableDir[i].Name[0] != 0 &&
TableDir[i].Name[0] != char(0xe5) && TableDir[i].Att != 0xf)
{
Dir[n].level = 0;
for (j = 0; j < 8; j++)
Dir[n].Name[j] = TableDir[i].Name[j];
Dir[n].Name[8] = '\0';
n++;
DirTotal += CountSubDir(TableDir[i].FirstFatEntry,d);
DirTotal++;
}
}

printf("[\\]\n");

for (i = 0; i < DirTotal - 1; i++)
{
Dir[i].next = 0;
for (j = i + 1; j < DirTotal; j++)
{
if (Dir[i].level == Dir[j].level)
{
Dir[i].next = 1;
break;
}
if (Dir[j].level < Dir[i].level)
break;
}
}
Dir[i].next = 0;

for (i = 0; i < DirTotal; i++)
{
if (Dir[i].level == 0 && Dir[i].next == 0)
flag = 0;
if (Dir[i].level > 0 && flag)
printf(view[1]);
else
printf(view[0]);
if (Dir[i].level > 0)
for (j = 0; j < Dir[i].level; j++)
{
k = i;
while (Dir[k].level > j + 1)
k--;
if (Dir[k].next)
printf(view[1]);
else
printf(view[0]);
}
gotoxy(wherex()-3,wherey());
if (Dir[i].next)
printf(view[2]);
else
printf(view[3]);
printf("%s\n",Dir[i].Name);
delay(500);
}

printf("\nTotal directories in drive A: %d\n",DirTotal);
}

/*==========================================================================*/

int CountSubDir(int FirstEntryDir,int level)
{
int i, j, Value, count = 0;
struct EntryDir *BlockEntryDir;
BlockEntryDir = (struct EntryDir*)calloc(16,32);

if (BlockEntryDir == NULL)
return -1;

if (absread(0,1,FirstEntryDir+31,BlockEntryDir)!=0)
return -1;

level++;
for (i = 0; i < 16; i++)
if ((BlockEntryDir[i].Att & 0x10) >> 4 && BlockEntryDir[i].Name[0] != 0 &&
BlockEntryDir[i].Name[0] != char(0xe5) && BlockEntryDir[i].Att != 0xf &&
BlockEntryDir[i].Name[0] != 0x2e)
{
for (j = 0; j < 8; j++)
Dir[n].Name[j] = BlockEntryDir[i].Name[j];
Dir[n].Name[8] = '\0';
Dir[n].level = level;
n++;

count += CountSubDir(BlockEntryDir[i].FirstFatEntry,level);
count++;
}

level--;
Value = GetValueFat(FirstEntryDir);
if (Value >=2 && Value <= 0x1EF)
{
count += CountSubDir(Value,level);
level--;
}

free(BlockEntryDir);

return count;
}

/*==========================================================================*/

int GetValueFat(int k)
{
   int i=(k*3)/2;
   int hibyte = FAT[i+1];
   int lobyte = FAT[i];
   if (k % 2 == 0)
return (lobyte | ((hibyte & 0xF) << 8));
   else
return ((hibyte<<4) | (lobyte >> 4));
}

Project Homepage: