⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 open.c

📁 操作系统实验,文件系统,c语言实现的
💻 C
字号:
#include<stdio.h>

#include "filesys.h"

unsigned short open(user_id,filename,openmode)

int user_id;

char  *filename;

unsigned short openmode;

{

 unsigned int dinodeid;

 struct inode *inode;

 int i,j;

 dinodeid=namei(filename);

 if(dinodeid == NULL)    /*no such file */

 {

    printf("\nfile does not existed!!!\n");

    return  NULL;

 }

 inode=iget(dinodeid);

 if(!access(user_id,inode,openmode))  /* access denled*/

 {  

     printf("\nfile open has not access!!!");

    iput(inode);

    return NULL;

 }

 /*  alloc the sys_ofile item  */

 for(i=0; i<SYSOPENFILE;i++)

     if(sys_ofile[i].f_count==0) break;

 if(i==SYSOPENFILE)

 {

     printf("\nsystem open file too much\n");

     iput(inode);

    return NULL;

 }

 sys_ofile[i].f_inode=inode;

 sys_ofile[i].f_flag=openmode;

 sys_ofile[i].f_count=1;

 if(openmode & FAPPEND)

     sys_ofile[i].f_off=inode->di_size;

  else

     sys_ofile[i].f_off=0;

  /*alloc the user open file item*/

 for(j=0;j<NOFILE;j++)

     if(user[user_id].u_ofile[j]==SYSOPENFILE+1) break;

 if(j==NOFILE)

 {

     printf("\nuser open file too much!!!\n");

     sys_ofile[i].f_count=0;

     iput(inode);

    return NULL;

 }

 user[user_id].u_ofile[j]=1;

 /*  if APPEND,free the block of the file before  */

 if(!(openmode & FAPPEND))

 {

      for(i=0;i<inode->di_size/BLOCKSIZ+1;i++)

        bfree(inode->di_addr[i]);

      inode->di_size=0;

  }

printf("\ni=%d,dinodeid=%d\n",i,dinodeid);

printf("\nj=%d,sys_ofile[i].f_off=%d\n",j,sys_ofile[i].f_off);
  return j;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -