finish the getdent

master
unknown 3 years ago
parent 0cbef7923e
commit d6bc73629f

@ -0,0 +1,78 @@
#define __LIBRARY__
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <asm/segment.h>
#include <sys/stat.h>
#include <fcntl.h>
#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 (;ret<inode->i_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;
}
Loading…
Cancel
Save