* 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. */
| 985 | * @return the next directory entry, NULL on the end of directory or failed. |
| 986 | */ |
| 987 | struct dirent *readdir(DIR *d) |
| 988 | { |
| 989 | int result; |
| 990 | struct dirent *dirent = NULL; |
| 991 | |
| 992 | if (d == NULL) |
| 993 | { |
| 994 | rt_set_errno(-EBADF); |
| 995 | return NULL; |
| 996 | } |
| 997 | |
| 998 | do |
| 999 | { |
| 1000 | if (d->num) |
| 1001 | { |
| 1002 | struct dirent *dirent_ptr; |
| 1003 | dirent_ptr = (struct dirent *)&d->buf[d->cur]; |
| 1004 | d->cur += dirent_ptr->d_reclen; |
| 1005 | } |
| 1006 | |
| 1007 | if (!d->num || d->cur >= d->num) |
| 1008 | { |
| 1009 | /* get a new entry */ |
| 1010 | result = dfs_file_getdents(fd_get(d->fd), |
| 1011 | (struct dirent *)d->buf, |
| 1012 | sizeof(d->buf) - 1); |
| 1013 | if (result <= 0) |
| 1014 | { |
| 1015 | rt_set_errno(result); |
| 1016 | |
| 1017 | return NULL; |
| 1018 | } |
| 1019 | |
| 1020 | d->num = result; |
| 1021 | d->cur = 0; /* current entry index */ |
| 1022 | } |
| 1023 | |
| 1024 | dirent = (struct dirent *)(d->buf + d->cur); |
| 1025 | if (rt_strcmp(".", dirent->d_name) != 0 && |
| 1026 | rt_strcmp("..", dirent->d_name) != 0) |
| 1027 | { |
| 1028 | break; |
| 1029 | } |
| 1030 | } while (dirent); |
| 1031 | |
| 1032 | return dirent; |
| 1033 | } |
| 1034 | RTM_EXPORT(readdir); |
| 1035 | |
| 1036 | /** |
no test coverage detected