diff --git a/mysystem_call.c b/mysystem_call.c new file mode 100644 index 0000000..4b6f9e9 --- /dev/null +++ b/mysystem_call.c @@ -0,0 +1,78 @@ +#define __LIBRARY__ +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +#define BUF_MAX 4096 +#define DIRBUF 8192 +#define NAME_MAX 14 + +struct dirent { + long d_ino; + off_t d_off; + unsigned short d_reclen; + char d_name[NAME_MAX+1]; +}; + +/*参数count指定该缓冲区的大小*/ +int sys_getdents(unsigned int fd,struct linux_dirent *dirp,unsigned int count); +{ + if(!count)return -1;/*count is zero*/ + if(fd>=NR_OPEN)return -EINVAL;/*fd is over range*/ + + struct file *file; + struct m_inode * inode; + int ret; + struct buffer_head *hd; + struct dir_entry *de; + struct dirent * temp; + char * buf; + int desize,ldsize,i,ldi; + + file=current->filp[fd]; + if(!file)return ENOTDIR;/*the file is not exist or not file*/ + ldsize = sizeof(struct dirent); + desize = sizeof(struct dir_entry); + inode = file ->f_inode; + temp = (struct dirent *)malloc(ldsize);/* //the inter veriable */ + buf = (char*)malloc(ldsize); + + /*get the inode's bread*/ + hd = bread(inode->i_dev , inode->i_zone[0]); + + ldi=0; + ret=0; + + for (;reti_size;ret += desize){ + if (ldi >= count-ldsize) + break; /* full */ + de = (struct dir_entry *)(hd->b_data + ret);/* de is set to the current dir_entry */ + if (!de -> inode )/* to skip if there is no data in de */ + continue; + /*To write, copying current dirent, */ + temp->d_ino = de->inode; + temp->d_off = 0; + temp->d_reclen = ldsize; + strcpy(temp->d_name,de->name); + + + /* by put_fs_byte to write back data to the usr */ + memcpy(buf, temp, ldsize); + for (i=0;i < ldsize;i++){ + put_fs_byte(*(buf+i), ((char*)dirp)+i+ldi); + } + /* memcpy(temp, buf, ldsize); */ + ldi += ldsize; + } + return ldi; +} +