/* 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. */
struct dirent* readdir(DIR *d)
/* 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. */
struct dirent* readdir(DIR *d)
{
    int result;
    struct dfs_file *fd;
    fd = fd_get(d->fd);
    if (fd == NULL)
    {
        rt_set_errno(-EBADF);
        return NULL;
    }
    if (d->num)
    {
        struct dirent *dirent_ptr;
        dirent_ptr = (struct dirent *)&d->buf[d->cur];
        d->cur += dirent_ptr->d_reclen;
    }
    if (!d->num || d->cur >= d->num)
    {
        result = dfs_file_getdents(fd,
                                   (struct dirent *)d->buf,
                                   sizeof(d->buf) - 1);
        if (result <= 0)
        {
            rt_set_errno(result);
            return NULL;
        }
        d->num = result;
        d->cur = 0;
    }
    return (struct dirent *)(d->buf + d->cur);
}