#include <io.h>
#include <conio.h> //里面有clrscr()清屏函数
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h> //内存分配函数
#include <string.h>
#include <ctype.h>
#define N 30 //用户数
#define M 20 //一个用户可保存M个文件
#define L 5 //用户只能一次打开L个文件
typedef struct MFD //主文件目录
{
char username[100];
char password[100];
FILE *fp; //文件目录指针
}MFD;
typedef struct UFD //用户文件目录
{
char filename[256];
char protect; //保护码
int length; //文件长度
}UFD;
typedef struct OFD //打开文件目录
{
char filename[256];
char opencode; //打开保护码
int *fp; //读写指针
}OFD;
typedef struct COMM //命令串
{
char string[256]; //用户命令串
struct COMM *next; //后继指针:指向命令各参数所在的结点
}COMM;
MFD mainfd[N]; //主文件目录数组
UFD userfd[M]; //用户文件目录数组
OFD openfd[L]; //打开文件目录数组
COMM *command; //命令串指针
char username[100];
int usernum,savenum,opennum;
int workfile;
void init(); //初始化主文件目录数组
void init_ufd(char *username); //初始化用户文件目录
void mesg(char *str); //输出函数
char *getuser(); //设置用户函数声明
char *getpass(); //设置口令函数声明
COMM *readcommand(); //读命令串函数声明
void login(); //用户登录
void logout(); //用户注销
void setpass(); //设置口令
void create(); //创建用户文件
void mydelete(); //删除
void myread(); //读
void myopen(); //打开
void myclose(); //关闭
void mywrite(); //写
void help(); //帮助
void dir(); //列文件目录
void mycopy(); //复制
void myrename(); //文件改名
void main()
{
init();
while(1)
{
readcommand();
if(strcmp(command->string,"create")==0)
create();
else if(strcmp(command->string,"delete")==0)
mydelete();
else if(strcmp(command->string,"open")==0)
myopen();
else if(strcmp(command->string,"close")==0)
myclose();
else if(strcmp(command->string,"read")==0)
myread();
else if(strcmp(command->string,"write")==0)
mywrite();
else if(strcmp(command->string,"copy")==0)
mycopy();
else if(strcmp(command->string,"rename")==0)
myrename();
else if(strcmp(command->string,"login")==0)
login();
else if(strcmp(command->string,"setpass")==0)
setpass();
else if(strcmp(command->string,"logout")==0)
logout();
else if(strcmp(command->string,"help")==0)
help();
else if(strcmp(command->string,"dir")==0)
dir();
else if(strcmp(command->string,"exit")==0)
break;
else
mesg("Bad command!");
}
}
void mesg(char *str)
{
printf("\n %s\n",str);
}
void init() //初始化主文件目录数组
{
FILE *fp; //文件指针
char tempname[20],temppass[20];
usernum=0; //全局变量初始化
savenum=0;
opennum=0;
strcpy(username,"");
//用户使用时,建立一个mainfile.txt文件,包括每个用户的用户名和口令
//然后,才能运行此程序
if((fp=fopen("mainfile.txt","r"))!=NULL)//以读方式打开文件mainfile.txt
{
while(!feof(fp))//若不是文件尾
{
strcpy(tempname,""); //清空数组操作
fgets(tempname,20,fp); //读用户名
if(strcmp(tempname,"")!=0)
{
fgets(temppass,20,fp);
tempname[strlen(tempname)-1]='\0'; //设置串结束符
temppass[strlen(temppass)-1]='\0';
strcpy(mainfd[usernum].username,tempname); //生成mainfd数组
strcpy(mainfd[usernum].password,temppass); //生成userfd数组
usernum++; //生成usernum的值
}
if(usernum>=N)
break;
}
fclose(fp);
}
}
void init_ufd(char *username) //初始化用户文件目录
{
FILE *fp;
char tempfile[100],tempprot;
int templength;
savenum=0;
opennum=0;
workfile=-1;
if((fp=fopen(username,"r+"))!=NULL)
{
while(!feof(fp))
{
strcpy(tempfile,"");
fgets(tempfile,50,fp);
if(strcmp(tempfile,"")!=0)
{
fscanf(fp,"%c",&tempprot);
fscanf(fp,"%d",&templength);
tempfile[strlen(tempfile)-1]='\0';
strcpy(userfd[savenum].filename,tempfile); //文件名
userfd[savenum].protect=tempprot; //保护码
userfd[savenum].length=templength; //文件长度
savenum++;
fgets(tempfile,50,fp);
}
}
}
}
char *getuser() //设置用户函数声明
{
char username[20];
char temp;
int i;
username[0]='\0';
for(i=0;i<20;)
{
while(!kbhit()); //按用户名规则输入用户名
temp=getch();
if(isalnum(temp)||temp=='_'||temp==(char)13)
{
username[i]=temp;
if(username[i]==(char)13)
{
username[i]='\0';
break;
}
putchar(temp);
i++;
username[i]='\0';
}
}
return username;
}
char *getpass() //设置口令函数声明
{
char password[20];
char temp;
int i;
password[0]='\0';
for(i=0;i<20;)
{
while(!kbhit()); //等待按键
temp=getch();
if(isalnum(temp)||temp=='_'||temp==(char)13)
{
password[i]=temp;
if(password[i]==(char)13)
{
password[i]='\0';
break;
}
putchar('*');
i++;
password[i]='\0';
}
}
return password;
}
COMM *readcommand() //读命令串函数声明
{
char temp[256];
char line[256];
unsigned int i,end;
COMM *newp,*p;
command=NULL;
strcpy(line,"");
while(strcmp(line,"")==0)
{
printf("\nc:\\>");
gets(line); //输入一个命令串
}
for(i=0;i<=strlen(line);i++)
{
if(line[i]==' ')
i++;
end=0;
while(line[i]!='\0'&&line[i]!=' ')
{
temp[end]=line[i];
end++;
i++;
}
if(end>0) //对命令行中的子串进行处理
{
temp[end]='\0';
newp=(COMM*)malloc(sizeof(COMM*));
strcpy(newp->string,temp);
newp->next=NULL;
if(command==NULL)
command=newp; //把各子串链成一个链表
else
{
p=command;
while(p->next!=NULL)
p=p->next;
p->next=newp;
}
}
}
p=command;
return command;
}
void login() //用户注册
{
FILE *fp;
int i;
char password[20],confirm[20],tempname[20];
if(command->next==NULL)
{
printf("\n User Name:");
strcpy(tempname,getuser()); //输入用户名并且返回之
}
else if(command->next->next!=NULL)
{
mesg("Too many parameters!");
return;
}
else strcpy(tempname,command->next->string);
for(i=0;i<usernum;i++)
if(strcmp(mainfd[i].username,tempname)==0)
break; //从mainfd表中查找要注册的用户
if(i>=usernum) //新用户
{
printf("\n new user account,enter your password twice!");
printf("\n Password:");
strcpy(password,getpass()); //输入口令并且返回之
printf("\n Password:");
strcpy(confirm,getpass()); //第二次输入口令
if(strcmp(password,confirm)==0) //两次输入的口令是否相同的处理情况
{
if(usernum>=N) //用户数不能超过N
mesg("Creat new account