#include"string.h"
#include"stdio.h"
#include"stdlib.h"
#include"math.h"
#include"conio.h"
#define getnode(type)(type*)malloc(sizeof(type))
char sign;/*命令提示符*/
char title[256];/*当前用户名*/
int num=255;
int symbol=0;
int show=0;
char str[256];
struct line{
char text[81];
int num;/*行号*/
struct line *next;/*指向下一个输入项目的指针*/
struct line *prior;/*指向前一项目的指针*/
};/*定义文件编辑行的结构属性*/
struct line *start;/*指向表中第一项目的指针*/
struct line *last;/*指向表中最后一个项目的指针*/
struct line *begin;/*指向缓冲区第一项目的指针*/
struct line *end;/*指向缓冲区最后一个项目的指针*/
struct line *find(),*dls_store();
struct node{
char name[256];/*节点名称*/
char code[256];/*若节点为用户注册目录,保存相应的用户密码*/
int attribute;/*attribute为1时表示节点为目录,为0表示节点为文件*/
struct node *parent,*firstchild,*nextsibling;/*定义双亲节点,孩子节点,兄弟节点*/
struct line *start,*last;
}*p;/*定义节点的结构属性*/
typedef struct node NODE;
NODE *root,*g,*su,*user1,*user2,*user3,*user4,*user5,*user6,*user7,*user8,*user9,*user10;
main()
{
p=getnode(NODE);
initialization();
entry();
while(symbol==0){clrscr();entry();}
clrscr();
printf("[%s]%c",title,sign);
while(num!=0)
{
gets(str);
operation();
switch(num){
case 1:pwd();printf("\n[%s]%c",title,sign);num=255;break;
case 2:ls();printf("\n[%s]%c",title,sign);num=255;break;
case 3:mkdir();printf("\n[%s]%c",title,sign);num=255;break;
case 4:cd();printf("\n[%s]%c",title,sign);num=255;break;
case 5:vi();printf("\n[%s]%c",title,sign);num=255;break;
case 6:rm();printf("\n[%s]%c",title,sign);num=255;break;
case 7:rmdir();printf("\n[%s]%c",title,sign);num=255;break;
case 8:help();printf("\n[%s]%c",title,sign);num=255;break;
case 9:cat();printf("\n[%s]%c",title,sign);num=255;break;
case 10:change();printf("\n[%s]%c",title,sign);num=255;break;
case 11:copy();printf("\n[%s]%c",title,sign);num=255;break;
case 12:paste();printf("\n[%s]%c",title,sign);num=255;break;
case 13:chpw();printf("\n[%s]%c",title,sign);num=255;break;
default:printf("\ncommand not found\n[%s]%c",title,sign);
}
}
}
initialization()/*初始化根目录和用户目录*/
{
root=getnode(NODE);
strcpy(root->name,"root");
root->attribute=1;
root->parent=NULL;
user10=getnode(NODE);
strcpy(user10->name,"stud10");
strcpy(user10->code,"10stud");
user10->attribute=1;
user10->parent=root;
user10->firstchild=NULL;
user10->nextsibling=NULL;
user9=getnode(NODE);
strcpy(user9->name,"stud09");
strcpy(user9->code,"09stud");
user9->attribute=1;
user9->parent=root;
user9->firstchild=NULL;
user9->nextsibling=user10;
user8=getnode(NODE);
strcpy(user8->name,"stud08");
strcpy(user8->code,"08stud");
user8->attribute=1;
user8->parent=root;
user8->firstchild=NULL;
user8->nextsibling=user9;
user7=getnode(NODE);
strcpy(user7->name,"stud07");
strcpy(user7->code,"07stud");
user7->attribute=1;
user7->parent=root;
user7->firstchild=NULL;
user7->nextsibling=user8;
user6=getnode(NODE);
strcpy(user6->name,"stud06");
strcpy(user6->code,"06stud");
user6->attribute=1;
user6->parent=root;
user6->firstchild=NULL;
user6->nextsibling=user7;
user5=getnode(NODE);
strcpy(user5->name,"stud05");
strcpy(user5->code,"05stud");
user5->attribute=1;
user5->parent=root;
user5->firstchild=NULL;
user5->nextsibling=user6;
user4=getnode(NODE);
strcpy(user4->name,"stud04");
strcpy(user4->code,"04stud");
user4->attribute=1;
user4->parent=root;
user4->firstchild=NULL;
user4->nextsibling=user5;
user3=getnode(NODE);
strcpy(user3->name,"stud03");
strcpy(user3->code,"03stud");
user3->attribute=1;
user3->parent=root;
user3->firstchild=NULL;
user3->nextsibling=user4;
user2=getnode(NODE);
strcpy(user2->name,"stud02");
strcpy(user2->code,"02stud");
user2->attribute=1;
user2->parent=root;
user2->firstchild=NULL;
user2->nextsibling=user3;
user1=getnode(NODE);
strcpy(user1->name,"stud01");
strcpy(user1->code,"01stud");
user1->attribute=1;
user1->parent=root;
user1->firstchild=NULL;
user1->nextsibling=user2;
su=getnode(NODE);
strcpy(su->name,"su");
strcpy(su->code,"su");
su->attribute=1;
su->parent=root;
su->firstchild=NULL;
su->nextsibling=user1;
root->firstchild=su;
root->nextsibling=NULL;
}
entry()/*系统登录界面,验证用户及密码*/
{
int h;
char username[256],password[256];
h=0;
g=getnode(NODE);
g=su;
printf("\n\n");
printf("\t\t********FILE MANAGE SYSTEM********\n");
printf("\n\n\n\n\n\n\n");
printf("\t\tuser:");
gets(username);
printf("\n");
printf("\t\tpassword:");
gets(password);
while(g!=NULL)
{
if(strcmp(username,g->name)==0)
{
h=1;
if(strcmp(password,g->code)==0)
{
symbol=1;
printf("\n\t\tSucceed in logging on!Press any key and enter......");
p=g;
if(strcmp(username,"su")==0) sign='#';
else sign='$';
strcpy(title,username);
}
else printf("\n\t\tIncorrect password!");
break;
}
else g=g->nextsibling;
}
if(h==0) printf("\n\t\tIncorrect username!");
getch();
}
change()/*切换用户*/
{
NODE *u;
char password[256];
printf("\npassword:");
gets(password);
u=root->firstchild;
while(strcmp(str,u->name)!=0)u=u->nextsibling;
if(strcmp(password,u->code)!=0) printf("\nIncorrect password!");
else{
p=u;
g=u;
strcpy(title,str);
if(strcmp(str,"su")==0) sign='#';
else sign='$';
begin=NULL;end=NULL;
show=0;
}
}
operation()/*验证输入的命令参数*/
{
NODE *u;
u=root->firstchild;
if(strcmp(str,"exit")==0) num=0;
else if(strcmp(str,"pwd")==0) num=1;
else if(strcmp(str,"ls")==0) num=2;
else if(strcmp(str,"mkdir")==0) num=3;
else if(strcmp(str,"cd")==0) num=4;
else if(strcmp(str,"vi")==0) num=5;
else if(strcmp(str,"rm")==0) num=6;
else if(strcmp(str,"rmdir")==0) num=7;
else if(strcmp(str,"help")==0) num=8;
else if(strcmp(str,"cat")==0) num=9;
else if(strcmp(str,"cp")==0) num=11;
else if(strcmp(str,"paste")==0) num=12;
else if(strcmp(str,"chpw")==0) num=13;
else {
while(u!=NULL&&strcmp(str,u->name)!=0)u=u->nextsibling;
if(u!=NULL)num=10;
}
}
pwd()/*显示当前的工作路径*/
{
NODE *t,*L,*k;
L=getnode(NODE);
L->firstchild=NULL;
t=p;
do
{
k=getnode(NODE);
strcpy(k->name,p->name);
k->firstchild=L->firstchild;
L->firstchild=k;
p=p->parent;
}while(p!=NULL);
for(k=L->firstchild;k!=NULL;k=k->firstchild)
{
printf("/%s",k->name);
}
printf("\n");
p=t;
}
mkdir()/*创建子目录*/
{
if(p!=root)
{
NODE *q,*t,*w;
t=p;
w=p;
q=getnode(NODE);
printf("\ndirectory name:");
scanf("%s",q->name);
q->attribute=1;
w=w->firstchild;
while((strcmp(w->name,q->name)!=0||w->attribute!=1)&&w!=NULL)
{
w=w->nextsibling;
}
if(w!=NULL) printf("\ndirectory already exist");
else
{
q->parent=p;
q->firstchild=NULL;
q->nextsibling=p->firstchild;
p->firstchild=q;
}
p=t;
gets(str);
}
else printf("\nDon't allow this operation in root directory!");
}
ls()/*显示当前目录下的所有内容,后缀为/的表示目录,*的表示文件*/
{
NODE *t;
t=p;
p=p->firstchild;
printf("\n");
while(p!=NULL)
{
if(p->attribute==1) printf("%s/\t",p->name);
else printf("%s*\t",p->name);
p=p->nextsibling;
}
p=t;
}
cd()/*改变当前的工作目录*/
{
NODE *t;
int flag=1;
printf("\ndirectory name or symbol:");
gets(str);
if(strcmp(str,"..")==0)/*返回当前目录的上一级目录*/
{
if(p->parent!=NULL) p=p->parent;
else printf("\nThat is already root directory!");
}
else if(strcmp(str,"")==0)/*返回用户注册子目录*/
{
p=g;
}
else if(strcmp(str,"/")==0)/*返回根目录*/
{
while(p->parent!=NULL) p=p->parent;
}
else if(strcmp(str,".")==0){}/*保持当前目录不变*/
else/*当前目录改变为指定的子目录*/
{
if(p!=root||sign!='$'){
t=p;
p=p->firstchild;