#define __LIBRARY__ #include #include #include #include #include #include #include #include #include #define BUF_MAX 128 #define DIRBUF 8192 #define NAME_MAX 14 struct dirent { /* Ŀ¼��ṹ�� */ long d_ino; /* i�ڵ� */ off_t d_off; /* λ�� */ unsigned short d_reclen; /* �ļ������� */ char d_name[NAME_MAX + 1]; /* �ļ��� */ }; int sys_getdents(unsigned int fd, struct dirent* dirp, unsigned int count) { struct file* file; struct m_inode* inode; struct buffer_head* head; struct dir_entry* de_tmp; struct dirent* self_tmp; /* ָ�������Զ���ṹ�����ʱָ�� */ char* buf; int dir_size, dire_size, read_bytes, addr_off, i; dir_size = sizeof(struct dirent); /* ��ǰĿ¼�ṹ��Ĵ�С */ dire_size = sizeof(struct dir_entry); /* Ŀ¼��ṹ��Ĵ�С */ self_tmp = (struct dirent*)malloc(dir_size); /* �ȷ����ʼ�ռ� */ buf = (char*)malloc(dir_size); file = current->filp[fd]; /* ��ȡ��ǰĿ¼����Ϣ */ inode = file->f_inode; head = bread(inode->i_dev, inode->i_zone[0]); /* �������ѭ������dirpΪ�����ڴ�洢������Ŀ¼����Ϣ��read_bytes�洢�������ֽ��� */ read_bytes = 0; for (addr_off = 0; addr_off < inode->i_size; addr_off += dire_size) { /* �ӵ�ǰĿ¼����ڿ�ʼ����ȡÿһ��Ŀ¼��Ļ��������� */ if (read_bytes >= count - dir_size) /* �����ȡ���ֽ����������ƣ�ֱ���˳�ѭ�� */ break; de_tmp = (struct dir_entry*)(head->b_data + addr_off); /* deָ��Ŀ¼������Ľṹ�� */ if (!de_tmp->inode) /* ���������Ŀ¼����� */ continue; self_tmp->d_ino = de_tmp->inode; /* Ŀ¼�����Ϣ����deָ��Ľṹ���У������ǿ�������ʱָ��self_tmpָ��Ľṹ���� */ self_tmp->d_off = 0; self_tmp->d_reclen = dir_size; strcpy(self_tmp->d_name, de_tmp->name); memcpy(buf, self_tmp, dir_size); /* �Ѹն��������ݿ�������buf�������ڴ��� */ for (i = 0; i < dir_size; i++) /* ���ڴ��������һ���ֽ�һ���ֽڵؿ�����dirp�������û�һ������ֵ */ put_fs_byte(*(buf + i), ((char*)dirp) + i + read_bytes); read_bytes += dir_size; } return read_bytes; }