open.c

来自「操作系统课程设计」· C语言 代码 · 共 63 行

C
63
字号

/*open.c*/

#include <stdio.h>
#include "filesys.h"
unsigned short aopen(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)    /* nosuchfile */ //~~
{
//	printf("\n文件不存在!\n");
	return NULL;
}
inode=iget(dinodeid);
if (!access(user_id,inode, openmode))    /* access denied */
{
	printf("\nfile open has not access!!!");
	iput(inode);
	return NULL;
}
/* alloc the sys-ofile item */
for (i=1;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=1; 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]=i;
/*if APPEND, free the block of the file before */
if (openmode & FWRITE) //~~~
{
	for(i=0; i<(inode->di_size/BLOCKSIZ+1); i++)
		bfree (inode->di_addr[i]);
	inode->di_size=0;
}
return j;
}

⌨️ 快捷键说明

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