return 0; }
if(temp->i_nlink!=0) { //如仍有用户使用该文件,则不能删除 printf(\还有用户共享了该文件,不能删除!\\n\
return 0; }
//删除工作 if(temp->parent==NULL) { //不是第一个孩子
temp->sibling_prev->sibling_next=temp->sibling_next; if(temp->sibling_next)//处理是最后一个兄弟的情况
temp->sibling_next->sibling_prev=temp->sibling_prev;
temp->sibling_prev=temp->sibling_next=NULL; }
else { //第一个孩子 if(temp->sibling_next)//处理是最后一个兄弟的情况
temp->sibling_next->parent=temp->parent;
temp->parent->child=temp->sibling_next; }
free(temp); return 1;
}
//获取当前目录名子函数
int GetDir(int begin,char *path,char *curDir) { int i=0; int len=strlen(path);
while(!((path[begin]=='\\\\')||(path[begin]=='/'))&&begin } //查找路径函数 int FindPath(char *ph) { struct FileNode *temp; //struct FileNode *tp,*temp; char oldpath[INPUT_LEN-COMMAND_LEN]; unsigned int i=0; //int i=0 int sign=1; if(strcmp(ph,\ //ph是根目录 cp=root; strcpy(path,\ return 1; } temp=cp; strcpy(oldpath,path);//保留原路径和指针 17 if(ph[0]=='/') { //指针指向根目录的左孩子 cp=root->child; i++; //滤过'/' strcpy(path,\ } else { if(cp!=NULL&&cp!=root) strcat(path,\ if(cp&&cp->child) { if(cp->isdir) cp=cp->child;//指针指向当前目录的左孩子 else { printf(\路径错误!\\n\ return 0; } } } while(i<=strlen(ph)&&cp) { //继续查找指定路径,如遇到文件则报错 int j=0; if(ph[i]=='/'&&cp->child) { i++; //略过'/' if(cp->isdir) cp=cp->child; //继续查找下级目录 else { printf(\路径错误!\\n\ return 0; } strcat(path,\ } while(ph[i]!='/'&&i<=strlen(ph)) { // curpath 记录当前要找的路径名 curpath[j]=ph[i]; i++; j++; } curpath[j]='\\0'; while((strcmp(cp->filename,curpath)!=0||(cp->isdir!=1))&&cp->sibling_next!=NULL) { cp=cp->sibling_next; } if(strcmp(cp->filename,curpath)==0) { if(cp->isdir==0) { strcpy(path,oldpath); cp=temp; printf(\是文件不是目录.\\n\ return 0; } strcat(path,cp->filename); } 18 if(strcmp(cp->filename,curpath)!=0||cp==NULL) { strcpy(path,oldpath); cp=temp; printf(\输入路径错误\\n\ return 0; } } return 1; } //命令检查函数 int CheckCommand() { if(strlen(Para2)==0) { printf(\命令语法不正确.\\n\ return 0; } return 1; } //分解命令子函数 int ParseCommand() { char Inputs[INPUT_LEN]; int i=0,j=0,ch; unsigned int k=0; printf(\ if(GetInput(Inputs,INPUT_LEN)==-1) { //获取输入 printf(\输入行太长.\\n\ return 0; } Para1[0]=Para2[0]='\\0'; //获取参数Para1,即操作名 while(Inputs[i]!=' '&&Inputs[i]!='\\0'&&i i++; } Para1[i]='\\0'; if(i==(COMMAND_LEN-1)) return 1; //输入命令太长 if(Inputs[i]!='\\0') { //获取参数2,即路径文件名 while(Inputs[i]==' '&&i Para2[j]=Inputs[i]; i++; j++; } Para2[j]='\\0'; } 19 for(k=0;k } return 1; } //执行命令函数 void ExecuteCommand() { int sign; //根据参数Para1调用相应的功能处理模块 if(strcmp(Para1,\ sign=cdComd(); //cd命令 else if(strcmp(Para1,\ sign=editComd(); //edit命令 else if(strcmp(Para1,\ sign=mdComd(); // else if(strcmp(Para1,\ sign=delComd(); //del命令 else if(strcmp(Para1,\ sign=rdComd(); else if(strcmp(Para1,\ sign=dirComd(); //dir命令 else if(strcmp(Para1,\ exit(0); //exit命令 else printf(\命令错误,请重试\\n\ //命令输入不正确,报错 } int main() { Init(); //初始化文件树 while(1) { if(ParseCommand()) //分解命令 ExecuteCommand(); //执行命令 } return 0; } 六.运行结果 1.显示根目录下所有文件和目录 20
相关推荐: