/*本系统只写了
   1. create    2. open    3. close                                          
   4. delete    5. read    6. write     
   7. driectory            8. quit  
中的1,2,3,4,7,8还有就是
5,6没写...谁帮忙写一下好吗?
操作系统的课程设计..
*/
program filesystem;
uses crt;
const openf:integer=0;
type str=string[6];
//type buffer=string[1024];

linku=^datau;
 datau=record //用户文件目录
    filename:str; //文件名
    pcode:integer;
    length:integer;
    addr:integer; //物理地址
    next:linku;
end;

linkm=^datam;
  datam=record  //系统目录(主目录)
    username:str; //用户名
    link:linku; //用户目录指针
end;

linka=^dataa;
  dataa=record  //活动文件目录
     filename:str; //文件名
     pcode:integer; //权限
     rpointer:integer; //读指针
     wpointer:integer; //写指针
//addr:integer; 
addr:buffer
     next:linka;
end;

var
   mfd :array[1..5] of linkm;  //主目录
   p1,q1:linkm;
   p2,q2:linku;
   afd,p3,q3:linka;
   x,y1:str;
   i,y:integer;
   f:boolean;
   code:integer;

procedure init;  //初始化
label a;
begin
clrscr;
for i:=1 to 10 do writeln;
writeln('':25,'WELCOME TO OUR FILE SYSTEM !');
writeln;
writeln;
writeln('':27,'Please press space bar !');
repeat until readkey=' ';
clrscr;
a:write('User number ?');
readln(y1);
val(y1,y,code);
If code<>0 then goto a;
if y>5 then
begin
   writeln('Users too many ! (<=5)'); goto a;
end;
writeln('Begin users login:');
for i:=1 to y do
begin
write('User name',i,'?');
        readln(x);
        new(mfd[i]);
        mfd[i]^.username:=x;
        new(p2);
        p2^.next:=NIL; p2^.filename:=' ';
        mfd[i]^.link:=p2;
end;
if y<5 then
for i:=y+1 to 5 do
        mfd[i]:=NIL;
new(afd);
afd^.next:=NIL;
afd^.filename:=' ';
end;

procedure pcreate; //创建文件
label a,b;
begin
write('User name?');
      readln(x);
      for i:=1 to 5 do
        if x=mfd[i]^.username then goto b;
        writeln(x,'not been logined !'); goto a;
      b:write('New file name?');
      readln(x);
      p2:=mfd[i]^.link;
      while p2^.next<>NIL do p2:=p2^.next;
      new(q2);
      q2^.next:=NIL;
      q2^.filename:=' ';
      p2^.next:=q2;
      p2^.filename:=x;
      repeat write('Protection code?');
         readln(y1);
         val(y1,y,code);
      until code=0;
      p2^.pcode:=y;
      p2^.length:=0;
      writeln('Create finished !');
a:end;

procedure popen; //打开文件
label a,b;
begin
write('Filename?');
      readln(x);
      for i:=1 to 5 do
    begin
        p2:=mfd[i]^.link;
          while(x<>p2^.filename) and (p2^.next<>NIL)
        do p2:=p2^.next;
          if x=p2^.filename then goto a;
    end;
    writeln('The file not been created !');
    goto b;
a:if openf<5 then
    begin
        openf:=openf+1;
          q3:=afd;
          while q3^.next<>nil do q3:=q3^.next;
          new(p3);
          q3^.next:=nil;
          q3^.next:=p3;
          q3^.filename:=x;
          q3^.pcode:=p2^.pcode;
          q3^.rpointer:=0;
          q3^.wpointer:=0;
          writeln('Open finished !');
    end
      else writeln('Open files too many !');
b:end;

procedure close1 (t:integer); //关闭文件
begin
  write('Filename?');
  readln(x);
  q3:=afd;i:=1;
  while(q3^.filename<>x) and (q3^.next<>nil) do
    begin
      p3:=q3;
      q3:=q3^.next;
    end;
    i:=i+1;
    if q3^.filename=x then
    begin
      if q3=afd then
        afd:=q3^.next
      else
        p3^.next:=q3^.next;
      openf:=openf-1;
      dispose(q3);
      if t=0 then
        writeln('Close finished !');
    end
    else if t=0 then
        writeln('The file not been opened !');
end;

procedure pcolse; //关闭文件
const t:integer=0;
begin
close1(t);
end;

procedure pdelete; //删除文件
const t:integer=1;
label a,b;
begin
close1(t);
      for i:=1 to 5 do
    begin
        p2:=mfd[i]^.link;
        while(x<>p2^.filename) and (p2^.next<>nil) do
        begin
            q2:=p2;
            p2:=p2^.next;
        end;
        if p2^.filename=x then goto a;
    end;
    writeln('The file not exist ! 1');goto a;
  a:if p2=mfd[i]^.link then mfd[i]^.link:=p2^.next
      else q2^.next:=p2^.next;
      dispose(p2);
      writeln('Delete finished !');
b:end;

procedure pread; //读文件
begin

 write('The procedure has not exist !');
end;

procedure pwrite; //写文件
begin
  writeln('The procedure has not exist !');
end;

procedure pdirectory;
label a,b;
var c:char;j,n:integer;
begin
write('Main or user directory ? (m/u)');
      readln(c);
      if c='m' then
    begin
        writeln('Main File Directory');
          writeln('---------------------------------------');
          writeln('     user_name          file_number    ');
          j:=0; n:=0;
          for i:=1 to 5 do
        begin
            if mfd[i]=nil then goto b;
                  write(         mfd[i]^.username);
              p2:=mfd[i]^.link;
              while p2^.next<>nil do
            begin
                j:=j+1;
                  p2:=p2^.next;
            end;
              n:=n+j; writeln('  ':14,j);
              j:=0;
        end;
b:writeln('total','':10,n)
      end
      else
    begin
        write('User name ?');
          readln(x);
          for i:=1 to 5 do
        begin
            if mfd[i]^.username=x then
            begin
                writeln('',x,'file directory');
                  writeln('-------------------------------------------');
                  writeln('Filename    protection code    length');
                  p2:=mfd[i]^.link;
                  while p2^.next<>nil do
                begin
                    writeln(' ',p2^.filename,' ':9,p2^.pcode,' ':13,p2^.length);
                      p2:=p2^.next;
                end;
                  goto a;
            end;
        end;
          writeln('The directory not exist !');
    end;
a:end;
begin
init;
f:=true;
while f do
begin
repeat
writeln;
writeln;
    writeln('':27,'main menu');
    writeln('':15,'------------------------------------------------------------');
    writeln('':15,'|                                       |');
    writeln('':15,'|      1. create    2. open    3. close      |');
    writeln('':15,'|                                       |');
    writeln('':15,'|      4. delete    5. read    6. write       |');
    writeln('':15,'|                                       |');
    writeln('':15,'|      7. driectory            8. quit       |');
    writeln('':15,'|                                       |');
    writeln('':15,'-----------------------------------------------------------');
    write('':17,'Which   ?');
     readln(y1);val(y1,y,code);
     until code=0;
       case y of
         1:pcreate;
         2:popen;
         3:pcolse;
         4:pdelete;
         5:pread;
         6:pwrite;
         7:pdirectory;
         8:halt;
       end;
     end;
end.