?? rdwt.c
字號:
#include <stdio.h>
#include "filesys.h"
unsigned int read(int fd1,char *buf,unsigned int size)
{
unsigned long off;
int block,block_off,i,j;
struct inode *inode;
char *temp_buf;
inode=sys_ofile[user[user_id].u_ofile[fd1]].f_inode;
printf("\nf_inode=%d\n",sys_ofile[user[user_id].u_ofile[fd1]].f_inode);
if(! (sys_ofile[user[user_id].u_ofile[fd1]].f_flag&FREAD))
{
printf("\nthe file is not opened for read\n");
return 0;
}
temp_buf=buf;
off=sys_ofile[user[user_id].u_ofile[fd1]].f_off;
if((off+size)>inode->di_size) size=inode->di_size-off;
block_off=off%BLOCKSIZ;
block=off/BLOCKSIZ;
if(block-off+size<BLOCKSIZ)
{
fseek(fd,DATASTART+inode->di_addr[block]*BLOCKSIZ+block_off,SEEK_SET);
fread(buf,1,size,fd);
return size;
}
fseek(fd,DATASTART+inode->di_addr[block]*BLOCKSIZ+block_off,SEEK_SET);
fread(temp_buf,1,BLOCKSIZ-block_off,fd);
temp_buf+=BLOCKSIZ-block_off;
j=(inode->di_size-off-block_off)/BLOCKSIZ;
for(i=0;i<(size-block_off)/BLOCKSIZ;i++)
{
fseek(fd,DATASTART+inode->di_addr[j+i]*BLOCKSIZ,SEEK_SET);
fread(temp_buf,1,BLOCKSIZ,fd);
temp_buf+=BLOCKSIZ;
}
block_off=(size-block_off)%BLOCKSIZ;
block=inode->di_addr[off+size/BLOCKSIZ+1];
fseek(fd,DATASTART+block*BLOCKSIZ,SEEK_SET);
fread(temp_buf,1,block_off,fd);
sys_ofile[user[user_id].u_ofile[fd1]].f_off+=size;
return size;
}
unsigned int write(unsigned short fd1,char *buf,unsigned int size) /*write*/
{
unsigned long off;
int block,block_off,i,j, k=0;
struct inode *inode;
char *temp_buf;
printf("f_off=%d\n",sys_ofile[user[user_id].u_ofile[fd1]].f_off);
inode=sys_ofile[user[user_id].u_ofile[fd1]].f_inode;
printf("di_size=%d,i_ino=%d\n",inode->di_size,inode->i_ino);
/*if(! (sys_ofile[user[user_id].u_ofile[fd1]].f_flag & FWRITE))
{
printf("\nthe file is not opened for wrie\n");
return 0;
}
*/
temp_buf=buf;
off=sys_ofile[user[user_id].u_ofile[fd1]].f_off;
block_off=off%BLOCKSIZ;
block=off/BLOCKSIZ;
if(block_off+size<BLOCKSIZ)
{
fseek(fd,DATASTART+inode->di_addr[block]* BLOCKSIZ+block_off,SEEK_SET);
fwrite(buf,1,size,fd);
return size;
}
if(sys_ofile[user[user_id].u_ofile[fd1]].f_flag == FAPPEND)
{
fseek(fd,DATASTART+inode->di_addr[block]*BLOCKSIZ+block_off,SEEK_SET);
fwrite(temp_buf,1,BLOCKSIZ-block_off,fd);
temp_buf+=BLOCKSIZ-block_off;
k=1;
}
printf("size=%d,block_off=%d\n",size,block_off);
for(i=0;i<(size-k*(BLOCKSIZ-block_off))/BLOCKSIZ;i++)
{
inode->di_addr[block+i]=balloc();
fseek(fd,DATASTART+inode->di_addr[block+k+i]*BLOCKSIZ,SEEK_SET);
fwrite(temp_buf,1,BLOCKSIZ,fd);
temp_buf+=BLOCKSIZ;
}
block_off=(size-k*(BLOCKSIZ-block_off))%BLOCKSIZ;
block=inode->di_addr[block+k+i]=balloc();
fseek(fd,DATASTART+block*BLOCKSIZ,SEEK_SET);
fwrite(temp_buf,1,block_off,fd);
sys_ofile[user[user_id].u_ofile[fd1]].f_off+=size;
inode->di_size=sys_ofile[user[user_id].u_ofile[fd1]].f_off;
return size;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -