www.pudn.com > FMS.rar > FMS.cpp


// FMS.cpp : Defines the entry point for the console application. 
// 
 
#include "stdafx.h" 
#include "io.h" 
#include "conio.h" 
#include "stdio.h" 
#include "stdlib.h" 
#include "malloc.h" 
#include "string.h" 
#include "ctype.h" 
#define M 20/*一个用户可保存M个文件*/ 
#define N 30/*用户数*/ 
#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 lenght;/*文件长度*/ 
}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[10]; 
int usernum=0,savenum=0,opennum=0; 
int workfile=0; 
void init(); 
void init_ufd(char *username);/*初始化用户文件目录*/ 
void mesg(char *str); 
char *getpass();/*设置口令*/ 
char *getuser();/*设置用户名*/ 
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();/*文件改名*/ 
int main(int argc, char* argv[]) 
{ 
	init(); 
	for(;;) 
	{ 
		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"); 
	} 
	return 0; 
} 
void init() 
{ 
	FILE *fp; 
	char tempname[20],tempass[20]; 
	int i=0; 
	usernum=0; 
	savenum=0; 
	opennum=0; 
	strcpy(username,""); 
	/*用户使用时,建立一个mainfile.txt文件,包括每个用户的用户名和口令*/ 
	/*然后才能运行此程序*/ 
	if((fp=fopen("mainfile.txt","r"))!=NULL) 
	{ 
		while(!feof(fp)) 
		{ 
			strcpy(tempname,""); 
			fgets(tempname,20,fp); 
			if(strcmp(tempname,"")!=0) 
			{ 
				fgets(tempass,20,fp); 
				tempname[strlen(tempname)-1]='\0'; 
				tempass[strlen(tempass)-1]='\0'; 
				strcpy(mainfd[usernum].username,tempname); 
				strcpy(mainfd[usernum].password,tempass); 
				usernum++; 
			} 
		} 
		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,"w+"))!=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].lenght=templength; 
				savenum++; 
				fgets(tempfile,50,fp); 
			} 
		} 
	} 
	fclose(fp); 
} 
void mesg(char *str) 
{ 
	printf("\n   %s\n",str); 
} 
char *getpass()/*设置密码*/ 
{ 
	char password[20]; 
	char temp; 
	int i=0; 
	password[0]='\0'; 
	for(;i<10;) 
	{ 
		while(!kbhit()); 
		temp=getch(); 
		if(isalnum(temp)||temp=='_'||temp==13) 
		{ 
			password[i]=temp; 
			if(password[i]==13) 
			{ 
				password[i]='\0'; 
				break; 
			} 
			putchar('*'); 
			i++; 
			password[i]='\0'; 
		} 
	} 
	return (password); 
} 
char *getuser()/*设置用户名*/ 
{ 
	char username[20]; 
	char temp; 
	int i=0; 
	username[0]='\0'; 
	for(i=0;i<10;) 
	{ 
		while(!kbhit()); 
		temp=getch(); 
		if(isalnum(temp)||temp=='_'||temp==13) 
		{ 
			username[i]=temp; 
			if(username[i]==13) 
			{ 
				username[i]='\0'; 
				break; 
			} 
			putchar(temp); 
			i++; 
			username[i]='\0'; 
		} 
	} 
	return (username); 
} 
COMM *readcommand()/*读命令串*/ 
{ 
	char temp[256]; 
	char line[256]; 
	int i=0,end=0; 
	COMM *newp,*p; 
	command=NULL; 
	strcpy(line,""); 
	while(strcmp(line,"")==0) 
	{ 
		printf("\nc:\\>"); 
		gets(line); 
	} 
	for(i=0;i0) 
		{ 
			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=0; 
	char password[20],confirm[20],tempname[20]; 
	COMM *p=command; 
	while(p!=NULL) 
	{ 
		printf("%s\n",p->string); 
		p=p->next; 
	} 
	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); 
//	printf("%s%d\n",tempname,usernum); 
	for(i=0;i=usernum) 
	{ 
		printf("\n new user account,enter your password twice!"); 
		printf("\n Password:"); 
		strcpy(password,getpass()); 
		printf("\n confirm Password:"); 
		strcpy(confirm,getpass()); 
		if(strcmp(password,confirm)==0) 
		{ 
			if(usernum>=N) 
				mesg("Create new account false! number of user account limited.\n  login false!"); 
			else 
			{ 
				strcpy(mainfd[usernum].username,tempname);//把新用户和口令填如mainfd中 
				strcpy(mainfd[usernum].password,password); 
				usernum++; 
				strcpy(username,tempname); 
				mesg("Create a new user! \n login suc!cess"); 
				init_ufd(username); 
				fp=fopen("mainfile.txt","w+");//将新用户添加到mainfile.txt文件中 
				for(int i=0;inext!=NULL) 
		mesg("Too many parameters!"); 
	else if(strcmp(username,"")==0) 
		mesg("No user login!"); 
	else 
	{ 
		strcpy(username,""); 
		opennum=0; 
		savenum=0; 
		workfile=-1; 
		mesg("User login!"); 
	} 
} 
void setpass()/*修改口令*/ 
{ 
	int i=0; 
	FILE *fp; 
	char oldpass[20],newpass[20],confirm[20]; 
	if(strcmp(username,"")==0) 
		mesg("No user login!"); 
	else 
	{ 
		printf("\n   Old    password:"); 
		strcpy(oldpass,getpass()); 
		for(int i=0;inext==NULL) 
		mesg("Too few parametets!"); 
	else if(command->next->next!=NULL) 
		mesg("Too many parameters!"); 
	else 
	{ 
		for(i=0;inext->string); 
			if(strcmp(userfd[i].filename,command->next->string)==0) 
				break; 
		} 
		if(i=M) 
			mesg("Error!  connot create file! number of files limited!"); 
		else 
		{ 
			strcpy(userfd[savenum].filename,command->next->string); 
			userfd[i].protect='r'; 
			userfd[i].lenght=rand(); 
			savenum++; 
			mesg("Create file success!"); 
			fp=fopen(username,"w+"); 
			for(i=0;inext==NULL) 
		mesg("Too few parametets!"); 
	else if(command->next->next!=NULL) 
		mesg("Too many parameters!"); 
	else 
	{ 
		for(i=0;inext->string); 
			if(strcmp(userfd[i].filename,command->next->string)==0) 
				break; 
		} 
		if(i>=savenum) 
			mesg("Error!   the file not existed!"); 
		else 
		{ 
			tempsave=i; 
			for(i=0;inext->string,openfd[i].filename)==0) 
					break; 
			} 
			if(i>=opennum) 
				mesg("File not open"); 
			//////////////////////////////////////////// 
			else 
			{ 
				if(tempsave==savenum-1) 
					savenum--; 
				else 
				{ 
					for(;tempsavenext==NULL) 
		mesg("Too few parametets!"); 
	else if(command->next->next!=NULL) 
		mesg("Too many parameters!"); 
	else 
	{ 
		strcpy(tempfile,command->next->string); 
		for(i=0;i=savenum) 
			mesg("File not existed!"); 
		else 
		{ 
			tempsave=i; 
			for(i=0;i=opennum) 
				mesg("File not opened"); 
			else 
			{ 
				if(userfd[tempsave].lenght<1000) 
					printf("\n The file size is %d KB",userfd[tempsave].lenght); 
				else if(userfd[tempsave].lenght==1000) 
					printf("\n The file size is 1000 KB"); 
				else 
					printf("\n The file size is %d,%d KB",userfd[tempsave].lenght/1000,userfd[tempsave].lenght%1000); 
				mesg("File read"); 
			} 
		} 
	} 
} 
void myopen()/*打开*/ 
{ 
	int i=0; 
	int type=0; 
	char tempcode; 
	char tempfile[100]; 
	if(strcmp(username,"")==0) 
		mesg("No user login!"); 
	else if(command->next==NULL) 
		mesg("Too few parameters!"); 
	else 
	{ 
		strcpy(tempfile,""); 
		tempcode='r'; 
		if(strcmp(command->next->string,"/r")==0) 
		{ 
			tempcode='r'; 
			type=1; 
		} 
		else if(strcmp(command->next->string,"/w")==0) 
		{ 
			tempcode='w'; 
			type=1; 
		} 
		else if(strcmp(command->next->string,"/d")==0) 
		{ 
			tempcode='d'; 
			type=1; 
		} 
		else if(command->next->string[0]=='/') 
			mesg("Error! /r/w/d request!"); 
		else if(command->next->next!=NULL) 
			mesg("Too many parameters!"); 
		else 
			strcpy(tempfile,command->next->string); 
		///////////////////////////////////////////////////// 
 
		////////////////////////////////////////////////////// 
		if(type==1) 
		{ 
			if(command->next->next!=NULL) 
			{ 
				if(command->next->next->next!=NULL) 
					mesg("Too many parameters!"); 
				else 
					strcpy(tempfile,command->next->next->string); 
			} 
			else 
				mesg("Too few parameters!"); 
		} 
		if(strcmp(tempfile,"")) 
			{ 
				for(i=0;i=savenum) 
					mesg("File not existed!"); 
				else 
				{ 
					for(i=0;i=L) 
					{ 
						mesg("Error! connot open file! number of opened files limited!"); 
					} 
					else 
					{ 
						strcpy(openfd[opennum].filename,tempfile); 
						openfd[opennum].opencode=tempcode; 
						workfile=opennum; 
						opennum++; 
						mesg("File open sucess!"); 
					} 
				} 
			} 
	} 
 
} 
void myclose()/*关闭*/ 
{ 
		int i=0; 
	int tempsave=0; 
	char tempfile[100]; 
		if(strcmp(username,"")==0) 
		mesg("No user lgoin!"); 
	else if(command->next==NULL) 
		mesg("Too few parametets!"); 
	else if(command->next->next!=NULL) 
		mesg("Too many parameters!"); 
	else 
	{ 
		strcpy(tempfile,command->next->string); 
		for(i=0;i=savenum) 
			mesg("File not existed!"); 
		else 
		{ 
			for(i=0;i=opennum) 
				mesg("File not opened"); 
			else 
			{ 
				if(i==opennum-1) 
					opennum--; 
				else 
				{ 
					for(;inext==NULL) 
		mesg("Too few parametets!"); 
	else if(command->next->next!=NULL) 
		mesg("Too many parameters!"); 
	else 
	{ 
		strcpy(tempfile,command->next->string); 
		for(i=0;i=savenum) 
			mesg("File not existed!"); 
		else 
		{ 
			tempsave=i; 
			for(i=0;i=opennum) 
				mesg("File not opened"); 
			else 
			{ 
				mesg("File write"); 
				int tt=rand(); 
				if(userfd[tempsave].lenght<=1000) 
					printf("\n The file size  from %d KB to %d KB",userfd[tempsave].lenght,userfd[tempsave].lenght+tt); 
			/*	else if(userfd[tempsave].lenght=1000) 
					printf("\n The file size from 1000 KB");*/ 
				else 
					printf("\n The file size form %d,%d KB to %d,%d KB",userfd[tempsave].lenght/1000,userfd[tempsave].lenght%1000,userfd[tempsave].lenght+tt/1000,userfd[tempsave].lenght+tt%1000); 
				userfd[tempsave].lenght=userfd[tempsave].lenght+tt; 
			} 
		} 
	} 
} 
void help()/*帮助*/ 
{ 
	static char *cmd[]={"login","setpass","logout","create", 
		               "open","read","write","delete","dir", 
					   "help","exit","copy","rename"}; 
	static char *cmdhlp[]={"aommand format: login []", 
		                   "command format:setpass", 
						   "command format:logout", 
						   "command format:create ", 
						   "command format:open [/r/w/d] ", 
						   "command format:read ", 
						   "command format:write ", 
						   "command format:delete ", 
						   "command format:dir [/u/o/f]", 
						   "command format:help []", 
						   "command format:exit", 
						   "command format:copy  ", 
						   "command format:rename  "}; 
	static char *detail[]={"explain:user login in the file system.", 
		                   "explain:modify the user password.", 
						   "explain:user logout the file system.", 
						   "explain:creat a new file.", 
						   "explain:/r -- read only [deflaut]\n\t   /w -- list opened files \n\t  /d --read,modify and delete.", 
						   "explain:read the file.", 
						   "explain:modify the file.", 
						   "explain:delete the file.", 
						   "explain:/u -- list the user account\n\t  /0 -- list opened files\n\t  /f --list user file[deflaut].", 
						   "explain: -- list the command detail format and explain.\n\t  [deflaut] list the command.", 
						   "explain:exit from the file sysytem.", 
						   "explain:copy from one file to another file.", 
						   "explain:modify the file name."}; 
	int helpnum=13; 
	int i=0; 
	if(command->next==NULL) 
	{ 
		mesg(cmdhlp[9]); 
		mesg(detail[9]); 
		mesg("step 1: login"); 
		printf("\t if user then login,if user not exist then create new user"); 
		mesg("step 2: open the file for read(/r),write(/w)or delete(/d)"); 
		printf("\t you can open one or more files.one command can open one file"); 
		mesg("step 3: read,write or delete some one file"); 
		printf("\t you can operate one of the opened files.one command can open one file"); 
		mesg("step 4: close the open file"); 
		printf("\t you can close one of the oepned files.one command can open one file"); 
		mesg("step 5: user loout.close all the user opened files"); 
		printf("\n command list:"); 
		for(i=0;inext->next!=NULL) 
		mesg("Too many parameters!"); 
	else 
	{ 
		for(i=0;inext->string,cmd[i])==0) 
				break; 
		} 
		if(i>=helpnum) 
			mesg("the command not existed!"); 
		else 
		{ 
			mesg(cmdhlp[i]); 
			mesg(detail[i]); 
		} 
	} 
} 
void dir()/*列目录文件*/ 
{ 
	int i=0; 
	int type=0; 
	char tempcode; 
	if(strcmp(username,"")==0) 
		mesg("No user login!"); 
	else 
	{ 
		if(command->next==NULL) 
		{ 
			tempcode='f'; 
		} 
		else 
		{ 
			if(strcmp(command->next->string,"/u")==0) 
			{ 
				tempcode='u'; 
			} 
			else if(strcmp(command->next->string,"/o")==0) 
			{ 
				tempcode='o'; 
			} 
			else if(strcmp(command->next->string,"/f")==0) 
			{ 
				tempcode='f'; 
			} 
			else if(command->next->string[0]=='/') 
				mesg("Error! /u/o/f request!"); 
			else if(command->next->next!=NULL) 
				mesg("Too many parameters!"); 
		} 
		if('u'==tempcode) 
		{ 
			printf("list the user account\n"); 
			printf("usename\n"); 
			for(i=0;inext==NULL) 
		mesg("Too few parametets!"); 
	else if(command->next->next==NULL) 
		mesg("Too few parametets!"); 
	else if(command->next->next->next!=NULL) 
		mesg("Too many parameters!"); 
	else 
	{ 
		strcpy(sourfile,command->next->string); 
		strcpy(destfile,command->next->next->string); 
		for(i=0;i=savenum) 
			printf("\n  the source file not eixsted!"); 
		else 
		{ 
			for(i=0;i=opennum) 
				printf("\n the source file not opened!"); 
			else 
			{ 
				for(j=0;j=M) 
						mesg("copy false!  file total limited."); 
					else 
					{ 
						strcpy(userfd[savenum].filename,destfile); 
						userfd[savenum].lenght=userfd[temp].lenght; 
						userfd[savenum].protect=userfd[temp].protect; 
						savenum++; 
						fp=fopen(username,"w+"); 
						for(i=0;inext==NULL) 
		mesg("Too few parametets!"); 
	else if(command->next->next==NULL) 
		mesg("Too few parametets!"); 
	else if(command->next->next->next!=NULL) 
		mesg("Too many parameters!"); 
	else 
	{ 
		strcpy(oldfile,command->next->string); 
		strcpy(newfile,command->next->next->string); 
		for(i=0;i=savenum) 
			printf("\n  the source file not eixsted!"); 
		else if(temp!=i&&i=opennum) 
				printf("\n the source file not opened!"); 
			else if(strcmp(oldfile,newfile)==0) 
			{ 
				printf("Rename false! oldfile and newfile can not be the same."); 
			} 
			else 
			{ 
				strcpy(openfd[i].filename,newfile); 
				strcpy(userfd[temp].filename,newfile); 
				mesg("Rename file success!\n"); 
			} 
		} 
	} 
}