code.......
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <stdlib.h>
#include <alloc.h>
//================================================================
const DISK_NUM = 2; //dinh dang cua no;2=USB;0=floppy....
//================================================================
#define READ_ONLY 0x01
#define HIDDEN 0x02
#define SYSTEM 0x04
#define VOLUME_LABEL 0x08
#define SUBDIRECTORY 0x10
#define ARCHIVE 0x20
#define DEVICE 0x40
#define UNUSED 0x80
//=================================================================
typedef struct {
char JumpCode[3];
char OemName[8];
unsigned int BytePerSector;
unsigned char SectorPerCluster;
unsigned int ReservedSectors;
unsigned char FatCopies;
unsigned int RootDirEntries;
unsigned int NumSectors;
unsigned char MediaType;
unsigned int SectorsPerFAT;
unsigned int SectorsPerTrack;
unsigned int NumberOfHeads;
unsigned long HiddenSectors;
unsigned long SectorsBig;
unsigned char PhysicalDriveNumber;
unsigned char Reserved;
char ExtendedBootSignature;
long VolumeID;
char VolumeLabel[11];
char FatTypeName[8];
char OSBootCode[448];
int BootSectorSignature;
}BOOTSECTOR;
//============================================================
typedef struct {
char FileName[8];
char Extension[3];
unsigned char Attribute;
unsigned char Reserved;
unsigned char CreateTime;
unsigned int CreateTime1;
unsigned int CreateDate;
unsigned int LastAccess;
unsigned int EA_Index;
unsigned int LastModTime;
unsigned int LastModDatel;
unsigned int FirstCluster;
unsigned long FileSize;
}DirectoryRoot;
//============================================================
void In_FAT(BOOTSECTOR &b)
{
int *p = (int *)malloc(b.BytePerSector);
if(p==NULL)
{
printf("
Ko du bo nho");
getch();
return ;
}
if(absread(DISK_NUM,1,b.ReservedSectors,p))
{
printf("
ko doc dc ");
getch();
return;
}
printf("
Thong tin 20 o dau tien cua bang FAT
");
for(i=0;i<20;++i)
printf("%d ",p[i]);
printf("
In xong");
free(p);
}
//============================================================
void FreeCluster(BOOTSECTOR &b)
{
int i,j;
unsigned long dem=0;
int *p = (int *)malloc(b.BytePerSector);
if(p==NULL)
{
printf("
Ko du bo nho");
getch();
return ;
}
for(i=0;i<b.SectorsPerFAT;++i)
{
if(absread(DISK_NUM,1,b.ReservedSectors+i,p))
{
printf("
ko doc dc ");
getch();
return;
}
for(j=0;j<b.BytePerSector/2;++j)
if(p[j]==0x0000)
++dem;
}
printf("
So Cluster trong la : %lu
",dem);
free(p);
}
//============================================================
void DefineClusterOfFile(BOOTSECTOR &b,unsigned long n)
{
unsigned long vitri = n;
int *p = (int *)malloc(b.BytePerSector);
if(p==NULL)
{
printf("
Ko du bo nho");
getch();
return ;
}
do
{
if(absread(DISK_NUM,1,b.ReservedSectors+((vitri*2)/b.BytePerSector),p))
{
printf("
ko doc dc ");
getch();
return;
}
printf("
Vi tri : %lu",vitri);
vitri = p[vitri%b.BytePerSector];
}while(vitri>=0x0002 && vitri<=0xFFEF);
free(p);
}
//============================================================
void KhoanMuc(BOOTSECTOR &b)
{
DirectoryRoot *d= (DirectoryRoot*)malloc(b.BytePerSector);
int i,j;
// vi tri cua sector cua thu muc goc:
unsigned int k = (unsigned int)b.ReservedSectors+(unsigned int)b.FatCopies*b.SectorsPerFAT;
if(absread(DISK_NUM,3,k,d))
{
printf("
ko doc dc ");
getch();
return;
}
for(i=0;i<48;++i)
{
if(d[i].FistCluster!=0 || d[i].FileName[0]!=-27)
{
printf("
File %d",i);
for(j=0;j<8;++j)
printf("
Ten File : %c",d[i].FileName[j]);
printf("
DIR = %d",!(d[i]. Attribute&ARCHIVE==ARCHIVE));
printf("
Cluster dau tien %u",d[i].FirstCluster);
printf("
Kich thuoc file %u",d[i].FileSize);
getch();
}
}
}
free(d);
}
//==================================================================
void main()
{
clrscr();
//Read Boot sector
BOOTSECTOR b;
printf("
Size = %d",sizeof(b));
if(absread(DISK_NUM,1,0,&b)!=0)
{
printf("
Disk problemed
");
getch();
}
else
{
printf("
Byte per Sector : %u",b.BytePerSector);
printf("
Sector per Cluster : %u",b.SectorPerCluster);
printf("
ReservedSectors : %u",b.ReservedSectors);
printf("
Fat Copy : %u",b.FatCopies);
printf("
RootDirEntries : %u",b.RootDirEntries);
printf("
Number Sector : %u",b.NumSectors);
printf("
Number of Heads : %x",b.NumberOfHeads);
printf("
Sector Big : %lu",b.SectorsBig);
printf("
Sectors per FAT : %u
",b.SectorsPerFAT);
printf("
PhysicalDriveNumber : %u
",b.PhysicalDriveNumber);
for(int i=0;i<8;++i)
printf("%c",b.OemName[i]);
printf("
Kieu o dia : %x",b.MediaType);
printf("
Volume ID : %x
",b.VolumeID);
for(i=0;i<11;++i)
printf("%c",b.VolumeLabel[i]);
printf("
");
for(i=0;i<8;++i)
printf("%c",b.FatTypeName[i]);
//Chuong trinh chinh
int chose;
unsigned long n;
do{
clrscr();
printf("
==========================Menu======================
1.In ra gia tri 20 o nho dau tien cua bang FAT.
2.In ra total free cluster.
3.In ra cac cluster cua 1 file biet file do bat dau tu cluster thu n.
4.Liet ke khoan muc.
0.Thoat khoi chuong trinh.");
scanf("%d",&chose);
switch(chose)
{
case 0:
{
chose=0;
break;
}
case 1:
{
In_FAT(b);
chose=1;
break;
}
case 2:
{
FreeCluster(b);
chose=1;
break;
}
case 3:
{
pritnf("
Nhap vao vi tri cluster bat dau cua file:");
scanf("%lu",&n);
DefineClusterOfFile(b,n);
chose=1;
break;
}
case 4:
{
KhoanMuc(b);
chose=1;
break;
}
default:
{
printf("
Nhap khong dung.
Chon 1 de thuc hien lai.Chon 0 de thoat")
scanf(chose);
break;
}
}
getch();
}while(chose);
}
}
Bạn đang đọc truyện trên: Truyen247.Pro