* this function is a POSIX compliant version, which will return a pointer * to a dirent structure representing the next directory entry in the * directory stream. * * @param d the directory stream pointer. * * @return the next directory entry, NULL on the end of directory or failed. */
| 758 | * @return the next directory entry, NULL on the end of directory or failed. |
| 759 | */ |
| 760 | struct dirent *readdir(DIR *d) |
| 761 | { |
| 762 | int result; |
| 763 | struct dfs_file *fd; |
| 764 | |
| 765 | fd = fd_get(d->fd); |
| 766 | if (fd == NULL) |
| 767 | { |
| 768 | rt_set_errno(-EBADF); |
| 769 | return NULL; |
| 770 | } |
| 771 | |
| 772 | if (d->num) |
| 773 | { |
| 774 | struct dirent *dirent_ptr; |
| 775 | dirent_ptr = (struct dirent *)&d->buf[d->cur]; |
| 776 | d->cur += dirent_ptr->d_reclen; |
| 777 | } |
| 778 | |
| 779 | if (!d->num || d->cur >= d->num) |
| 780 | { |
| 781 | /* get a new entry */ |
| 782 | result = dfs_file_getdents(fd, |
| 783 | (struct dirent *)d->buf, |
| 784 | sizeof(d->buf) - 1); |
| 785 | if (result <= 0) |
| 786 | { |
| 787 | rt_set_errno(result); |
| 788 | |
| 789 | return NULL; |
| 790 | } |
| 791 | |
| 792 | d->num = result; |
| 793 | d->cur = 0; /* current entry index */ |
| 794 | } |
| 795 | |
| 796 | return (struct dirent *)(d->buf + d->cur); |
| 797 | } |
| 798 | RTM_EXPORT(readdir); |
| 799 | |
| 800 | /** |