#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"filesys.h"
unsigned short currentuser=65535;
char *command;
struct usergroup MFD; //主目录,所有的用户都在一各组里??
char block_buf[BLOCKSIZE] ; //内存的块位图数组
char inode_buf [INODENUM] ;//内存中结点位图数组
FILE *fd; //文件类型指针
struct superblock *super;
struct userdir UFD;
struct linkgroup LGP;
char block_buff[BLOCKNUM];
char inode_buff[INODENUM];
FILE *fd1;
char * command1;
char * command2;
char * command3;
void print_help()
{
printf("\n\t\t\t help\n");
printf("\t\t format :creat a new file system\n");
printf("\t\t login :enter filesystem\n");
printf("\t\t logout :logout enter\n ");
printf("\t\t adduser :add user \n");
printf("\t\t createfile :create a file\n");
printf("\t\t writefile:write content to file\n");
printf("\t\t readfile :read content from file\n");
printf("\t\t copyfile: copy from one file to another\n");
printf("\t\t showfiles :list files\n");
printf("\t\t listuser :list users\n");
printf("\t\t removefile:delete files\n");
printf("\t\t removeuser:delete user\n");
printf("\t\t quit:exit filesystem\n");
printf("\t\t changepsw:change passward\n");
printf("\t\t refile:rename a file\n");
printf("\t\t createlink:create a link to a file\n");
printf("\t\t showlinks:show all links\n");
printf("\t\t linkfile:link to a file\n");
}
int main()
{ print_help();
int pid;
char yn;
int loginf=3;
fd1=fopen("filesysterm","r");//打开文件filesysterm, 并返回一个关联该文件的流
if(fd1==NULL)
{
printf("\n Disk has not format!! format now:(Y/N)\n");
scanf("%c",&yn);
if(yn=='y'||yn=='Y')
{
format();
printf("\n ------format completed------\n");
}
else
exit(0);
}
else{
printf("\n -------filesystem exist------\n");
printf("\n -------please press enter------\n");
fclose(fd1);
}
command=(char *)getinput('\n');
free(command);
load(); //-----------------------------?????????
while(loginf) //只有三次登陆机会
{ loginf--;
print_help(); //-------------实现选项的循环出现
printf("\n please enter the operation,login first \n");
command=(char *)getinput('\n');
if(!strcmp(command,"login"))
{printf("\n please enter your name \n");
command1=(char *)getinput('\n');
printf("\n please enter your passward \n");
command2=(char *)getinput('\n');
currentuser=login(command1,command2);
}
if(currentuser==65535)
{//printf("\n user not exist!\n");
printf("\n you have %d chances to login",loginf ,"\n");
continue;
}
else if(currentuser==65534)
{
//printf("\n name or password error\n");
printf("\n you have %d chances to login",loginf ,"\n");
continue;
}
else
{printf("LOGIN SUCCESSFUL!\n");
if(loginf==0)
loginf++;
break;
free(command);
}
}
if(loginf!=0)
{ while(1)
{ print_help();
printf("\n Please enter your command: \n");
command=(char *)getinput('\n');
if(!strcmp(command,"login"))
{ printf("\n Please enter your name \n");
command1=(char *)getinput('\n');
printf("\n Please enter your password\n");
command2=(char *)getinput('\n');
currentuser=login(command1,command2);
if(currentuser!=65535)
{
printf("LOGIN SUCCESSFUL!\n");
continue;
}
}
if(currentuser==65535)
{
printf("Please login first\n");
}
else
{
if(!strcmp(command,"quit"))
{exit(0);
}
else if (!strcmp(command,"logout"))
{ pid=logout();//令currentuser=65535
printf("LOGOUT SUCCESSFUL!\n");
}
else if(!strcmp(command,"createfile"))
{
printf("\n Please enter file name :\n");
command1=(char *)getinput('\n');
printf("\n Please enter file format :\n");
command2=(char *)getinput('\n');
int id1 =oct2dec(atoi(command2));
struct inode* pinode=makefile(currentuser,command1,id1);
if (pinode==NULL)//如果重名返回NULL
{
printf("%s The file has already exist!\n",command1);
}
else
{printf("\nThe file is created successful!\n");
}
}
else if(!strcmp(command,"showfiles"))
{ show_dir();
}
else if(!strcmp(command,"writefile"))
{
printf("\n Please enter file name\n");//请输入文件名
command1=(char *)getinput('\n');
//******************************
unsigned short inode_num=mapUFDinode(command1);
if(inode_num==MAPERR)
{ printf("\n The file does not eixst\n");
}
else
{ printf("\n please input the context ,# end\n");
char * context=(char *)getinput('#');
if(writefile(context,command1)==NULL)
{ printf("\n Write fail\n");}
else
{printf("\n Write successful\n");
}
}
}
else if(!strcmp(command,"readfile"))
{ printf("\n Please enter file name:\n");//请输入文件名
command1=(char *)getinput('\n');
char *context=(char *)readfile(command1);
if(context==NULL)
{ printf("\n The file does not exist \n");}//文件不存在
else
{printf("\n %s\n",context);}
}
else if(!strcmp(command,"copyfile"))
{
unsigned short j;
printf("\n Please enter name of the source file \n");
command1=(char *)getinput('\n');
printf("\n Please enter name of the distincting file \n");
command2=(char *)getinput('\n');
j=copy(command2,command1);
if(j==0)
{
printf("\n The dsitinct file does not exist\n");//目标文件不存在
}
if(j==1)
{
printf("\n The source file does not eixst\n");//源文件文件不存在
printf("\t\t\t\t 操作命令表\n\n");
printf("\t\t\t printhelp:\t显示帮助\n\n");
printf("\t\t\t format:\t建立一个新的文件系统\n\n");
printf("\t\t\t adduser:\t增加用户\n\n");
printf("\t\t\t createfile :\t建立新文件\n\n");
printf("\t\t\t writefile:\t写文件\n\n");
printf("\t\t\t readfile :\t读文件\n\n");
printf("\t\t\t copyfile :\t复制文件\n\n");
printf("\t\t\t showfiles:\t列出文件\n\n");
printf("\t\t\t listuser:\t列出用户\n\n");
printf("\t\t\t removefile:\t移除文件\n\n");
printf("\t\t\t removeuser:\t移除用户\n\n");
printf("\t\t\t login:\t\t登录文件系统\n\n");
printf("\t\t\t logout:\t注销登录,退出文件系统\n\n");
printf("\t\t\t quit:\t\t退出\n");
}
if(j==2)
{
printf("\n Copy successful!\n");//拷贝成功
}
if(j==3)
{
printf("\n Copy fail\n");//拷贝失败
}
}
else if(!strcmp(command,"removefile"))
{ unsigned short i;
printf("\n Please enter the name of file\n");//请输入文件名
command1=(char *)getinput('\n');
i=removefile(command1);
if(i==0)
{printf("\n The file does not exist\n");}//文件不存在
else if(i==1)
{printf("\n The file delete successful!\n");}//文件删除成功
else
{printf("\n You do not have the right of deleting a file \n");}//权限不对
}
else if(!strcmp(command,"adduser"))
{
printf("\n Please enter the name of user:\n");//请输入用户名
command1=(char *)getinput('\n');
printf("\n Please enter the group of user:\n");//请输入所在组名
command2=(char *)getinput('\n');
printf("\n please enter passward:\n");
command3=(char *)getinput('\n');
int i;
i=adduser(command1,command2,command3);
if(i==2)
{printf("\n You do not have the right of adding a user\n");}//权限不对,只有root用户才能添加新用户
else if(i==1)
{printf("\n Add user successful!\n");}//添加成功
else
{printf("\n Add