📄 use.cpp
字号:
#include"use.h"
#include<string.h>
#include<iostream.h>
#include<iomanip.h>
use::use()
{
//初始化用户系统,当前用户指针,用户数目,根目录
for(int yy=0;yy<userno;yy++)
{
iuser[yy]=NULL;
}
cuser=-1;
usernum=0;
rdir=iopenfile.openf(NULL,"#rdir",IFDIR);
}
use::~use()
{
//关闭根目录
iopenfile.closef(rdir);
}
int use::login(char*u_name,char*u_password)
{
//在用户系统中寻找
for(int xx=0;xx<userno;xx++)
{
if(iuser[xx]!=NULL)
{
if(strcmp(iuser[xx]->u_name,u_name)==0)
{
if(strcmp(iuser[xx]->u_password,u_password)==0)
{
cuser=xx;
return OK;
}else
{
cout<<"PASSWORD INCORRECT\n";
return ERROR;
}
}
}
}
//在uwords中寻找
ofile *curfile;
curfile=iopenfile.openf(rdir->o_fcb,"uwords",IFREG|IREAD|GREAD|OREAD);
if(curfile->o_fcb->f_add[0]==0)
{
char name[]="root\0 1\0 A0";
curfile->o_fcb->f_uid='a';
curfile->o_fcb->f_gid='0';
iopenfile.writef(curfile->o_fcb,0,26,name);
}
char*base=new char[curfile->o_fcb->f_fsize+1];
iopenfile.readf(curfile->o_fcb,0,curfile->o_fcb->f_fsize,base);
char name[12];
char pw[12];
char uid;
char gid;
for(int c=0;c<=(curfile->o_fcb->f_fsize-1)/26;c++)
{
for(int i=0;i<12;i++)
{
name[i]=base[26*c+i];
pw[i]=base[26*c+i+12];
}
uid=base[26*c+24];
gid=base[26*c+25];
if(strcmp(name,u_name)==0)
{
if(strcmp(pw,u_password)==0)
{
for(int u=0;u<userno;u++)
{
if(iuser[u]==NULL)
{
iuser[u]=new struct user;
strcpy(iuser[u]->u_name,name);
strcpy(iuser[u]->u_password,pw);
iuser[u]->u_flag=UINC;
iuser[u]->u_number=c;
iuser[u]->u_uid=uid;
iuser[u]->u_gid=gid;
iuser[u]->u_cdir=0;
for(int kk=0;kk<16;kk++)
{
iuser[u]->u_ofile[kk]=NULL;
}
iuser[u]->u_ofile[0]=iopenfile.openf(NULL,"#rdir",IFDIR);
cuser=u;
usernum++;
iopenfile.closef(curfile);
delete[]base;
return OK;
}
}
iopenfile.closef(curfile);
cout<<"USER FULL\n";
delete[]base;
return ERROR;
}else
{
iopenfile.closef(curfile);
cout<<"password incorrect\n";
delete[]base;
return ERROR;
}
}
}
iopenfile.closef(curfile);
cout<<"usename incorrect\n";
delete[]base;
return ERROR;
}
int use::logout()
{
//关闭用户工作文件目录数组
for(int l=iuser[cuser]->u_cdir;l>=0;l--)
{
iopenfile.closef(iuser[cuser]->u_ofile[l]);
}
if(iuser[cuser]->u_flag==UCHG)
{
//存储用户信息
ofile *curfile;
curfile=iopenfile.openf(rdir->o_fcb,"uwords",MODE);
char*base=new char[curfile->o_fcb->f_fsize-iuser[cuser]->u_number*26+1];
iopenfile.readf(curfile->o_fcb,iuser[cuser]->u_number*26,curfile->o_fcb->f_fsize-iuser[cuser]->u_number*26,base);
char name[12];
char pw[12];
for(int i=0;i<12;i++)
{
name[i]=base[i];
pw[i]=base[i+12];
}
if(strcmp(name,iuser[cuser]->u_name)==0)
{
for(int dd=0;dd<12;dd++)
{
base[dd+12]=iuser[cuser]->u_password[dd];
}
iopenfile.writef(curfile->o_fcb,iuser[cuser]->u_number*26,curfile->o_fcb->f_fsize-iuser[cuser]->u_number*26,base);
iopenfile.closef(curfile);
}else
{
delete[]base;
return ERROR;
}
delete[]base;
}
//删除用户在用户系统中的用户结构
delete iuser[cuser];
iuser[cuser]=NULL;
usernum--;
for(int ll=0;ll<userno;ll++)
{
if(iuser[ll]!=NULL)
{
cuser=ll;
return OK;
}
}
cuser=-1;
return OK;
}
int use::createuser(char*u_name,char*password)
{
//创建用户,并将新用户信息存储在文件uwords中
if(strlen(u_name)>=12||strlen(password)>=12)
{
cout<<"the length of the username and password must be less than 12bytes\n";
return OK;
}
if(strcmp(iuser[cuser]->u_name,"root")!=0)
{
cout<<"ONLY THE ROOT CAN CREAT USER\n";
return OK;
}else
{
ofile *curfile;
curfile=iopenfile.openf(rdir->o_fcb,"uwords",MODE);
char*base=new char[curfile->o_fcb->f_fsize+1];
iopenfile.readf(curfile->o_fcb,0,curfile->o_fcb->f_fsize,base);
char name[12];
for(int c=0;c<=(curfile->o_fcb->f_fsize-1)/26;c++)
{
for(int i=0;i<12;i++)
{
name[i]=base[26*c+i];
}
if(strcmp(name,u_name)==0)
{
cout<<"THERE HAS BEEN A USER "<<u_name<<endl;
delete[]base;
iopenfile.closef(curfile);
return OK;
}
}
delete[]base;
char buf[26+1];
char uid=(curfile->o_fcb->f_fsize-1)/26+1+'A';
for(int rr=0;rr<12;rr++)
{
buf[rr]=u_name[rr];
buf[rr+12]=password[rr];
}
buf[24]=uid;
buf[25]='0';
buf[26]='\0';
iopenfile.writef(curfile->o_fcb,curfile->o_fcb->f_fsize,26,buf);
iopenfile.closef(curfile);
return OK;
}
}
int use::deleteuser(char*u_name)
{
if(strcmp(iuser[cuser]->u_name,"root")!=0)
{
cout<<"ONLY THE ROOT CAN DELETE USER\n";
return OK;
}else
{
//在用户系统中删除
for(int xx=0;xx<userno;xx++)
{
if(iuser[xx]!=NULL)
{
if(strcmp(iuser[xx]->u_name,u_name)==0)
{
int temp=cuser;
cuser=xx;
logout();
cuser=temp;
break;
}
}
}
//在uwords中删除
ofile *curfile;
curfile=iopenfile.openf(rdir->o_fcb,"uwords",IFREG|IREAD|GREAD|OREAD);
char*base=new char[curfile->o_fcb->f_fsize+1];
iopenfile.readf(curfile->o_fcb,0,curfile->o_fcb->f_fsize,base);
char name[12];
for(int c=0;c<=(curfile->o_fcb->f_fsize-1)/26;c++)
{
for(int i=0;i<12;i++)
{
name[i]=base[26*c+i];
}
if(strcmp(name,u_name)==0)
{
for(int jj=(c+1)*26;jj<=curfile->o_fcb->f_fsize-1;jj++)
{
base[jj-26]=base[jj];
}
iopenfile.writef(curfile->o_fcb,0,curfile->o_fcb->f_fsize-26,base);
iopenfile.closef(curfile);
delete[]base;
iopenfile.closef(curfile);
return OK;
}
}
iopenfile.closef(curfile);
delete[]base;
return OK;
}
}
//更改当前用户密码
int use::changepw(char*password)
{
strcpy(iuser[cuser]->u_password,password);
iuser[cuser]->u_flag=UCHG;
return OK;
}
int use::create(char*name)
{
if(iuser[cuser]->u_ofile[iuser[cuser]->u_cdir]->o_fcb->f_mode>=IFREG)
{
cout<<"Please close the file first\n";
return OK;
}
ofile *curfile;
curfile=iopenfile.openf(iuser[cuser]->u_ofile[iuser[cuser]->u_cdir]->o_fcb,name,IFREG,1);
if(curfile!=NULL)
{
//文件存在,则返回
iopenfile.closef(curfile);
cout<<"THERE HAS BEEN THE FILE "<<name<<endl;
return OK;
}else
{
//不存在则创建
curfile=iopenfile.openf(iuser[cuser]->u_ofile[iuser[cuser]->u_cdir]->o_fcb,name,MODE);
curfile->o_fcb->f_uid=iuser[cuser]->u_uid;
curfile->o_fcb->f_gid=iuser[cuser]->u_gid;
iopenfile.closef(curfile);
return OK;
}
}
int use::del(char*name)
{
ofile *curfile;
curfile=iopenfile.openf(iuser[cuser]->u_ofile[iuser[cuser]->u_cdir]->o_fcb,name,IFREG,1);
if(curfile==NULL)
{
//文件不存在则,返回
cout<<"CANNOT FIND THE FILE "<<name<<endl;
return OK;
}else
{
//存在,则调用openfile的deletef删除文件
if(curfile->o_fcb->f_uid=='a')
{
cout<<"this file is system file!you cannot delete!\n";
iopenfile.closef(curfile);
return OK;
}
if(curfile->o_fcb->f_uid!=iuser[cuser]->u_uid&&iuser[cuser]->u_uid!='A')
{
iopenfile.closef(curfile);
cout<<"YOU CANNOT DELETE THIS FILE\n";
return OK;
}
if(iopenfile.deletef(iuser[cuser]->u_ofile[iuser[cuser]->u_cdir],curfile)==-1)
{
iopenfile.closef(curfile);
}
return OK;
}
}
int use::open(char*name)
{
ofile *curfile;
curfile=iopenfile.openf(iuser[cuser]->u_ofile[iuser[cuser]->u_cdir]->o_fcb,name,IFREG,1);
if(curfile==NULL)
{
//不存在,则返回
cout<<"CANNOT FIND THE FILE "<<name<<endl;
return OK;
}
//存在打开
return OK;
}
//关闭当前工作文件
int use::close(char*name)
{
ofile *curfile;
curfile=iopenfile.openf(iuser[cuser]->u_ofile[iuser[cuser]->u_cdir]->o_fcb,name,IFREG,0,1);
if(curfile==NULL)
{
cout<<"maybe this file has not been opened or there is not the file"<<endl;
return OK;
}
iopenfile.closef(curfile);
iopenfile.closef(curfile);
return OK;
}
//读文件信息
int use::read(char*name)
{
ofile *curfile;
curfile=iopenfile.openf(iuser[cuser]->u_ofile[iuser[cuser]->u_cdir]->o_fcb,name,IFREG,0,1);
if(curfile==NULL)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -