Home > Back-end >  C language file management
C language file management

Time:11-19

I of the program when the new file system functions can be implemented, but when built on open has some system, the information stored inside and the bosses to help me take a look at the code
#include
#include
#include
#include
#include


# define SYS_SIZE 0 XFFFFF * 100
# define BLOCK_SIZE 512

The FILE * f;

Struct filsys {
Int s_nfree;
Long s_free [100].
Int s_ninode;
Int s_inode [96].

}, * p;

Struct index_block {
Int s_nfree;
Long s_free [96].
} q;

Struct filelist {
Char name [14].
Int inode_num;
} the file;

Struct inode {
Int i_size;
Int block_amount;
Long i_addr [16].
Char create_time [25];
} the inode;

Struct FCB {
Char name [12].
Int inode_num;
Int, informs;
Int i_size;
Int block_amount;
Long i_addr [16].
} FCB [5], * PTR.

Struct open_file_table {
Int offset.
Struct FCB * f_node;
} table [5];

Struct fd {
Struct open_file_table * t_node;
} fd [5];

The FILE * apply_room (char * sys_name)
{
F=fopen (sys_name, "w + b");//create a new read/write binary file
Fseek (f, SYS_SIZE SEEK_SET);
Fputc (EOF, f);
The fclose (f);
Return fopen (sys_name, "r + b");//open a read/write binary file
}

Void myfree (long block_num)
{
int i;
If (p - & gt; S_nfree & lt; 100)
{
P - & gt; S_free [p - & gt; s_nfree]=block_num;
P - & gt; S_nfree + +;
}
The else
{
Q.s _nfree=p - & gt; S_nfree;
for(i=0; I<100; I++)
Q.s _free [I]=p - & gt; S_free [I];
Fseek (f, (block_num - 1) * BLOCK_SIZE, SEEK_SET);
Fwrite (& amp; Q, sizeof (struct index_block), 1, f);
P - & gt; S_nfree=1;
P - & gt; S_free [0]=block_num;
}
}

Long myalloc ()
{
int i;
Long a;
P - & gt; S_nfree -;
If (p - & gt; S_nfree==0) {
A=p - & gt; S_free [0].
Fseek (f, (a - 1) * BLOCK_SIZE, SEEK_SET);
Fread (& amp; Q, sizeof (struct index_block), 1, f);
P - & gt; S_nfree=q.s _nfree;
for(i=0; I<100; I++)
P - & gt; S_free [I]=q.s _free [I];
Return a;
} the else return p - & gt; S_free [p - & gt; s_nfree];
}

Void init ()
{
Int j;
Long I;
P - & gt; S_nfree=1;
P - & gt; S_free [0]=0;
P - & gt; S_ninode=96;
for(i=0; I<96; I++)
P - & gt; S_inode [I]=1;
For (I=22; I<=SYS_SIZE/BLOCK_SIZE; I++)
Myfree (I);
J=p - & gt; S_nfree + 1;
While (j<100)
P - & gt; S_free [j++]=0;
Fseek (f 0, SEEK_SET);
Fwrite (p, sizeof (struct filsys), 1, f);
}

Int ialloc () {
int i=0;
While (p - & gt; S_inode [I] & gt;=0) i++;
P - & gt; S_inode [I]=0;
P - & gt; S_ninode -;
return i;
}

Int namei (char * name)
{
Int k=0;
While (k<96) {
If (p - & gt; S_inode [k]!=1) {
Fseek (f, BLOCK_SIZE + k x 16, SEEK_SET);
Fread (& amp; The file, sizeof (struct filelist), 1, f);
if(! STRCMP (the file name, name))
Return the file. Inode_num;
}
k++;
};
return -1;
}

Int name_i (char * name)
{
Int k=0;
Do
{
If (FCB [k]. 2==1)
{
if(! STRCMP (FCB [k]. Name, name))
Return FCB [k]. Inode_num;
}
k++;
} while (k<5);
return -1;
}
Void the create ()//create
{
Int I, inode_num;
Long t;
Char name [12].
Printf (" input file name: ");
The scanf (" % s ", name);
getchar();
If (namei (name)!=1) printf (" the file exited! \n");
The else
{
Inode_num=ialloc ();
Strcpy (the file name, name);
File. Inode_num=inode_num;
Fseek (f, BLOCK_SIZE + inode_num * 16, SEEK_SET);
Fwrite (& amp; The file, sizeof (struct filelist), 1, f);
The inode. I_size=0;
The inode. Block_amount=0;
for(i=0; I<16. I++) inode. I_addr [I]=0;
Time (& amp; T);
Strcpy (inode, create_time, ctime (& amp; T));
BLOCK_SIZE + inode_num fseek (f, 4 * * sizeof (struct inode), SEEK_SET);
Fwrite (& amp; The inode, sizeof (struct inode), 1, f);
P - & gt; S_inode [inode_num]=0;
Printf (" create success! \n");
}
}
Void main ()
{
int i;
Char ch, sys_name [15].
P=(struct filsys *) malloc (sizeof (struct filsys));
While (1)
{
Printf (" \ n 1: create a new file system ");
Printf (" 2: open the existing file system \ n ");
Printf (" choose: ");
If ((ch=getchar ())=='1')
{
Printf (" input the name of the file system: ");
The scanf (" % s ", sys_name);
getchar();
F=apply_room (sys_name);
The init ();
break;//break here used to jump out of the while (1) the cycle of
}
Else if (ch=='2')
{
Printf (" input the name of the file system: ");
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related