* Read a directory entry, and return a pointer to a dirent structure * containing the name of the entry in d_name field. Individual directory * entries returned by this very function include regular files, * sub-directories, pseudo-directories "." and "..", but also volume labels, * hidden files and system files may be returned. */
| 266 | * hidden files and system files may be returned. |
| 267 | */ |
| 268 | static struct dirent *readdir(DIR *dirp) |
| 269 | { |
| 270 | DWORD attr; |
| 271 | if (dirp == NULL) { |
| 272 | /* directory stream did not open */ |
| 273 | DIRENT_SET_ERRNO (EBADF); |
| 274 | return NULL; |
| 275 | } |
| 276 | |
| 277 | /* get next directory entry */ |
| 278 | if (dirp->cached != 0) { |
| 279 | /* a valid directory entry already in memory */ |
| 280 | dirp->cached = 0; |
| 281 | } else { |
| 282 | /* get the next directory entry from stream */ |
| 283 | if (dirp->search_handle == INVALID_HANDLE_VALUE) { |
| 284 | return NULL; |
| 285 | } |
| 286 | if (FindNextFileA (dirp->search_handle, &dirp->find_data) == FALSE) { |
| 287 | /* the very last entry has been processed or an error occured */ |
| 288 | FindClose (dirp->search_handle); |
| 289 | dirp->search_handle = INVALID_HANDLE_VALUE; |
| 290 | return NULL; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /* copy as a multibyte character string */ |
| 295 | DIRENT_STRNCPY ( dirp->curentry.d_name, |
| 296 | dirp->find_data.cFileName, |
| 297 | sizeof(dirp->curentry.d_name) ); |
| 298 | dirp->curentry.d_name[MAX_PATH] = '\0'; |
| 299 | |
| 300 | /* compute the length of name */ |
| 301 | dirp->curentry.d_namlen = strlen (dirp->curentry.d_name); |
| 302 | |
| 303 | /* determine file type */ |
| 304 | attr = dirp->find_data.dwFileAttributes; |
| 305 | if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) { |
| 306 | dirp->curentry.d_type = DT_CHR; |
| 307 | } else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) { |
| 308 | dirp->curentry.d_type = DT_DIR; |
| 309 | } else { |
| 310 | dirp->curentry.d_type = DT_REG; |
| 311 | } |
| 312 | return &dirp->curentry; |
| 313 | } |
| 314 | |
| 315 | |
| 316 | /***************************************************************************** |