?? filesystem.c
字號:
#include <stdio.h>
#include <string.h>
#include <io.h>
#include <dos.h>
#include <conio.h>
#include <process.h>
#include "head.h"
void main()
{ long currentuserpos,newuserpos;
long currentfilepos,prefilepos,sourcefilepos,targetfilepos;
long textpos;
long currentpasspos,newpasspos;
log newuser,user;
pas newpass,pass;
fil currentfile,prefile,sourcefile,targetfile;
texter content,newtext,sourcetext,targettext;
int Pcode,numbers,basement;
char choice,choice1,choice2,choice3;
char loguser[20],pw[20],enterfile[8],texts[1000];
FILE *fp;
printf(" Wellcome to virtual file system.\n"
" This program will not damage your current file system.\n"
" copyright 2004/7/2\n"
" computer 015 xudong 01171121\n");
//創(chuàng)建虛擬硬盤
if((fp=fopen("vdisk.vdr","rb+"))==NULL) //新建vdisk為空文件
{if((fp=fopen("vdisk.vdr","wb+"))==NULL)//判斷剩余空間,無空間則退出系統(tǒng)
{printf("Not enough disk space for a virtual drive");
exit(0);
}
else
{strcpy(newuser.username,"Administrator");//給newuser.username賦一個初值
strcpy(newpass.password,"newpassword");//給newpass.password賦一個初值
newuser.link=newuser.nextuser=0l;
newpass.link1=newpass.nextpass=0l;
fwrite(&newuser,loglen,1,fp);
fwrite(&newpass,paslen,1,fp);
}
}
/////////////////////login///////////////////////////////////
begin:rewind(fp);
printf("\n LOGIN or QUIT(l/q):");
enter:
choice=getch();
putch(choice);
switch(choice)
{case 'l':
case 'L':break;
case 'q':
case 'Q':exit(0);
default:printf("\nplease choose again(l/q)");goto enter;
}
printf("\nENTER UR USERNAME:");
scanf("%s",loguser);printf("\n");
password: printf("\nENTER UR PASSWORD:");
scanf("%s",pw);printf("\n");
start:
currentuserpos=ftell(fp);//輸入的新指針給當(dāng)前用戶地址
currentpasspos=ftell(fp);//輸入的新指針給當(dāng)前密碼地址
fread(&user,loglen,1,fp);
fread(&pass,paslen,1,fp);
if(!strcmp(user.username,loguser))//輸入用戶名和當(dāng)前用戶名比較
{if(!strcmp(pass.password,pw))//輸入密碼和當(dāng)前密碼比較
{printf("\nWelcome back,%s.U have been registered!^_^\n",loguser);//相同,輸出歡迎老用戶
goto menu;
}else
goto password;
}
else
{if(user.nextuser==0l) goto anewuser;//如果當(dāng)前vdisk中沒有用戶,跳轉(zhuǎn)newuser
else
{fseek(fp,user.nextuser,0);
fseek(fp,pass.nextpass,0);
goto start;
}
}
anewuser:
goend;
newuserpos=ftell(fp);//輸入的新指針給新用戶地址
newpasspos=ftell(fp);//輸入的新指針給新密碼地址
strcpy(newuser.username,loguser);//輸入的新用戶名給newuser.username
strcpy(newpass.password,pw);//輸入的新密碼給newpass.password
newuser.link=newuser.nextuser=0l;
newpass.link1=newpass.nextpass=0l;
fwrite(&newuser,loglen,1,fp);//寫入
fwrite(&newpass,paslen,1,fp);
fseek(fp,currentuserpos,0);//移動指針到當(dāng)前用戶地址
fseek(fp,currentpasspos,0);//移動指針到當(dāng)前密碼地址
fread(&user,loglen,1,fp);//打開
fread(&pass,paslen,1,fp);
user.nextuser=newuserpos;//新用戶地址給user的后繼
pass.nextpass=newpasspos;//新密碼地址給password的后繼
fseek(fp,currentuserpos,0);//指針移動到當(dāng)前用戶地址
fseek(fp,currentpasspos,0);//指針移動到當(dāng)前密碼地址
fwrite(&user,loglen,1,fp);//寫入用戶名
fwrite(&pass,paslen,1,fp);//寫入密碼
currentuserpos=newuserpos;//新用戶地址給當(dāng)前用戶地址
currentpasspos=newpasspos;//新密碼地址給當(dāng)前密碼地址
user=newuser;
printf("\nWelcome new user,try to use this OS %s.\n",loguser);
goto menu;
pass=newpass;
///////////////////////////////MENU/////////////////////////////////////
menu:
while (1)
{
printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n"
" THE DEMO DISPLAY FOR THE FILE SYSTEM \n"
" \n"
" 1 : create a new file \n"
" 2 : delete a file \n"
" 3 : write \n"
" 4 : read \n"
" 5 : directory \n"
" 6 : copy \n"
" 7 : logout for another user \n"
" 8 : exit the OS \n"
"\n");
do
{
printf("\n PLEASE ENTER YOUR CHOICE :");
choice1=getch();
putch(choice1);
}while((choice1<'1')||(choice1>'8'));
printf("\n\n");
switch (choice1)
{
case '1' :goto create;
break;
case '2' :goto del;
break;
case '3' :goto write;
break;
case '4' :goto read;
break;
case '5' :goto dir;
break;
case '6' :goto copy;
break;
case '7' :goto logout;
break;
case '8' :goto exit;
break;
default: goto error;
break;
}
/////////////////////exit/////////////////////////
exit:
exit(0);//退出
//////////////////////logout/////////////////////////
logout:
printf("\nlogout or not(Y/N):");
choice2=getch();
// putch(choice2);
switch(choice2)
{case 'Y':goto begin;
case 'y':goto begin;
case 'N':goto menu;
case 'n':goto menu;
default: goto logout;
}
/////////////////////create//////////////////////////
create:
printf("\nEnter a new file name:");//創(chuàng)建文件
scanf("%s",enterfile);
printf("\nenter a pcode(0-read only,1-write only,2-both w/r):");//輸入保護(hù)碼
entercode:
scanf("%d",&Pcode);
if(Pcode!=0&&Pcode!=1&&Pcode!=2)
{printf("\nout of range,re-enter:");
goto entercode;
}
if(user.link!=0l)//用戶還沒有文件
{again:
currentfilepos=user.link;
loop:
fseek(fp,currentfilepos,0);
fread(¤tfile,fillen,1,fp);
if(!strcmp(currentfile.filename,enterfile))//輸入的文件名和當(dāng)前文件文件名比較
{printf("\nThere is a file with the same file name!Please re-enter the file name:");
scanf("%s",enterfile);
goto again;//文件名相同,跳轉(zhuǎn)again
}
if(currentfile.nextfile==0l) goto out;
currentfilepos=currentfile.nextfile;//后繼文件地址給當(dāng)前文件地址,跳轉(zhuǎn)loop,重新比較
goto loop;
out:goend;
currentfile.nextfile=ftell(fp);
fseek(fp,currentfilepos,0);
fwrite(¤tfile,fillen,1,fp);
goto createnew;
}
else
{goend;
currentfilepos=ftell(fp);
user.link=currentfilepos;
fseek(fp,currentuserpos,0);
fwrite(&user,loglen,1,fp);
createnew:
goend;
currentfilepos=ftell(fp);
strcpy(currentfile.filename,enterfile);//輸入文件名賦給當(dāng)前文件的文件名
currentfile.pcode=Pcode;//保護(hù)碼賦給當(dāng)前文件的保護(hù)碼
currentfile.length=0;//默認(rèn)當(dāng)前文件長度為0
currentfile.addr=currentfile.nextfile=0l;
fwrite(¤tfile,fillen,1,fp);
if(Pcode==0)
{printf("\nThis file is read-only! ");
printf("\?");
getch();
fgets(texts,1000,stdin);
anewtext://創(chuàng)建新文件
goend;
currentfile.addr=ftell(fp);
fseek(fp,currentfilepos,0);
fwrite(¤tfile,fillen,1,fp);
printf("%s has been created!\n",enterfile);
goto writing;
}
else goto menu;
}
/////////////////////////directory////////////////////
dir:
printf("\nFile name\tPcode\tblocks\n"); //顯示當(dāng)前用戶所擁有的文件
currentfilepos=user.link;
while(currentfilepos!=0l)
{fseek(fp,currentfilepos,0);
fread(¤tfile,fillen,1,fp);
printf("%s\t\t %d\t %d\n",currentfile.filename,currentfile.pcode,currentfile.length);
currentfilepos=currentfile.nextfile;
}
goto menu;
}
//////////////////////write///////////////////////
write:
printf("\nPlease enter the file name:"); //寫入
scanf("%s",enterfile);
getchar();
if(user.link==0l) goto nofile;
currentfilepos=user.link;
fseek(fp,currentfilepos,0);
fread(¤tfile,fillen,1,fp);
while(strcmp(currentfile.filename,enterfile)&¤tfile.nextfile!=0l)//輸入用戶名跟當(dāng)前用戶名比較
{currentfilepos=currentfile.nextfile; //當(dāng)前用戶后繼地址給當(dāng)前文件地址
fseek(fp,currentfilepos,0);
fread(¤tfile,fillen,1,fp);
}
if(strcmp(currentfile.filename,enterfile)) goto nofile;
if(currentfile.pcode==0)//如果只讀,則不允許
{printf("\nThis file is write-only.You cannot read it!\n");
goto menu;
}
printf("\nstart:");//開始寫入文字
fgets(texts,1000,stdin);
printf("\nSave or Not(Y/N):");
//////////////////////////save///////////////////////
save:choice3=getchar(); //保存
switch(choice3)
{case 'Y':
case 'y':break;
case 'N':
case 'n':goto menu;
default:printf("\nwrong choice,again(Y/N):");goto save;
}
if((textpos=currentfile.addr)==0l) goto anewtext;//當(dāng)前文件塊的地址
fseek(fp,textpos,0);
fread(&content,textlen,1,fp);
while(content.nexttext!=0l)
{textpos=content.nexttext;
fseek(fp,textpos,0);
fread(&content,textlen,1,fp);
}
goend;
content.nexttext=ftell(fp);
fseek(fp,textpos,0);
fwrite(&content,textlen,1,fp);
writing:
goend;
textpos=ftell(fp);
numbers=strlen(texts)/19; //需要的文件塊的數(shù)量/
basement=0;
while(basement<numbers)
{strcopy(newtext.text,texts+basement*19,19);
newtext.nexttext=0l;
fwrite(&newtext,textlen,1,fp);
goend;
newtext.nexttext=ftell(fp);
fseek(fp,textpos,0);
fwrite(&newtext,textlen,1,fp);
textpos=newtext.nexttext;
basement++;
fseek(fp,textpos,0);
}
strcopy(newtext.text,texts+basement*19,19); //最后一個文件塊的內(nèi)容/
newtext.nexttext=0l;
fwrite(&newtext,textlen,1,fp);
currentfile.length+=numbers+1;////////change///////////
fseek(fp,currentfilepos,0);
fwrite(¤tfile,fillen,1,fp);
goto menu;
////////////////////////copy/////////////////////////////
copy:
printf("\nPlease enter the source file name:");
scanf("%s",enterfile);
if(user.link==0l) goto nofile;
sourcefilepos=user.link;
fseek(fp,sourcefilepos,0);
fread(&sourcefile,fillen,1,fp);
while(strcmp(sourcefile.filename,enterfile)&&sourcefile.nextfile!=0l) //尋找原文件
{sourcefilepos=sourcefile.nextfile;
fseek(fp,sourcefilepos,0);
fread(&sourcefile,fillen,1,fp);
}
if(strcmp(sourcefile.filename,enterfile)) goto nofile;
printf("\nPlease enter the target file name:");
tarfile:scanf("%s",enterfile);
currentfilepos=user.link;
fseek(fp,currentfilepos,0);
fread(¤tfile,fillen,1,fp);
while(strcmp(currentfile.filename,enterfile)&¤tfile.nextfile!=0l) //尋找目標(biāo)文件
{currentfilepos=currentfile.nextfile;
fseek(fp,currentfilepos,0);
fread(¤tfile,fillen,1,fp);
}
if(!strcmp(currentfile.filename,enterfile))
{printf("\nThere is a file with the same name.Please re-enter the target file name:");
goto tarfile;
}
goend;
targetfilepos=currentfile.nextfile=ftell(fp);
fseek(fp,currentfilepos,0);
fwrite(¤tfile,fillen,1,fp);
goend;
targetfile=sourcefile; //把sourcefile的file信息給targefile
targetfile.nextfile=0l;
strcpy(targetfile.filename,enterfile);
fwrite(&targetfile,fillen,1,fp);
if(targetfile.addr==0l) goto menu;
goend;
targetfile.addr=ftell(fp);
fseek(fp,targetfilepos,0);
fwrite(&targetfile,fillen,1,fp);
textpos=sourcefile.addr; //sourcefile文件塊的地址
fseek(fp,textpos,0);
fread(&sourcetext,textlen,1,fp);
while(sourcetext.nexttext!=0l)
{strcpy(targettext.text,sourcetext.text);
goend;
fwrite(&targettext,textlen,1,fp);
goend;
targettext.nexttext=ftell(fp);
fseek(fp,-(long)textlen,2);
fwrite(&targettext,textlen,1,fp);
textpos=sourcetext.nexttext;
fseek(fp,textpos,0);
fread(&sourcetext,textlen,1,fp);
}
strcpy(targettext.text,sourcetext.text); //拷貝最后一個文件塊
targettext.nexttext=0l;
goend;
fwrite(&targettext,textlen,1,fp);
goto menu;
nofile:printf("\nThe file is not found!"); //error
goto menu;
/////////////////////read///////////////////////////
read:
printf("\nPlease enter the file name:");
scanf("%s",enterfile);
if(user.link==0l) goto nofile;
currentfilepos=user.link;
fseek(fp,currentfilepos,0);
fread(¤tfile,fillen,1,fp);
while(strcmp(currentfile.filename,enterfile)&¤tfile.nextfile!=0l)
{currentfilepos=currentfile.nextfile;
fseek(fp,currentfilepos,0);
fread(¤tfile,fillen,1,fp);
}
if(strcmp(currentfile.filename,enterfile)) goto nofile;
if(currentfile.pcode==1) //文件只讀則不允許
{printf("\nThis file is read-only.You cannot modify it!\n");
goto menu;
}
else
{textpos=currentfile.addr;
printf("\n");
while(textpos!=0l)
{fseek(fp,textpos,0);
fread(&content,textlen,1,fp);
printf("%s",content.text);
textpos=content.nexttext;
}
goto menu;
};
/////////////////////////delete///////////////////////////
del:printf("\nPlease enter the file name:");
scanf("%s",enterfile);
if(user.link==0l) goto nofile;
currentfilepos=user.link;
fseek(fp,currentfilepos,0);
fread(¤tfile,fillen,1,fp);
while(strcmp(currentfile.filename,enterfile)&¤tfile.nextfile!=0l)
{prefilepos=currentfilepos;
currentfilepos=currentfile.nextfile;
fseek(fp,currentfilepos,0);
fread(¤tfile,fillen,1,fp);
}
if(strcmp(currentfile.filename,enterfile)) goto nofile;
if(currentfilepos==user.link)
{user.link=currentfile.nextfile;
fseek(fp,currentuserpos,0);
fwrite(&user,fillen,1,fp);
goto menu;
}
else
{fseek(fp,prefilepos,0);
fread(&prefile,fillen,1,fp);
prefile.nextfile=currentfile.nextfile;
fseek(fp,prefilepos,0);
fwrite(&prefile,fillen,1,fp);
goto menu;
};
error:printf("you have chosen a wrong choice,choose again!!\n");
goto menu;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -