* Read next directory entry. * * When working with text consoles, please note that file names * returned by readdir() are represented in the default ANSI code * page while any output toconsole is typically formatted on another * code page. Thus, non-ASCII characters in file names will not usually * display correctly on console. The problem can be fixed in two ways: * (1) change the characte
| 444 | * console code page so many non-ASCII characters will display correctly. |
| 445 | */ |
| 446 | static struct dirent* |
| 447 | readdir(DIR *dirp) |
| 448 | { |
| 449 | WIN32_FIND_DATAW *datap; |
| 450 | struct dirent *entp; |
| 451 | |
| 452 | /* Read next directory entry */ |
| 453 | datap = dirent_next(dirp->wdirp); |
| 454 | if (datap) { |
| 455 | size_t n; |
| 456 | int error; |
| 457 | |
| 458 | /* Attempt to convert file name to multi-byte string */ |
| 459 | error = dirent_wcstombs_s(&n, dirp->ent.d_name, |
| 460 | PATH_MAX, datap->cFileName, PATH_MAX); |
| 461 | |
| 462 | /* |
| 463 | * If the file name cannot be represented by a multi-byte |
| 464 | * string, then attempt to use old 8+3 file name. |
| 465 | * This allows traditional Unix-code to access some file |
| 466 | * names despite of unicode characters, although file names |
| 467 | * may seem unfamiliar to the user. |
| 468 | * |
| 469 | * Be ware that the code below cannot come up with a short |
| 470 | * file name unless the file system provides one. At least |
| 471 | * VirtualBox shared folders fail to do this. |
| 472 | */ |
| 473 | if (error && datap->cAlternateFileName[0] != '\0') { |
| 474 | error = dirent_wcstombs_s( |
| 475 | &n, dirp->ent.d_name, PATH_MAX, |
| 476 | datap->cAlternateFileName, PATH_MAX); |
| 477 | } |
| 478 | |
| 479 | if (!error) { |
| 480 | DWORD attr; |
| 481 | |
| 482 | /* Initialize directory entry for return */ |
| 483 | entp = &dirp->ent; |
| 484 | |
| 485 | /* Length of file name excluding zero terminator */ |
| 486 | entp->d_namlen = n - 1; |
| 487 | |
| 488 | /* File attributes */ |
| 489 | attr = datap->dwFileAttributes; |
| 490 | if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) |
| 491 | entp->d_type = DT_CHR; |
| 492 | else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) |
| 493 | entp->d_type = DT_DIR; |
| 494 | else |
| 495 | entp->d_type = DT_REG; |
| 496 | |
| 497 | /* Reset dummy fields */ |
| 498 | entp->d_ino = 0; |
| 499 | entp->d_reclen = sizeof(struct dirent); |
| 500 | |
| 501 | } else { |
| 502 | /* |
| 503 | * Cannot convert file name to multi-byte string so |