📄 main.lst
字号:
705 6 return 1;
706 6 }
707 5 }
708 4 }
709 3 }
710 2 }
711 1 return return_value;
712 1 }
713
714 //找FAT表中的空登记项
715 //返回值:1 成功(有空闲磁盘空间);2 失败(磁盘满)
716 //clustor_chain:
717 /*
718 FAT explanation
719
720 0x0000 referenced clustor is currently unused
721 0xfff0-0xfff6 reserved cluster
722 0xfff7 bad track
723 0xffff last clustor of a file
724 0xnnnn relative number of next clustor for a file
725 */
726 bit SerchFreeFAT(unsigned int *clustor_chain,unsigned int data_len,unsigned int start_sector)
727 {
728 1 bit return_value=0;
C51 COMPILER V7.07 MAIN 12/01/2005 22:22:50 PAGE 13
729 1 unsigned int i,j,clustor_pos=0;
730 1 unsigned int fat16_value=0;
731 1 clustor_pos=0;
732 1 for(j=0; j<bootinfo.fat_size+1-start_sector; j++)
733 1 {
734 2 ReadOneSec(j+start_sector);
735 2 for(i=0;i<512;i=i+2)
736 2 {
737 3 // fat16_value=data_buf[i]*256+data_buf[i+1];
738 3 fat16_value=data_buf[i+1]*256+data_buf[i];
739 3 if(fat16_value==0x0000)
740 3 {
741 4 // clustor_chain[clustor_pos]=i+j*512;
742 4 clustor_chain[clustor_pos]=(i+j*512)/2;
743 4 clustor_pos++;
744 4 if(clustor_pos>=data_len)
745 4 {
746 5 return 1;
747 5 }
748 4 }
749 3 }
750 2 }
751 1 return 0;
752 1 }
753
754 // function: like dos command "md"
755 // input:directory name is 8 bytes
756 // return: 1 success;-1 FAT table full;-2 FDT table full;-3 write FDT table Error
757 char CFMakeDirectory(char * name,unsigned int start_clustor)
758 {
759 1 xdata struct sRootDir dir;
760 1 bit success=0;
761 1 static unsigned int sector;
762 1 unsigned int i;
763 1 static unsigned int fdt_pos,record_pos;
764 1 for(i=1; i<=bootinfo.fat_size; i++)
765 1 {
766 2 success=SerchFreeFAT(§or,1,1); //find free fat table
767 2 if(success==1)
768 2 {
769 3 #ifdef DEBUG
printf("\ni=%x,sector=%x\n",i,sector);
#endif
772 3 break;
773 3 }
774 2 }
775 1 if(success==0)
776 1 {
777 2 return -1; //Fat table full
778 2 }
779 1 if(SerchFreeFDT(&fdt_pos,&record_pos,0)==0) //find free fdt table
780 1 return -2; //FDT table full
781 1 if(start_clustor!=0)
782 1 {
783 2 fdt_pos=start_clustor;
784 2 record_pos=0;
785 2 }
786 1 #ifdef DEBUG
printf("fdt pos=%x,record pos=%x\n",fdt_pos,record_pos);
#endif
789 1 if(ReadOneSec(fdt_pos)==0)
790 1 return -3;
C51 COMPILER V7.07 MAIN 12/01/2005 22:22:50 PAGE 14
791 1 for(i=0;i<32;i++)
792 1 {
793 2 ((unsigned char *)(&dir))[i]=0;
794 2 }
795 1 for(i=0;i<8;i++)
796 1 {
797 2 dir.file_name[i]=name[i];
798 2 }
799 1 for(i=0;i<3;i++)
800 1 {
801 2 dir.exten_name[i]=' ';
802 2 }
803 1 dir.file_attribute=0x10;
804 1 dir.first_clust[0]=(sector)%256;
805 1 dir.first_clust[1]=(sector)/256;
806 1 for(i=0;i<32;i++)
807 1 {
808 2 data_buf[record_pos*32+i]=((unsigned char*)(&dir))[i];
809 2 }
810 1 if(WriteOneSec(data_buf,fdt_pos)==0)
811 1 {
812 2 return -3; //write fdt error
813 2 }
814 1 if(ReadOneSec((sector*2)/512+1)==0) //read one sector of Fat table
815 1 return -3;
816 1 for(i=(sector*2)%512;i<(sector*2)%512+2;i++)
817 1 {
818 2 data_buf[i]=0xff;
819 2 }
820 1 if(WriteOneSec(data_buf,(sector*2)/512+1)==0)
821 1 {
822 2 return -3; //write fdt error
823 2 }
824 1 for(i=0;i<32;i++)
825 1 {
826 2 ((unsigned char *)(&dir))[i]=0;
827 2 }
828 1 for(i=0;i<11;i++)
829 1 {
830 2 ((unsigned char*)(&dir))[i]=' ';
831 2 }
832 1 dir.file_name[0]='.';
833 1 dir.file_attribute=0x10;
834 1 dir.first_clust[0]=(sector)%256;
835 1 dir.first_clust[1]=(sector)/256;
836 1 for(i=0;i<512;i++)
837 1 {
838 2 data_buf[i]=0;
839 2 }
840 1 for(i=0;i<32;i++)
841 1 {
842 2 data_buf[i]=((unsigned char*)(&dir))[i];
843 2 }
844 1 dir.file_name[1]='.';
845 1 dir.first_clust[0]=0;
846 1 dir.first_clust[1]=0;
847 1 for(i=0;i<32;i++)
848 1 {
849 2 data_buf[32+i]=((unsigned char*)(&dir))[i];
850 2 }
851 1 if(WriteOneSec(data_buf,(sector-2)*bootinfo.sec_per_clust
852 1 +(1+bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512))==0)
C51 COMPILER V7.07 MAIN 12/01/2005 22:22:50 PAGE 15
853 1 {
854 2 return -3;
855 2 }
856 1 return 1;
857 1 }
858
859
860 char CFMakeSubDirectory(char *sub_dir_name,char *parent_dir_name)
861 {
862 1 static xdata unsigned int parent_dir_sector;
863 1 static xdata unsigned int fdt_pos,record_pos;
864 1 static xdata unsigned int sector;
865 1 xdata unsigned int temp_sec;
866 1 xdata unsigned int i;
867 1 xdata struct sRootDir dir;
868 1 xdata unsigned int un_use;
869 1 if(SerchFreeFAT(§or,1,1)==0) //find free clustor
870 1 {
871 2 return -1; //disk full
872 2 }
873 1 #ifdef DEBUG
printf("\n free sec=%x\n",sector);
#endif
876 1 parent_dir_sector=CFFindFileInRoot(parent_dir_name,&un_use,&un_use);
877 1 if(parent_dir_sector==0xffff) //not find that root directory
878 1 {
879 2 return -1;
880 2 }
881 1 if(SerchFreeFDT(&fdt_pos,&record_pos,parent_dir_sector)==0) //find free record space in directory
882 1 {
883 2 return -1;
884 2 }
885 1 #ifdef DEBUG
printf("\nmd sub: record_pos=%x,fdt_pos=%x,p_dir_sector=%x\n",record_pos,fdt_pos,parent_dir_sector);
#endif
888 1 if(ReadOneSec(fdt_pos)==0)
889 1 {
890 2 return -1;
891 2 }
892 1 for(i=0;i<13;i++)
893 1 {
894 2 ((unsigned char*)(&dir))[i]=sub_dir_name[i];
895 2 }
896 1 dir.file_attribute = DIR;
897 1 dir.file_size[0] = 0;
898 1 dir.file_size[1] = 0;
899 1 dir.file_size[2] = 0;
900 1 dir.file_size[3] = 0;
901 1 dir.first_clust[0]=(sector)%256;
902 1 dir.first_clust[1]=(sector)/256;
903 1 for(i=0;i<32;i++)
904 1 {
905 2 data_buf[record_pos*32+i]=((unsigned char*)(&dir))[i];
906 2 }
907 1 if(WriteOneSec(data_buf,fdt_pos)==0)
908 1 {
909 2 return -1;
910 2 }
911 1 if(ReadOneSec((sector*2)/512+1)==0) //read one sector of Fat table
912 1 {
913 2 return -1;
914 2 }
C51 COMPILER V7.07 MAIN 12/01/2005 22:22:50 PAGE 16
915 1 for(i=(sector*2)%512;i<(sector*2)%512+2;i++)
916 1 {
917 2 #ifdef DEBUG
printf("\ni=%x\n",i);
#endif
920 2 data_buf[i]=0xff;
921 2 }
922 1 if(WriteOneSec(data_buf,(sector*2)/512+1)==0)
923 1 {
924 2 return -1; //write fat table error
925 2 }
926 1 //---------------write in sub directory------------------------------
927 1 if(ReadOneSec((sector*2)/512+1)==0) //read one sector of Fat table
928 1 {
929 2 return -1;
930 2 }
931 1 for(i=(sector*2)%512;i<(sector*2)%512+2;i++)
932 1 {
933 2 data_buf[i]=0xff;
934 2 }
935 1 if(WriteOneSec(data_buf,(sector*2)/512+1)==0)
936 1 {
937 2 return -1; //write fdt error
938 2 }
939 1 for(i=0;i<32;i++)
940 1 {
941 2 ((unsigned char *)(&dir))[i]=0;
942 2 }
943 1 for(i=0;i<11;i++)
944 1 {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -