• prwposrtbons
    了解作者
  • Visual C++
    开发工具
  • 3KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 0
    下载次数
  • 2018-12-31 18:57
    上传日期
磁盘文件系统的模拟,能实现文件的添加,删除,实现多级目录
dryja6.rar
  • W17_184955_yvn3IFS.C
    15.5KB
内容介绍
#define BlockSize 16 #define BlockNum 1000 #define MaxBlockNumForFile 16 #define MaxFileLength ((BlockSize)*(MaxBlockNumForFile)) #define MaxUserNum 8 #define MaxFileNumForUser 16 #define MaxOpenFileNum 4 #include<stdio.h> #include<string.h> /*struct define*/ typedef struct { char UserName[16]; char UserPwd[16]; int UserGroup; int FlagInUse; }UserInfo; typedef struct { char text[BlockSize]; }BlockStruct; typedef struct { char FileName[16]; int FileAttrib; int FileLength; int FileBlock[MaxBlockNumForFile]; int FlagInUse; }FileCtrlBlock; typedef struct { FileCtrlBlock FCB[MaxFileNumForUser]; }UserFileDir; typedef struct { char UserName[16]; int UFDAddr; }MainFileDir; typedef struct { char FileName[16]; int FileAttrib; int FileLength; int FileState; /*0:create,1:open*/ int ReadPointer; int WritePointer; int FlagInUse; }UserOpenFile; /*initialize*/ UserInfo USER[MaxUserNum]; MainFileDir MFD[MaxUserNum]; UserFileDir UFD[MaxUserNum]; BlockStruct BLOCK[BlockNum]; int BlockIsUse[BlockNum]; UserOpenFile UOF[MaxUserNum][MaxOpenFileNum]; char ActiveUser[16]; char Domain[16]={"localhost"}; char Directory[16]; int mfdindex,ufdindex,fileindex; /*function define*/ void cls(void) { int i; for (i=0;i<24 ;i++ ) { printf("\n"); } } int AddUser(char uname[16],char upwd[16],int ugroup) { int i; for (i=0;i<MaxUserNum;i++) { if (USER[i].FlagInUse&&!strcmp(USER[i].UserName,uname)) { printf("User %s has already exist.",uname); return -1; } } for (i=0;i<MaxUserNum ;i++ ) { if (USER[i].FlagInUse) { continue; } strcpy(USER[i].UserName,uname); strcpy(USER[i].UserPwd,upwd); USER[i].UserGroup=ugroup; USER[i].FlagInUse=1; strcpy(MFD[i].UserName,uname); MFD[i].UFDAddr=i; return 0; } printf("\nUsers\' number has reached the max value."); return -1; } int DelUser(char uname[16]) { int i,j; if (!strcmp(uname,USER[0].UserName)) { printf("\nuser \'%s\' must in the system.",USER[0].UserName); return -1; } if (!strcmp(uname,ActiveUser)) { printf("\nYou can not delete yourself."); return -1; } for (i=0;i<MaxUserNum;i++) { if (USER[i].FlagInUse&&!(strcmp(USER[i].UserName,uname))) { for (j=0;j<MaxFileNumForUser;j++) { if (UFD[ufdindex].FCB[j].FlagInUse) { printf("User has file in the system."); return -1; } } USER[i].FlagInUse=0; return 0; } } printf("user \'%s\' not exist.",uname); return -1; } void shell(void) { char sign='#'; if (strcmp(ActiveUser,"root")) { sign='$'; } printf("\n[%s@%s %s]%c ",ActiveUser,Domain,ActiveUser,sign); } int Login(char uname[16],char upwd[16]) { int i; for (i=0;i<MaxUserNum;i++) { if (!USER[i].FlagInUse||strcmp(USER[i].UserName,uname)) { continue; } else if (strcmp(USER[i].UserPwd,upwd)) { printf("Password incorrect."); return -1; } else return 0; } printf("User %s not exist.",uname); return -1; } int Login_fun(void) { char uname[16],upwd[16]; int i=0,key=0; fflush(stdin); printf("login:"); scanf("%s",uname); printf("Password:"); while (key!=13&&i<15) { if (kbhit()) { key=getch(); upwd[i++]=key; } } upwd[i-1]='\0'; if (!Login(uname,upwd)) { strcpy(ActiveUser,uname); strcpy(Directory,uname); for (i=0;i<MaxFileNumForUser;i++) { if (USER[i].FlagInUse&&!strcmp(MFD[i].UserName,ActiveUser)) { mfdindex=i; break; } } ufdindex=MFD[mfdindex].UFDAddr; return 0; } else{ printf("Login incorrect.\n"); if (Login_fun()) return -1; else return 0; } } int GeneralInitialize(void) { printf("\n%s ",Domain); AddUser("root","root",1); return 0; } int SeekFreeBlock(void) { static int i=0; int j; for (;i<BlockNum;i++) { if (!BlockIsUse[i]) { BlockIsUse[i]=1; return i; } } for (j=0;j<BlockNum;j++) { if (!BlockIsUse[j]) { BlockIsUse[j]=1; return j; } } return -1; } int Create(char fname[16],int flength,int fattrib) { int i,j=0,k,flag,uofindex,alocblock[MaxBlockNumForFile]; if (flength>=MaxFileLength) { printf("File Length too long."); return -1; } if (fattrib!=0&&fattrib!=1) { printf("File Attribute Error."); return -1; } for (i=0;i<MaxFileNumForUser;i++) { if (UFD[ufdindex].FCB[i].FlagInUse&&!strcmp(UFD[ufdindex].FCB[i].FileName,fname)) { printf("Filename has already been used."); return -1; } } for (i=0;i<MaxOpenFileNum;i++) { if (!UOF[mfdindex][i].FlagInUse) { break; } } if (i==MaxOpenFileNum) { printf("Can not open more files."); return -1; } uofindex=i; for (i=0;i<MaxFileNumForUser;i++) { if (!UFD[ufdindex].FCB[i].FlagInUse) { break; } } if (i==MaxFileNumForUser) { printf("Can not create more files."); return -1; } fileindex=i; k=flength; do { if (-1==(i=SeekFreeBlock())) { printf("Not enough free space."); return -1; } alocblock[j++]=i; k-=BlockSize; } while(k>0); for (i=0;i<MaxBlockNumForFile;i++) { if (i<j) { UFD[ufdindex].FCB[fileindex].FileBlock[i]=alocblock[i]; } else { UFD[ufdindex].FCB[fileindex].FileBlock[i]=-1; } } UOF[mfdindex][uofindex].ReadPointer=0; UOF[mfdindex][uofindex].WritePointer=0; strcpy(UFD[ufdindex].FCB[fileindex].FileName,fname); UFD[ufdindex].FCB[fileindex].FileAttrib=fattrib; UFD[ufdindex].FCB[fileindex].FileLength=flength; UFD[ufdindex].FCB[fileindex].FlagInUse=1; strcpy(UOF[mfdindex][uofindex].FileName,fname); UOF[mfdindex][uofindex].FileAttrib=fattrib; UOF[mfdindex][uofindex].FileLength=flength; UOF[mfdindex][uofindex].FileState=0; UOF[mfdindex][uofindex].FlagInUse=1; return 0; } int Open(char fname[16],int opreate) { int i,flag=0; for (i=0;i<MaxFileNumForUser;i++) { if (UFD[ufdindex].FCB[i].FlagInUse&&!strcmp(UFD[ufdindex].FCB[i].FileName,fname)) { fileindex=i; flag=1; break; } } if (!flag) { printf("File %s not exist.",fname); return -1; } flag=0; for (i=0;i<MaxOpenFileNum;i++) { if (UOF[mfdindex][i].FlagInUse&&!strcmp(UOF[mfdindex][i].FileName,fname)) { flag=1; break; } } if (flag) { if (UOF[mfdindex][i].FileState==0) { printf("File under creating."); } else { printf("File has already been opened."); } return -1; } else { if (UFD[ufdindex].FCB[fileindex].FileAttrib<opreate) { printf("File is ReadOnly."); return -1; } for (i=0;i<MaxOpenFileNum;i++) { if (UOF[mfdindex][i].FlagInUse==0) { break; } } if (i==MaxOpenFileNum) { printf("Can not open more files."); return -1; } strcpy(UOF[mfdindex][i].FileName,UFD[ufdindex].FCB[fileindex].FileName); UOF[mfdindex][i].FileAttrib=opreate; UOF[mfdindex][i].FileLength=UFD[ufdindex].FCB[fileindex].FileLength; UOF[mfdindex][i].FileState=1; UOF[mfdindex][i].FlagInUse=1; UOF[mfdindex][i].ReadPointer=0; UOF[mfdindex][i].WritePointer=0; return 0; } } int Write(char fname[16],int wrpoint) { int i,j,k,wrlong,b,e; char str[MaxFileLength]; for (i=0;i<MaxOpenFileNum;i++) { if (UOF[mfdindex][i].FlagInUse&&!strcmp(UOF[mfdindex][i].FileName,fname)) { break; } } if (MaxOpenFileNum==i) { printf("File was not been created nor opened."); return -1; } fileindex=i; if (UOF[mfdindex][fileindex].FileState&&!UOF[mfdindex][fileindex].FileAttrib) { printf("The file is ReadOnly."); return -1; } if (-1==wrpoint) { wrpoint=UOF[mfdindex][fileindex].WritePointer; } gets(str); wrlong=strlen(str); if (wrlong+wrpoint>=UOF[mfdindex][fileindex].FileLength) { printf("Write too long."); return -1; } i=0;k=0; for (i=wrpoint/BlockS
评论
    相关推荐