⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 模拟linux的ext3文件系统。采用位图法。用一个文件模拟硬盘。实现创建、删除目录、文件。读、写文件
💻 C
字号:
#define MAIN#include "structure.h"struct user thisuser={{"Kong"},{"03055078"}};int login(){	u8 str[10];	printf("\nlocalhost login:");	scanf("%s",str);	if(strcmp(str,thisuser.username))		return 0;	printf("password:");	scanf("%s",str);	if(strcmp(str,thisuser.passwd))		return 0;	return 1;}void chpsd(){	u8 str1[10],str2[10];	printf("\nInput the old password:");	scanf("%s",str1);       while(strcmp(str1,thisuser.passwd))       {	       printf("Password is not match!!\n");	               	printf("\nInput the old password:");		scanf("%s",str1);       }       printf("Input the new password:");       scanf("%s",str1);       printf("Input again to make sure:");       scanf("%s",str2);       while(strcmp(str1,str2))       {	       printf("Authencation error!\n");                   printf("Input the new password:");              scanf("%s",str1);              printf("Input again to make sure:");              scanf("%s",str2);       }       strcpy(thisuser.passwd,str1);       printf("The password has been changed!\n");}void chusr(){	u8 str1[10],str2[10];	printf("\nInput the old user name:");	scanf("%s",str1);       while(strcmp(str1,thisuser.username))       {	       printf("User name is not match!!\n");	               	printf("\nInput the old user name:");		scanf("%s",str1);       }       printf("Input the new user name:");       scanf("%s",str1);       printf("Input again to make sure:");       scanf("%s",str2);       while(strcmp(str1,str2))       {	       printf("Authencation error!\n");                   printf("Input the new user name:");              scanf("%s",str1);              printf("Input again to make sure:");              scanf("%s",str2);       }       strcpy(thisuser.username,str1);       printf("The user name has been changed!\n");}void copy_string(u16 len, u8 *name, u8 *string){ u16 i; for(i=0;i<len;i++){            *name++=*string++; }}int main(){ u8 string[MAX_LENGTH]={0,}; u8 cmd[MAX_LENGTH]={0,}; u8 path[MAX_LENGTH]={0,}; u8 name[MAX_LENGTH]={0,}; u8 *cmdstart,*pathstart,*namestart,*thisname,*endname; u8 ch,ch1; u16 cmdlen,namelen,pathlen, i,*fd=NULL;  if(!(init_memory())){	 printf("Do you want to create new FS?(y/n)\n");	 ch1=getchar();	 if(ch1=='y') {		 init_FS();		 init_memory();	 }	 else return 0; }out: while(!login()) {	 printf("\nInvalid username or passwd!!\nPay attention to Caps Lock key,the form of letter must be right!\n"); } getchar(); while(1){       	for(i=0;i<MAX_LENGTH;i++) { 	string[i]=0;	cmd[i]=0;	path[i]=0;	name[i]=0;}	cmdlen=0;	pathlen=0;	namelen=0;       	printf("[%s-shell@%s ]# ",thisuser.username,current_path);       	gets(string);	if(!(strcmp(string,"exit"))) return 0;        thisname=string;	cmdstart=string;	while((ch=*thisname)!=' '&&(*thisname)!=0){    	         thisname++; 	} 	cmdlen=thisname-cmdstart;	copy_string(cmdlen,cmd,cmdstart); 	while((ch=*thisname)==' ')    thisname++; 	if(ch){  	 	pathstart=thisname;         	while((ch=*thisname)) thisname++;	 	endname=thisname;		ch=*(thisname-1);	        if(ch=='/'){			pathlen=endname-pathstart; 			copy_string(pathlen,path,pathstart);		}		else{		       while((ch=*thisname)!='/'&&thisname>pathstart)  thisname--;		       if(ch=='/'){ 		                pathlen=thisname-pathstart+1;		                copy_string(pathlen,path,pathstart);			        namestart=thisname+1;			        namelen=endname-thisname-1;			        copy_string(namelen,name,namestart);		       }		       else {			        namestart=thisname;			        namelen=endname-thisname;			        copy_string(namelen,name,namestart);		       }		}	}        //printf("cmd:%s %d\npath:%s %d\nname:%s %d\n",cmd,cmdlen,path,pathlen,name,namelen);        if(match(cmdlen,cmd,"ls")&&cmdlen>=2){	 	dir(); 	} 	else if(match(cmdlen,cmd,"help")&&cmdlen>=4)	{		printf("command list:\nls--------List the content of the current directory\n");		printf("mkdir-----Make a new directory\n");		printf("rmdir-----Remove a directory\n");		printf("cd--------Change current directory\n");		printf("create----Create a file\n");		printf("open------Open a file\n");		printf("close-----Close the opened file\n");		printf("write-----Write into a file\n");		printf("read------Read from a file\n");		printf("off-------Login out\n");		printf("exit------Exit the file system\n");		printf("chpsd-----Change password\n");                printf("chusr-----Change user name\n");		printf("format----Format the FS\n");			}	else if(match(cmdlen,cmd,"chpsd")&&cmdlen>=5){		chpsd();		getchar();	}        else if(match(cmdlen,cmd,"chusr")&&cmdlen>=5){		chusr();		getchar();	}			else if(match(cmdlen,cmd,"off")&&cmdlen>=3){		goto out;	}				 	else if(match(cmdlen,cmd,"mkdir")&&cmdlen>=5){	 	mkdir(path,name); 	} 	else if(match(cmdlen,cmd,"rmdir")&&cmdlen>=5){	 	rmdir(path,name); 	} 	else if(match(cmdlen,cmd,"cd")&&cmdlen>=2){          	cd(path); 	}        else if(match(cmdlen,cmd,"create")&&cmdlen>=6){ 	 	create(path,name); 	} 	else if(match(cmdlen,cmd,"delete")&&cmdlen>=6){ 	 	delete(path,name); 	} 	else if(match(cmdlen,cmd,"open")&&cmdlen>=4){ 	 	fd=open(path,name); 	} 	else if(match(cmdlen,cmd,"close")&&cmdlen>=5){	 	close(fd);		fd=NULL; 	}	else if(match(cmdlen,cmd,"write")&&cmdlen>=5){ 	 	write(fd,path,name); 	} 	else if(match(cmdlen,cmd,"read")&&cmdlen>=4){ 	 	read(fd); 	} 	 	else if(match(cmdlen,cmd,"format")&&cmdlen>=6){                 printf("Format will clear all the content of the file system,are you sure?(y/n)\n");	         ch1=getchar();	         if(ch1=='y') {		 init_FS();		 init_memory();	        }             getchar();	 	 	}        else printf("Bad command!!\n"); }  return 1;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -