近段时间刚做操作系统实验三,解析FAT12文件系统,输出其目录树。
我们可以通过读取一个软盘镜像文件,即img文件。
FAT12文件系统一般是由启动扇区,2个FAT,根目录区,数据区组成。
C代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Boot//启动扇区 属性
{
unsigned short int BytePerSec;
unsigned char SecPerClus;
unsigned short int ReservedSec;
unsigned char FatCopy;
unsigned short int SizeofRootDirectory;
unsigned short int SizeofSec;
unsigned short int SecPerFAT;
unsigned short int SecPerTrack;
};
struct Entry//根目录条目 属性
{
unsigned char FileName[8];
unsigned char Extension[3];
unsigned char Attribute;
unsigned short int FirstCluster;
unsigned int FileSize;
};
//读取boot
void getBoot(struct Boot* boot,FILE* file);
//读取file,得到根目录描述符以及实际的描述符数目
int getEntry(struct Boot boot,struct Entry* entry,FILE* file);
//开始输出目录数,若描述符是文件,输出其文件名及后缀名,若是文件夹,则通过查找fat中数据读取DATA区的目录数,递归
void outputTree(unsigned short int* fat,unsigned short int first,FILE *file,struct Boot boot);
void getBoot(struct Boot* boot,FILE* file)
{
fseek(file,11,0);//定位file内部指向指针,使其跳过前11个byte
fread(&(boot->BytePerSec),2,1,file);
fread(&(boot->SecP