📄 final.c
字号:
if(strlen(path)==0)return clus;
if(OpType==1)strcpy(TmpTip,tip);
while(1)
{
if(path[i]!=0)
{
if(path[i]=='\\')
{
name[j]=0;
i++;
if(j==0)return -1;
j=0;
goto deal;
}
else
{
if((path[i]>=97)&&(path[i]<=122))name[j]=path[i]-32;
else name[j]=path[i];
i++;j++;
}
}
else
{name[j]=0;flag=1;goto deal;}
GoOn:;
}
deal:;
if(!strcmp(name,"$:"))
{
if(OpType==1)strcpy(TmpTip,"$:\\");
clus=0;
}
else
{
if(flag&&(OpType&0x10))clus=search(clus,name,OpType);
else clus=search(clus,name,0x22);
if(clus==-1)return -1;
clus=(((dword)FileInfo.HI)<<16)|((dword)FileInfo.LO);
if(OpType==1)
{
if(!strcmp(name,".")){}
else if(!strcmp(name,".."))
{
k=strlen(TmpTip);
if(TmpTip[k-1]=='\\')
k--;
while(1)
{
if(k<3)return -1;
if(TmpTip[k-1]=='\\')
{TmpTip[k]=0;break;}
k--;
}
}
else
{strcat(TmpTip,name);strcat(TmpTip,"\\");}
}
}
if(path[i]!=0)goto GoOn;
if((clus!=-1)&&(OpType==1))strcpy(tip,TmpTip);
return clus;
}
/* 显示提示符 */
void ShowTip()
{
printf("%s>",tip);
}
/* type为0检查文件名,1检查目录名 */
int checkname(byte * name,byte type)
{
int i,DotNum=0,k,DotPos=0;
byte a[3];
i=strlen(name);
if((type==0)&&(i>12))return -1;
else if((type==1)&&(i>8))return -1;
for(k=0;k<i;k++)
{
if((name[k]>='a')&&(name[k]<='z')){name[k]-=32;continue;}
else if((name[k]>='@')&&(name[k]<='Z'))continue;
else if((name[k]>='0')&&(name[k]<='9'))continue;
else if(name[k]=='.'){DotNum++;DotPos=k;continue;}
else if((name[k]>='#')&&(name[k]<=')'))continue;
else if((name[k]=='-')||(name[k]=='_')||(name[k]=='~')||(name[k]=='`')||
(name[k]=='!')||(name[k]=='{')||(name[k]=='}')||(name[k]=='^'))continue;
else if(name[k]>127)continue;
else return -1;
}
if((type==0)&&(DotNum>1))return -1;
else if((type==1)&&(DotNum>0))return -1;
if(type==0)
{
if(DotNum==1)
{
if((i-DotPos-1)>3)return -1;
a[0]=name[DotPos+1];a[1]=name[DotPos+2];a[2]=name[DotPos+3];
while(DotPos<8){name[DotPos]=0x20;DotPos++;}
while(DotPos<11){if(a[DotPos-8]==0)break;else name[DotPos]=a[DotPos-8];DotPos++;}
}
else {if(i>8)return -1;DotPos=i;}
while(DotPos<11){name[DotPos]=0x20;DotPos++;}
}
if(type==1)while(i<11){name[i]=0x20;i++;}
return 1;
}
void dir(const char * path)
{
dword clus;
if(strlen(path)==0)clus=CurrentClus;
else clus=allocal(path,2);
if(clus!=-1) {putchar('\n');search(clus,0,0);}
else {printf("Bad path name!");putchar('\n');}
}
void cd(const char *path)
{
dword clus=0;
if(strlen(path)!=0)
{
clus=allocal(path,1);
if(clus!=-1)CurrentClus=clus;
else printf("Bad path name!\n");
}
}
void BoToBo(dword SecA,dword SecB,int size)
{
read1(buf,SecA,p);
write1(buf,SecB,p);
}
void copy(const char *path1,const char *path2)
{
dword clus1,clus2,FirstSec1,FirstSec2,File1ClusNum,*ClusLink,ClusNum=0;
word offset;
sDir16 TmpDir;
int i,l;
char TmpPath[260],name[255];
clus1=allocal(path1,0x12);
if(clus1==-1){printf("Source file %s dosen't exist!\n",path1);return;}
File1=FileInfo;
if((clus1=allocal(path2,2))!=-1)
{
l=strlen(path1);
while((path1[l-1]!='\\')&&(l!=0))l--;
strcpy(name,path1+l);
strcpy(TmpPath,path2);
}
else
{
l=strlen(path2);
strcpy(TmpPath,path2);
while((TmpPath[l-1]!='\\')&&(l!=0))l--;
if(l!=strlen(path2))
{
strcpy(name,TmpPath+l);
TmpPath[l]=0;
}
if((clus1=allocal(TmpPath,2))==-1){printf("The destinate directoy dosen't exist!\n");return;}
}
if(search(clus1,0,3)==-1)
{printf("Not enough space!\n");return;}
File2=FileInfo;
if(checkname(name,0)==-1){printf("Illegal file name!\n");return;}
File1ClusNum=(File1.FileSize+dbr.BPB_BytsPerSec*dbr.BPB_SecPerClus-1)/(dbr.BPB_BytsPerSec*dbr.BPB_SecPerClus);
ClusLink=(dword*)malloc(sizeof(dword)*(File1ClusNum+1));
clus1=RWFAT(File1ClusNum,0,5,ClusLink);
if(clus1==0){printf("There isn't enough space in the disk!\n");return;}
ClusLink[File1ClusNum]=0xFFFF;
clus1=((dword)File1.LO)|(((dword)File1.HI)<<16);
while(ClusNum<File1ClusNum)
{
if(clus1>0xFFF6){printf("The file is damaged!\n");return;}
clus2=ClusLink[ClusNum++];
FirstSec1=((clus1-2)*dbr.BPB_SecPerClus)+FirstPos.FirstData;
FirstSec2=((clus2-2)*dbr.BPB_SecPerClus)+FirstPos.FirstData;
for(i=0;i<dbr.BPB_SecPerClus;i++)
BoToBo(FirstSec1+i,FirstSec2+i,dbr.BPB_BytsPerSec);
clus1=RWFAT(clus1,0,0,0);
}
offset=(File1.offset%16)*32;
if(File1.ParrentClus==0)
read1(buf,File1.offset/16+FirstPos.FirstRoot,p);
else
read1(buf,File1.offset/16+((File1.DetailClus-2)*dbr.BPB_SecPerClus)+FirstPos.FirstData,p);
TmpDir=*((sDir16*)(buf+offset));
for(i=0;i<11;i++)TmpDir.DIR_Name[i]=name[i];
TmpDir.DIR_FstClusLO=ClusLink[0]&0x0000FFFF;
TmpDir.DIR_FstClusHI=ClusLink[0]&0xFFFF0000;
offset=(File2.offset%16)*32;
if(File2.ParrentClus==0)
read1(buf,File2.offset/16+FirstPos.FirstRoot,p);
else
read1(buf,File2.offset/16+((File2.DetailClus-2)*dbr.BPB_SecPerClus)+FirstPos.FirstData,p);
*((sDir16*)(buf+offset))=TmpDir;
if(File2.ParrentClus==0)
write1(buf,File2.offset/16+FirstPos.FirstRoot,p);
else
write1(buf,File2.offset/16+((File2.DetailClus-2)*dbr.BPB_SecPerClus)+FirstPos.FirstData,p);
if(RWFAT(0,0,6,ClusLink)==0)printf("Error!\n");
}
void del(const char *path)
{
dword clus,sec=0;
word off;
clus=allocal(path,0x12);
if(clus==-1){printf("Wrong name!\n");return;}
if(!RWFAT(clus,0,2,0)){printf("Error!\n");return;}
if(FileInfo.FileNum==1)
{
RWFAT(FileInfo.PreClus,FileInfo.NextClus,1,0);
RWFAT(FileInfo.DetailClus,0,1,0);
}
else
{
off=(FileInfo.offset%16)*32;
if(FileInfo.DetailClus==0)
sec=(FileInfo.offset/16)+FirstPos.FirstRoot;
else
sec=(FileInfo.offset/16)+((FileInfo.DetailClus-2)*dbr.BPB_SecPerClus)+FirstPos.FirstData;
read1(buf,sec,p);
*(buf+off)=0xE5;
write1(buf,sec,p);
}
printf("A file is deleted!\n");
}
void deltree(const char *path)
{
dword clus,sec=0;
word off;
clus=allocal(path,2);
if(clus==-1){printf("The directory dosen't exist!\n");return;}
else if(clus==0){printf("Can't delete root directory!\n");return;}
else if((FileInfo.Dir_Attr&0x10)==0){printf("Error!This isn't a directory!\n");return;}
else if(!search(clus,0,4)){printf("The directory isn't null!\n");return;}
if(!RWFAT(clus,0,2,0)){printf("Error!\n");return;}
clus=allocal(path,2);
if(FileInfo.FileNum==1)
{
RWFAT(FileInfo.PreClus,FileInfo.NextClus,1,0);
RWFAT(FileInfo.DetailClus,0,1,0);
}
else
{
off=(FileInfo.offset%16)*32;
if(FileInfo.DetailClus==0)
sec=(FileInfo.offset/16)+FirstPos.FirstRoot;
else
sec=(FileInfo.offset/16)+((FileInfo.DetailClus-2)*dbr.BPB_SecPerClus)+FirstPos.FirstData;
read1(buf,sec,p);
*(buf+off)=0xE5;
write1(buf,sec,p);
}
printf("A directory is deleted!\n");
}
void md(const char *path)
{
dword ParrentClus,ChildClus;
word offset;
char TmpPath[260];
int k;
char name[255];
if((allocal(path,0x32))!=-1)
{printf("Same name with the exist file!\n");return;}
strcpy(TmpPath,path);
k=strlen(TmpPath);
while((TmpPath[k-1]!='\\')&&(k!=0))k--;
if(k==0){ParrentClus=CurrentClus;strcpy(name,TmpPath);}
else
{
strcpy(name,TmpPath+k);
TmpPath[k]=0;
ParrentClus=allocal(TmpPath,2);
if(ParrentClus==-1){printf("Bad path name!");return;}
}
if(checkname(name,1)==-1){printf("Bad dir name!\n");return;}
ChildClus=RWFAT(0,0,3,0);
if(ChildClus==0){printf("There is not enough space in the disk!\n"); return;}
if(search(ParrentClus,0,3)==-1){printf("Error!\n");return;}
offset=(FileInfo.offset%16)*32;
if(ParrentClus==0)
read1(buf,FileInfo.offset/16+FirstPos.FirstRoot,p);
else
read1(buf,FileInfo.offset/16+((FileInfo.DetailClus-2)*dbr.BPB_SecPerClus)+FirstPos.FirstData,p);
k=0;
while(k<11){((sDir16*)(buf+offset))->DIR_Name[k]=name[k];k++;}
((sDir16*)(buf+offset))->DIR_Attr=0x10;
((sDir16*)(buf+offset))->DIR_NTRes=0x00;
((sDir16*)(buf+offset))->DIR_CrtTimeTenth=0;
((sDir16*)(buf+offset))->DIR_CrtTime=0;
((sDir16*)(buf+offset))->DIR_CrtDate=0;
((sDir16*)(buf+offset))->DIR_LstAccDate=0;
((sDir16*)(buf+offset))->DIR_FstClusHI=(ChildClus&0xFFFF0000)>>16;
((sDir16*)(buf+offset))->DIR_WrtTime=0;
((sDir16*)(buf+offset))->DIR_WrtDate=0;
((sDir16*)(buf+offset))->DIR_FstClusLO=ChildClus&0x0000FFFF;
((sDir16*)(buf+offset))->DIR_FileSize=0;
if(ParrentClus==0)
write1(buf,FileInfo.offset/16+FirstPos.FirstRoot,p);
else
write1(buf,FileInfo.offset/16+((FileInfo.DetailClus-2)*dbr.BPB_SecPerClus)+FirstPos.FirstData,p);
((sDir16*)buf)->DIR_Name[0]='.';
for(k=1;k<11;k++)((sDir16*)buf)->DIR_Name[k]=' ';
((sDir16*)buf)->DIR_Attr=0x10;
((sDir16*)buf)->DIR_NTRes=0x00;
((sDir16*)buf)->DIR_CrtTimeTenth=0;
((sDir16*)buf)->DIR_CrtTime=0;
((sDir16*)buf)->DIR_CrtDate=0;
((sDir16*)buf)->DIR_LstAccDate=0;
((sDir16*)buf)->DIR_FstClusHI=(ChildClus&0xFFFF0000)>>16;
((sDir16*)buf)->DIR_WrtTime=0;
((sDir16*)buf)->DIR_WrtDate=0;
((sDir16*)buf)->DIR_FstClusLO=ChildClus&0x0000FFFF;
((sDir16*)buf)->DIR_FileSize=0;
((sDir16*)(buf+32))->DIR_Name[0]='.';
((sDir16*)(buf+32))->DIR_Name[1]='.';
for(k=2;k<11;k++)((sDir16*)(buf+32))->DIR_Name[k]=' ';
((sDir16*)(buf+32))->DIR_Attr=0x10;
((sDir16*)(buf+32))->DIR_NTRes=0x00;
((sDir16*)(buf+32))->DIR_CrtTimeTenth=0;
((sDir16*)(buf+32))->DIR_CrtTime=0;
((sDir16*)(buf+32))->DIR_CrtDate=0;
((sDir16*)(buf+32))->DIR_LstAccDate=0;
((sDir16*)(buf+32))->DIR_FstClusHI=(ParrentClus&0xFFFF0000)>>16;
((sDir16*)(buf+32))->DIR_WrtTime=0;
((sDir16*)(buf+32))->DIR_WrtDate=0;
((sDir16*)(buf+32))->DIR_FstClusLO=ParrentClus&0x0000FFFF;
((sDir16*)(buf+32))->DIR_FileSize=0;
for(k=64;k<512;k++)buf[k]=0;
write1(buf,((ChildClus-2)*dbr.BPB_SecPerClus)+FirstPos.FirstData,p);
for(k=0;k<64;k++)buf[k]=0;
for(k=1;k<dbr.BPB_SecPerClus;k++)
write1(buf,((ChildClus-2)*dbr.BPB_SecPerClus)+FirstPos.FirstData+k,p);
}
void creat_bin()
{
dword clus,FirstSec,FileSize=0;
word offset;
int k,i=0;
char *name="ERROR BIN";
if(allocal("$:\\error.bin",0x12)==-1)
{
if(search(0,0,3)==-1){printf("Too many file in the root directory!\n");return;}
if((clus=RWFAT(0,0,3,0))==-1){printf("Not enough space!\n");return;};
offset=(FileInfo.offset%16)*32;
read1(buf,FileInfo.offset/16+FirstPos.FirstRoot,p);
k=0;
while(name[k]!=0)
{
((sDir16*)(buf+offset))->DIR_Name[k]=name[k];
k++;
}
((sDir16*)(buf+offset))->DIR_Attr=0x00;
((sDir16*)(buf+offset))->DIR_NTRes=0x00;
((sDir16*)(buf+offset))->DIR_CrtTimeTenth=0;
((sDir16*)(buf+offset))->DIR_CrtTime=0;
((sDir16*)(buf+offset))->DIR_CrtDate=0;
((sDir16*)(buf+offset))->DIR_LstAccDate=0;
((sDir16*)(buf+offset))->DIR_FstClusHI=(clus&0xFFFF0000)>>16;
((sDir16*)(buf+offset))->DIR_WrtTime=0;
((sDir16*)(buf+offset))->DIR_WrtDate=0;
((sDir16*)(buf+offset))->DIR_FstClusLO=clus&0x0000FFFF;
((sDir16*)(buf+offset))->DIR_FileSize=0;
write1(buf,FileInfo.offset/16+FirstPos.FirstRoot,p);
}
allocal("$:\\error.bin",0x12);
FileInfo.ParrentClus=((dword)FileInfo.LO)|(((dword)FileInfo.HI)<<16);
printf("End with (ctrl+D):\n");
k=0;
FirstSec=((FileInfo.ParrentClus-2)*dbr.BPB_SecPerClus)+FirstPos.FirstData;
while(1)
{
if(k==512)
{
FileSize+=512;
write1(buf,FirstSec+i,p);
if(i==dbr.BPB_SecPerClus-1)
{
clus=RWFAT(FileInfo.ParrentClus,0,0,0);
if(clus>0xFFF7)
{
if((clus=RWFAT(0,0,3,0))==0){printf("Not enough space!\n");break;}
RWFAT(FileInfo.ParrentClus,clus,1,0);
}
FileInfo.ParrentClus=clus;
FirstSec=((FileInfo.ParrentClus-2)*dbr.BPB_SecPerClus)+FirstPos.FirstData;
i=0;
}
else i++;
k=0;
}
buf[k]=getch();
if(buf[k]==4)break;
putchar(buf[k]);
k++;
}
if(k<512)
{
FileSize+=k;
write1(buf,FirstSec+i,p);
}
read1(buf,FileInfo.offset/16+FirstPos.FirstRoot,p);
offset=(FileInfo.offset%16)*32;
((sDir16*)(buf+offset))->DIR_FileSize=FileSize;
write1(buf,FileInfo.offset/16+FirstPos.FirstRoot,p);
putchar('\n');
}
void main()
{
char com1[9][10]={{"dir"},{"cd"},{"md"},{"creat_bin"},{"del"},
{"copy"},{"deltree"},{"free"},{"exit"}};
char com2[4][260],ch[260];
int i,j,k;
p=fopen("mp3.bin","rb+");
if(p==NULL){printf("Open file error!\n");getch();exit(0);}
com2[0][0]=com2[1][0]=com2[2][0]=0;
if(initialize()==-1){printf("File system not support OR bad disk!\n");return;}
ShowTip();
do
{
gets(ch);
if(strlen(ch)==0){ShowTip();continue;}
i=j=k=0;
while((ch[k]==' ')&&(ch[k]!='\0'))k++;
while(ch[k]!='\0')
{
if(ch[k]==' ')
{com2[i][j]=0;i++;j=0;while((ch[k]==' ')&&(ch[k]!='\0'))k++;}
else
{com2[i][j]=ch[k];j++;k++;}
}
com2[i][j]=0;
if(!strcmp(com2[0],com1[0]))
dir(com2[1]);
else if(!strcmp(com2[0],com1[1]))
cd(com2[1]);
else if(!strcmp(com2[0],com1[2]))
md(com2[1]);
else if(!strcmp(com2[0],com1[3]))
creat_bin();
else if(!strcmp(com2[0],com1[4]))
del(com2[1]);
else if(!strcmp(com2[0],com1[5]))
copy(com2[1],com2[2]);
else if(!strcmp(com2[0],com1[6]))
deltree(com2[1]);
else if(!strcmp(com2[0],com1[7]))
RWFAT(0,0,4,0);
else if(!strcmp(com2[0],com1[8]))
break;
else
printf("Error\n");
com2[0][0]=com2[1][0]=com2[2][0]=0;
ShowTip();
}while(1);
fclose(p);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -