作業系統檔案系統C語言實現的部分函式(一)
阿新 • • 發佈:2019-02-13
void startsys() { int i; FILE *fp; char str[9]; myvhard = (unsigned char *)malloc(SIZE*sizeof(char)); /*申請 1M空間*/ if((fp=fopen("myfsys.dat","rb")) == NULL) { printf("myfsys檔案系統不存在,現在開始建立檔案系統\n"); my_format(); //fclose(fp); if((fp = fopen("myfsys.dat","w"))== NULL) { printf("open error!\n"); exit(1); } strcpy(str,"10101010"); fwrite(str,sizeof( char ),8,fp); fwrite(myvhard,SIZE,1,fp); fclose(fp); } else { fread(str, sizeof( char ), 8, fp ); str[8] = '\0'; if(strcmp(str,"10101010") == 0) { //將上述緩衝區中的內容複製到記憶體中的虛擬磁碟空間中 fread(myvhard,SIZE,1,fp); } else { printf("myfsys檔案系統不存在,現在開始建立檔案系統\n"); my_format(); fclose(fp); if((fp=fopen("myfsys.dat","w"))== NULL) { printf("open error!\n"); exit(1); } strcpy(str,"10101010"); fwrite(str,sizeof( char ),8,fp); fwrite(myvhard,SIZE,1,fp); } fclose(fp); } fat_f = (struct FAT *)(myvhard + BLOCKSIZE); /*找到FAT表地址*/ root = (struct FCB *)(myvhard + BLOCKSIZE * 5);/*找到根目錄地址*/ strcpy(currentdir,"Root:"); lengthdir = 2; for(i = 0; i < MAXOPENFILE; i++) { strcpy(openfilelist[i].filename,""); openfilelist[i].topenfile = -1; openfilelist[i].dirno = 5; } struct FCB *cur_mkdir = root; strcpy(openfilelist[0].filename,cur_mkdir[0].filename); openfilelist[0].attribute = 0; openfilelist[0].data =0; openfilelist[0].time = 0; openfilelist[0].nextdirectory = 0; openfilelist[0].nextfile = -1; openfilelist[0].length = 0; openfilelist[0].diroff = -1; openfilelist[0].dirno = 5; openfilelist[0].topenfile = -2; fd = 0; }
//格式化 void my_format() { int i; block0 block; strcpy(block.information,"10101010 磁碟塊大小:1024000 磁碟塊數量: 1024 最多開啟檔案數:10\n"); block.root = 5; block.startblock = (unsigned char *)(myvhard + BLOCKSIZE * 6); fat_f = (struct FAT*)(myvhard + BLOCKSIZE); for(i = 0; i < 6; i++) fat_f[i].id = END; for(; i < 1000 ;i++) fat_f[i].id = FREE; lengthdir = 0; root = (struct FCB *)(myvhard + BLOCKSIZE * 5); strcpy(root[0].filename,"."); root[0].attribute = 0; root[0].data = 0; root[0].time = 12; root[0].nextdirectory = -1; root[0].nextfile = 1; root[0].length = 0; root[0].first = 0; lengthdir++; strcpy(root[1].filename,".."); root[1].attribute = 0; root[1].data = 0; root[1].time = 12; root[1].nextdirectory = -1; root[1].nextfile = -1; root[1].length = 0; root[1].first = 0; lengthdir++; for(i = 2 ; i < MAXFCB ; i++) { root[i].attribute = -1; root[i].nextdirectory = -1; root[i].nextfile = -1; } }
int my_cd(char *dirname) { int i,j; struct FCB *cur_mkdir = root; for(j = fd ; j >= 0 ; j--) if(openfilelist[j].attribute == 0) break; char dir = openfilelist[j].nextdirectory; if(dir == -1) return -1; //no file frontdir int flag = 0; /* for(int i = 0; i < MAXOPENFILE; i++) { if(!strcmp(openfilelist[i].filename,dirname)) return -2; // } */ while(cur_mkdir[dir].nextfile != -1) { if(!strcmp(cur_mkdir[dir].filename,dirname) && cur_mkdir[dir].attribute == 0) { flag = 1; break; } dir = cur_mkdir[dir].nextfile; } if(!strcmp(cur_mkdir[dir].filename,dirname)) flag = 1; if(flag == 0) //not find return -1; for(i = 0; i < MAXOPENFILE; i++) { if(openfilelist[i].topenfile == -1) break; } if(i == MAXOPENFILE) return -3; // openlist is full strcpy(openfilelist[i].filename,dirname); openfilelist[i].attribute = cur_mkdir[dir].attribute; openfilelist[i].data = cur_mkdir[dir].data; openfilelist[i].time = cur_mkdir[dir].time; openfilelist[i].nextdirectory = cur_mkdir[dir].nextdirectory; openfilelist[i].nextfile = cur_mkdir[dir].nextfile; openfilelist[i].length = cur_mkdir[dir].length; openfilelist[i].dirno = 5; openfilelist[i].topenfile = dir; openfilelist[i].fcbstate = 0; for(j = i-1 ;j >0 ; j--) if(openfilelist[i].topenfile != -1 && openfilelist[i].attribute == 0) break; openfilelist[i].diroff = j; fd = i; strcat(currentdir,"\\"); strcat(currentdir,dirname); return 1; }