James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Dirent interface for Microsoft Visual Studio |
| 3 | * |
| 4 | * Copyright (C) 1998-2019 Toni Ronkko |
| 5 | * This file is part of dirent. Dirent may be freely distributed |
| 6 | * under the MIT license. For all details and documentation, see |
| 7 | * https://github.com/tronkko/dirent |
| 8 | */ |
| 9 | #ifndef DIRENT_H |
| 10 | #define DIRENT_H |
| 11 | |
| 12 | /* Hide warnings about unreferenced local functions */ |
| 13 | #if defined(__clang__) |
| 14 | # pragma clang diagnostic ignored "-Wunused-function" |
| 15 | #elif defined(_MSC_VER) |
| 16 | # pragma warning(disable:4505) |
| 17 | #elif defined(__GNUC__) |
| 18 | # pragma GCC diagnostic ignored "-Wunused-function" |
| 19 | #endif |
| 20 | |
| 21 | /* |
| 22 | * Include windows.h without Windows Sockets 1.1 to prevent conflicts with |
| 23 | * Windows Sockets 2.0. |
| 24 | */ |
| 25 | #ifndef WIN32_LEAN_AND_MEAN |
| 26 | # define WIN32_LEAN_AND_MEAN |
| 27 | #endif |
| 28 | #include <windows.h> |
| 29 | |
| 30 | #include <cerrno> |
| 31 | #include <cstdarg> |
| 32 | #include <cstdio> |
| 33 | #include <cstdlib> |
| 34 | #include <cstring> |
| 35 | #include <cwchar> |
| 36 | #include <sys/stat.h> |
| 37 | #include <sys/types.h> |
| 38 | |
| 39 | /* Indicates that d_type field is available in dirent structure */ |
| 40 | #define _DIRENT_HAVE_D_TYPE |
| 41 | |
| 42 | /* Indicates that d_namlen field is available in dirent structure */ |
| 43 | #define _DIRENT_HAVE_D_NAMLEN |
| 44 | |
| 45 | /* Entries missing from MSVC 6.0 */ |
| 46 | #if !defined(FILE_ATTRIBUTE_DEVICE) |
| 47 | # define FILE_ATTRIBUTE_DEVICE 0x40 |
| 48 | #endif |
| 49 | |
| 50 | /* File type and permission flags for stat(), general mask */ |
| 51 | #if !defined(S_IFMT) |
| 52 | # define S_IFMT _S_IFMT |
| 53 | #endif |
| 54 | |
| 55 | /* Directory bit */ |
| 56 | #if !defined(S_IFDIR) |
| 57 | # define S_IFDIR _S_IFDIR |
| 58 | #endif |
| 59 | |
| 60 | /* Character device bit */ |
| 61 | #if !defined(S_IFCHR) |
| 62 | # define S_IFCHR _S_IFCHR |
| 63 | #endif |
| 64 | |
| 65 | /* Pipe bit */ |
| 66 | #if !defined(S_IFFIFO) |
| 67 | # define S_IFFIFO _S_IFFIFO |
| 68 | #endif |
| 69 | |
| 70 | /* Regular file bit */ |
| 71 | #if !defined(S_IFREG) |
| 72 | # define S_IFREG _S_IFREG |
| 73 | #endif |
| 74 | |
| 75 | /* Read permission */ |
| 76 | #if !defined(S_IREAD) |
| 77 | # define S_IREAD _S_IREAD |
| 78 | #endif |
| 79 | |
| 80 | /* Write permission */ |
| 81 | #if !defined(S_IWRITE) |
| 82 | # define S_IWRITE _S_IWRITE |
| 83 | #endif |
| 84 | |
| 85 | /* Execute permission */ |
| 86 | #if !defined(S_IEXEC) |
| 87 | # define S_IEXEC _S_IEXEC |
| 88 | #endif |
| 89 | |
| 90 | /* Pipe */ |
| 91 | #if !defined(S_IFIFO) |
| 92 | # define S_IFIFO _S_IFIFO |
| 93 | #endif |
| 94 | |
| 95 | /* Block device */ |
| 96 | #if !defined(S_IFBLK) |
| 97 | # define S_IFBLK 0 |
| 98 | #endif |
| 99 | |
| 100 | /* Link */ |
| 101 | #if !defined(S_IFLNK) |
| 102 | # define S_IFLNK 0 |
| 103 | #endif |
| 104 | |
| 105 | /* Socket */ |
| 106 | #if !defined(S_IFSOCK) |
| 107 | # define S_IFSOCK 0 |
| 108 | #endif |
| 109 | |
| 110 | /* Read user permission */ |
| 111 | #if !defined(S_IRUSR) |
| 112 | # define S_IRUSR S_IREAD |
| 113 | #endif |
| 114 | |
| 115 | /* Write user permission */ |
| 116 | #if !defined(S_IWUSR) |
| 117 | # define S_IWUSR S_IWRITE |
| 118 | #endif |
| 119 | |
| 120 | /* Execute user permission */ |
| 121 | #if !defined(S_IXUSR) |
| 122 | # define S_IXUSR 0 |
| 123 | #endif |
| 124 | |
| 125 | /* Read group permission */ |
| 126 | #if !defined(S_IRGRP) |
| 127 | # define S_IRGRP 0 |
| 128 | #endif |
| 129 | |
| 130 | /* Write group permission */ |
| 131 | #if !defined(S_IWGRP) |
| 132 | # define S_IWGRP 0 |
| 133 | #endif |
| 134 | |
| 135 | /* Execute group permission */ |
| 136 | #if !defined(S_IXGRP) |
| 137 | # define S_IXGRP 0 |
| 138 | #endif |
| 139 | |
| 140 | /* Read others permission */ |
| 141 | #if !defined(S_IROTH) |
| 142 | # define S_IROTH 0 |
| 143 | #endif |
| 144 | |
| 145 | /* Write others permission */ |
| 146 | #if !defined(S_IWOTH) |
| 147 | # define S_IWOTH 0 |
| 148 | #endif |
| 149 | |
| 150 | /* Execute others permission */ |
| 151 | #if !defined(S_IXOTH) |
| 152 | # define S_IXOTH 0 |
| 153 | #endif |
| 154 | |
| 155 | /* Maximum length of file name */ |
| 156 | #if !defined(PATH_MAX) |
| 157 | # define PATH_MAX MAX_PATH |
| 158 | #endif |
| 159 | #if !defined(FILENAME_MAX) |
| 160 | # define FILENAME_MAX MAX_PATH |
| 161 | #endif |
| 162 | #if !defined(NAME_MAX) |
| 163 | # define NAME_MAX FILENAME_MAX |
| 164 | #endif |
| 165 | |
| 166 | /* File type flags for d_type */ |
| 167 | #define DT_UNKNOWN 0 |
| 168 | #define DT_REG S_IFREG |
| 169 | #define DT_DIR S_IFDIR |
| 170 | #define DT_FIFO S_IFIFO |
| 171 | #define DT_SOCK S_IFSOCK |
| 172 | #define DT_CHR S_IFCHR |
| 173 | #define DT_BLK S_IFBLK |
| 174 | #define DT_LNK S_IFLNK |
| 175 | |
| 176 | /* Macros for converting between st_mode and d_type */ |
| 177 | #define IFTODT(mode) ((mode) & S_IFMT) |
| 178 | #define DTTOIF(type) (type) |
| 179 | |
| 180 | /* |
| 181 | * File type macros. Note that block devices, sockets and links cannot be |
| 182 | * distinguished on Windows and the macros S_ISBLK, S_ISSOCK and S_ISLNK are |
| 183 | * only defined for compatibility. These macros should always return false |
| 184 | * on Windows. |
| 185 | */ |
| 186 | #if !defined(S_ISFIFO) |
| 187 | # define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO) |
| 188 | #endif |
| 189 | #if !defined(S_ISDIR) |
| 190 | # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) |
| 191 | #endif |
| 192 | #if !defined(S_ISREG) |
| 193 | # define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) |
| 194 | #endif |
| 195 | #if !defined(S_ISLNK) |
| 196 | # define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK) |
| 197 | #endif |
| 198 | #if !defined(S_ISSOCK) |
| 199 | # define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK) |
| 200 | #endif |
| 201 | #if !defined(S_ISCHR) |
| 202 | # define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR) |
| 203 | #endif |
| 204 | #if !defined(S_ISBLK) |
| 205 | # define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK) |
| 206 | #endif |
| 207 | |
| 208 | /* Return the exact length of the file name without zero terminator */ |
| 209 | #define _D_EXACT_NAMLEN(p) ((p)->d_namlen) |
| 210 | |
| 211 | /* Return the maximum size of a file name */ |
| 212 | #define _D_ALLOC_NAMLEN(p) ((PATH_MAX)+1) |
| 213 | |
| 214 | |
| 215 | #ifdef __cplusplus |
| 216 | extern "C" { |
| 217 | #endif |
| 218 | |
| 219 | |
| 220 | /* Wide-character version */ |
| 221 | struct _wdirent { |
| 222 | /* Always zero */ |
| 223 | long d_ino; |
| 224 | |
| 225 | /* File position within stream */ |
| 226 | long d_off; |
| 227 | |
| 228 | /* Structure size */ |
| 229 | unsigned short d_reclen; |
| 230 | |
| 231 | /* Length of name without \0 */ |
| 232 | size_t d_namlen; |
| 233 | |
| 234 | /* File type */ |
| 235 | int d_type; |
| 236 | |
| 237 | /* File name */ |
| 238 | wchar_t d_name[PATH_MAX+1]; |
| 239 | }; |
| 240 | typedef struct _wdirent _wdirent; |
| 241 | |
| 242 | struct _WDIR { |
| 243 | /* Current directory entry */ |
| 244 | struct _wdirent ent; |
| 245 | |
| 246 | /* Private file data */ |
| 247 | WIN32_FIND_DATAW data; |
| 248 | |
| 249 | /* True if data is valid */ |
| 250 | int cached; |
| 251 | |
| 252 | /* Win32 search handle */ |
| 253 | HANDLE handle; |
| 254 | |
| 255 | /* Initial directory name */ |
| 256 | wchar_t *patt; |
| 257 | }; |
| 258 | typedef struct _WDIR _WDIR; |
| 259 | |
| 260 | /* Multi-byte character version */ |
| 261 | struct dirent { |
| 262 | /* Always zero */ |
| 263 | long d_ino; |
| 264 | |
| 265 | /* File position within stream */ |
| 266 | long d_off; |
| 267 | |
| 268 | /* Structure size */ |
| 269 | unsigned short d_reclen; |
| 270 | |
| 271 | /* Length of name without \0 */ |
| 272 | size_t d_namlen; |
| 273 | |
| 274 | /* File type */ |
| 275 | int d_type; |
| 276 | |
| 277 | /* File name */ |
| 278 | char d_name[PATH_MAX+1]; |
| 279 | }; |
| 280 | typedef struct dirent dirent; |
| 281 | |
| 282 | struct DIR { |
| 283 | struct dirent ent; |
| 284 | struct _WDIR *wdirp; |
| 285 | }; |
| 286 | typedef struct DIR DIR; |
| 287 | |
| 288 | |
| 289 | /* Dirent functions */ |
| 290 | static DIR *opendir (const char *dirname); |
| 291 | static _WDIR *_wopendir (const wchar_t *dirname); |
| 292 | |
| 293 | static struct dirent *readdir (DIR *dirp); |
| 294 | static struct _wdirent *_wreaddir (_WDIR *dirp); |
| 295 | |
| 296 | static int readdir_r( |
| 297 | DIR *dirp, struct dirent *entry, struct dirent **result); |
| 298 | static int _wreaddir_r( |
| 299 | _WDIR *dirp, struct _wdirent *entry, struct _wdirent **result); |
| 300 | |
| 301 | static int closedir (DIR *dirp); |
| 302 | static int _wclosedir (_WDIR *dirp); |
| 303 | |
| 304 | static void rewinddir (DIR* dirp); |
| 305 | static void _wrewinddir (_WDIR* dirp); |
| 306 | |
| 307 | static int scandir (const char *dirname, struct dirent ***namelist, |
| 308 | int (*filter)(const struct dirent*), |
| 309 | int (*compare)(const struct dirent**, const struct dirent**)); |
| 310 | |
| 311 | static int alphasort (const struct dirent **a, const struct dirent **b); |
| 312 | |
| 313 | static int versionsort (const struct dirent **a, const struct dirent **b); |
| 314 | |
| 315 | |
| 316 | /* For compatibility with Symbian */ |
| 317 | #define wdirent _wdirent |
| 318 | #define WDIR _WDIR |
| 319 | #define wopendir _wopendir |
| 320 | #define wreaddir _wreaddir |
| 321 | #define wclosedir _wclosedir |
| 322 | #define wrewinddir _wrewinddir |
| 323 | |
| 324 | |
| 325 | /* Internal utility functions */ |
| 326 | static WIN32_FIND_DATAW *dirent_first (_WDIR *dirp); |
| 327 | static WIN32_FIND_DATAW *dirent_next (_WDIR *dirp); |
| 328 | |
| 329 | static int dirent_mbstowcs_s( |
| 330 | size_t *pReturnValue, |
| 331 | wchar_t *wcstr, |
| 332 | size_t sizeInWords, |
| 333 | const char *mbstr, |
| 334 | size_t count); |
| 335 | |
| 336 | static int dirent_wcstombs_s( |
| 337 | size_t *pReturnValue, |
| 338 | char *mbstr, |
| 339 | size_t sizeInBytes, |
| 340 | const wchar_t *wcstr, |
| 341 | size_t count); |
| 342 | |
| 343 | static void dirent_set_errno (int error); |
| 344 | |
| 345 | |
| 346 | /* |
| 347 | * Open directory stream DIRNAME for read and return a pointer to the |
| 348 | * internal working area that is used to retrieve individual directory |
| 349 | * entries. |
| 350 | */ |
| 351 | static _WDIR* |
| 352 | _wopendir( |
| 353 | const wchar_t *dirname) |
| 354 | { |
| 355 | _WDIR *dirp; |
| 356 | DWORD n; |
| 357 | wchar_t *p; |
| 358 | |
| 359 | /* Must have directory name */ |
| 360 | if (dirname == NULL || dirname[0] == '\0') { |
| 361 | dirent_set_errno (ENOENT); |
| 362 | return NULL; |
| 363 | } |
| 364 | |
| 365 | /* Allocate new _WDIR structure */ |
| 366 | dirp = (_WDIR*) malloc (sizeof (struct _WDIR)); |
| 367 | if (!dirp) { |
| 368 | return NULL; |
| 369 | } |
| 370 | |
| 371 | /* Reset _WDIR structure */ |
| 372 | dirp->handle = INVALID_HANDLE_VALUE; |
| 373 | dirp->patt = NULL; |
| 374 | dirp->cached = 0; |
| 375 | |
| 376 | /* |
| 377 | * Compute the length of full path plus zero terminator |
| 378 | * |
| 379 | * Note that on WinRT there's no way to convert relative paths |
| 380 | * into absolute paths, so just assume it is an absolute path. |
| 381 | */ |
| 382 | #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) |
| 383 | /* Desktop */ |
| 384 | n = GetFullPathNameW (dirname, 0, NULL, NULL); |
| 385 | #else |
| 386 | /* WinRT */ |
| 387 | n = wcslen (dirname); |
| 388 | #endif |
| 389 | |
| 390 | /* Allocate room for absolute directory name and search pattern */ |
| 391 | dirp->patt = (wchar_t*) malloc (sizeof (wchar_t) * n + 16); |
| 392 | if (dirp->patt == NULL) { |
| 393 | goto exit_closedir; |
| 394 | } |
| 395 | |
| 396 | /* |
| 397 | * Convert relative directory name to an absolute one. This |
| 398 | * allows rewinddir() to function correctly even when current |
| 399 | * working directory is changed between opendir() and rewinddir(). |
| 400 | * |
| 401 | * Note that on WinRT there's no way to convert relative paths |
| 402 | * into absolute paths, so just assume it is an absolute path. |
| 403 | */ |
| 404 | #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) |
| 405 | /* Desktop */ |
| 406 | n = GetFullPathNameW (dirname, n, dirp->patt, NULL); |
| 407 | if (n <= 0) { |
| 408 | goto exit_closedir; |
| 409 | } |
| 410 | #else |
| 411 | /* WinRT */ |
| 412 | wcsncpy_s (dirp->patt, n+1, dirname, n); |
| 413 | #endif |
| 414 | |
| 415 | /* Append search pattern \* to the directory name */ |
| 416 | p = dirp->patt + n; |
| 417 | switch (p[-1]) { |
| 418 | case '\\': |
| 419 | case '/': |
| 420 | case ':': |
| 421 | /* Directory ends in path separator, e.g. c:\temp\ */ |
| 422 | /*NOP*/; |
| 423 | break; |
| 424 | |
| 425 | default: |
| 426 | /* Directory name doesn't end in path separator */ |
| 427 | *p++ = '\\'; |
| 428 | } |
| 429 | *p++ = '*'; |
| 430 | *p = '\0'; |
| 431 | |
| 432 | /* Open directory stream and retrieve the first entry */ |
| 433 | if (!dirent_first (dirp)) { |
| 434 | goto exit_closedir; |
| 435 | } |
| 436 | |
| 437 | /* Success */ |
| 438 | return dirp; |
| 439 | |
| 440 | /* Failure */ |
| 441 | exit_closedir: |
| 442 | _wclosedir (dirp); |
| 443 | return NULL; |
| 444 | } |
| 445 | |
| 446 | /* |
| 447 | * Read next directory entry. |
| 448 | * |
| 449 | * Returns pointer to static directory entry which may be overwritten by |
| 450 | * subsequent calls to _wreaddir(). |
| 451 | */ |
| 452 | static struct _wdirent* |
| 453 | _wreaddir( |
| 454 | _WDIR *dirp) |
| 455 | { |
| 456 | struct _wdirent *entry; |
| 457 | |
| 458 | /* |
| 459 | * Read directory entry to buffer. We can safely ignore the return value |
| 460 | * as entry will be set to NULL in case of error. |
| 461 | */ |
| 462 | (void) _wreaddir_r (dirp, &dirp->ent, &entry); |
| 463 | |
| 464 | /* Return pointer to statically allocated directory entry */ |
| 465 | return entry; |
| 466 | } |
| 467 | |
| 468 | /* |
| 469 | * Read next directory entry. |
| 470 | * |
| 471 | * Returns zero on success. If end of directory stream is reached, then sets |
| 472 | * result to NULL and returns zero. |
| 473 | */ |
| 474 | static int |
| 475 | _wreaddir_r( |
| 476 | _WDIR *dirp, |
| 477 | struct _wdirent *entry, |
| 478 | struct _wdirent **result) |
| 479 | { |
| 480 | WIN32_FIND_DATAW *datap; |
| 481 | |
| 482 | /* Read next directory entry */ |
| 483 | datap = dirent_next (dirp); |
| 484 | if (datap) { |
| 485 | size_t n; |
| 486 | DWORD attr; |
| 487 | |
| 488 | /* |
| 489 | * Copy file name as wide-character string. If the file name is too |
| 490 | * long to fit in to the destination buffer, then truncate file name |
| 491 | * to PATH_MAX characters and zero-terminate the buffer. |
| 492 | */ |
| 493 | n = 0; |
| 494 | while (n < PATH_MAX && datap->cFileName[n] != 0) { |
| 495 | entry->d_name[n] = datap->cFileName[n]; |
| 496 | n++; |
| 497 | } |
| 498 | entry->d_name[n] = 0; |
| 499 | |
| 500 | /* Length of file name excluding zero terminator */ |
| 501 | entry->d_namlen = n; |
| 502 | |
| 503 | /* File type */ |
| 504 | attr = datap->dwFileAttributes; |
| 505 | if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) { |
| 506 | entry->d_type = DT_CHR; |
| 507 | } else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) { |
| 508 | entry->d_type = DT_DIR; |
| 509 | } else { |
| 510 | entry->d_type = DT_REG; |
| 511 | } |
| 512 | |
| 513 | /* Reset dummy fields */ |
| 514 | entry->d_ino = 0; |
| 515 | entry->d_off = 0; |
| 516 | entry->d_reclen = sizeof (struct _wdirent); |
| 517 | |
| 518 | /* Set result address */ |
| 519 | *result = entry; |
| 520 | |
| 521 | } else { |
| 522 | |
| 523 | /* Return NULL to indicate end of directory */ |
| 524 | *result = NULL; |
| 525 | |
| 526 | } |
| 527 | |
| 528 | return /*OK*/0; |
| 529 | } |
| 530 | |
| 531 | /* |
| 532 | * Close directory stream opened by opendir() function. This invalidates the |
| 533 | * DIR structure as well as any directory entry read previously by |
| 534 | * _wreaddir(). |
| 535 | */ |
| 536 | static int |
| 537 | _wclosedir( |
| 538 | _WDIR *dirp) |
| 539 | { |
| 540 | int ok; |
| 541 | if (dirp) { |
| 542 | |
| 543 | /* Release search handle */ |
| 544 | if (dirp->handle != INVALID_HANDLE_VALUE) { |
| 545 | FindClose (dirp->handle); |
| 546 | } |
| 547 | |
| 548 | /* Release search pattern */ |
| 549 | free (dirp->patt); |
| 550 | |
| 551 | /* Release directory structure */ |
| 552 | free (dirp); |
| 553 | ok = /*success*/0; |
| 554 | |
| 555 | } else { |
| 556 | |
| 557 | /* Invalid directory stream */ |
| 558 | dirent_set_errno (EBADF); |
| 559 | ok = /*failure*/-1; |
| 560 | |
| 561 | } |
| 562 | return ok; |
| 563 | } |
| 564 | |
| 565 | /* |
| 566 | * Rewind directory stream such that _wreaddir() returns the very first |
| 567 | * file name again. |
| 568 | */ |
| 569 | static void |
| 570 | _wrewinddir( |
| 571 | _WDIR* dirp) |
| 572 | { |
| 573 | if (dirp) { |
| 574 | /* Release existing search handle */ |
| 575 | if (dirp->handle != INVALID_HANDLE_VALUE) { |
| 576 | FindClose (dirp->handle); |
| 577 | } |
| 578 | |
| 579 | /* Open new search handle */ |
| 580 | dirent_first (dirp); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | /* Get first directory entry (internal) */ |
| 585 | static WIN32_FIND_DATAW* |
| 586 | dirent_first( |
| 587 | _WDIR *dirp) |
| 588 | { |
| 589 | WIN32_FIND_DATAW *datap; |
| 590 | DWORD error; |
| 591 | |
| 592 | /* Open directory and retrieve the first entry */ |
| 593 | dirp->handle = FindFirstFileExW( |
| 594 | dirp->patt, FindExInfoStandard, &dirp->data, |
| 595 | FindExSearchNameMatch, NULL, 0); |
| 596 | if (dirp->handle != INVALID_HANDLE_VALUE) { |
| 597 | |
| 598 | /* a directory entry is now waiting in memory */ |
| 599 | datap = &dirp->data; |
| 600 | dirp->cached = 1; |
| 601 | |
| 602 | } else { |
| 603 | |
| 604 | /* Failed to open directory: no directory entry in memory */ |
| 605 | dirp->cached = 0; |
| 606 | datap = NULL; |
| 607 | |
| 608 | /* Set error code */ |
| 609 | error = GetLastError (); |
| 610 | switch (error) { |
| 611 | case ERROR_ACCESS_DENIED: |
| 612 | /* No read access to directory */ |
| 613 | dirent_set_errno (EACCES); |
| 614 | break; |
| 615 | |
| 616 | case ERROR_DIRECTORY: |
| 617 | /* Directory name is invalid */ |
| 618 | dirent_set_errno (ENOTDIR); |
| 619 | break; |
| 620 | |
| 621 | case ERROR_PATH_NOT_FOUND: |
| 622 | default: |
| 623 | /* Cannot find the file */ |
| 624 | dirent_set_errno (ENOENT); |
| 625 | } |
| 626 | |
| 627 | } |
| 628 | return datap; |
| 629 | } |
| 630 | |
| 631 | /* |
| 632 | * Get next directory entry (internal). |
| 633 | * |
| 634 | * Returns |
| 635 | */ |
| 636 | static WIN32_FIND_DATAW* |
| 637 | dirent_next( |
| 638 | _WDIR *dirp) |
| 639 | { |
| 640 | WIN32_FIND_DATAW *p; |
| 641 | |
| 642 | /* Get next directory entry */ |
| 643 | if (dirp->cached != 0) { |
| 644 | |
| 645 | /* A valid directory entry already in memory */ |
| 646 | p = &dirp->data; |
| 647 | dirp->cached = 0; |
| 648 | |
| 649 | } else if (dirp->handle != INVALID_HANDLE_VALUE) { |
| 650 | |
| 651 | /* Get the next directory entry from stream */ |
| 652 | if (FindNextFileW (dirp->handle, &dirp->data) != FALSE) { |
| 653 | /* Got a file */ |
| 654 | p = &dirp->data; |
| 655 | } else { |
| 656 | /* The very last entry has been processed or an error occurred */ |
| 657 | FindClose (dirp->handle); |
| 658 | dirp->handle = INVALID_HANDLE_VALUE; |
| 659 | p = NULL; |
| 660 | } |
| 661 | |
| 662 | } else { |
| 663 | |
| 664 | /* End of directory stream reached */ |
| 665 | p = NULL; |
| 666 | |
| 667 | } |
| 668 | |
| 669 | return p; |
| 670 | } |
| 671 | |
| 672 | /* |
| 673 | * Open directory stream using plain old C-string. |
| 674 | */ |
| 675 | static DIR* |
| 676 | opendir( |
| 677 | const char *dirname) |
| 678 | { |
| 679 | struct DIR *dirp; |
| 680 | |
| 681 | /* Must have directory name */ |
| 682 | if (dirname == NULL || dirname[0] == '\0') { |
| 683 | dirent_set_errno (ENOENT); |
| 684 | return NULL; |
| 685 | } |
| 686 | |
| 687 | /* Allocate memory for DIR structure */ |
| 688 | dirp = (DIR*) malloc (sizeof (struct DIR)); |
| 689 | if (!dirp) { |
| 690 | return NULL; |
| 691 | } |
| 692 | { |
| 693 | int error; |
| 694 | wchar_t wname[PATH_MAX + 1]; |
| 695 | size_t n; |
| 696 | |
| 697 | /* Convert directory name to wide-character string */ |
| 698 | error = dirent_mbstowcs_s( |
| 699 | &n, wname, PATH_MAX + 1, dirname, PATH_MAX + 1); |
| 700 | if (error) { |
| 701 | /* |
| 702 | * Cannot convert file name to wide-character string. This |
| 703 | * occurs if the string contains invalid multi-byte sequences or |
| 704 | * the output buffer is too small to contain the resulting |
| 705 | * string. |
| 706 | */ |
| 707 | goto exit_free; |
| 708 | } |
| 709 | |
| 710 | |
| 711 | /* Open directory stream using wide-character name */ |
| 712 | dirp->wdirp = _wopendir (wname); |
| 713 | if (!dirp->wdirp) { |
| 714 | goto exit_free; |
| 715 | } |
| 716 | |
| 717 | } |
| 718 | |
| 719 | /* Success */ |
| 720 | return dirp; |
| 721 | |
| 722 | /* Failure */ |
| 723 | exit_free: |
| 724 | free (dirp); |
| 725 | return NULL; |
| 726 | } |
| 727 | |
| 728 | /* |
| 729 | * Read next directory entry. |
| 730 | */ |
| 731 | static struct dirent* |
| 732 | readdir( |
| 733 | DIR *dirp) |
| 734 | { |
| 735 | struct dirent *entry; |
| 736 | |
| 737 | /* |
| 738 | * Read directory entry to buffer. We can safely ignore the return value |
| 739 | * as entry will be set to NULL in case of error. |
| 740 | */ |
| 741 | (void) readdir_r (dirp, &dirp->ent, &entry); |
| 742 | |
| 743 | /* Return pointer to statically allocated directory entry */ |
| 744 | return entry; |
| 745 | } |
| 746 | |
| 747 | /* |
| 748 | * Read next directory entry into called-allocated buffer. |
| 749 | * |
| 750 | * Returns zero on success. If the end of directory stream is reached, then |
| 751 | * sets result to NULL and returns zero. |
| 752 | */ |
| 753 | static int |
| 754 | readdir_r( |
| 755 | DIR *dirp, |
| 756 | struct dirent *entry, |
| 757 | struct dirent **result) |
| 758 | { |
| 759 | WIN32_FIND_DATAW *datap; |
| 760 | |
| 761 | /* Read next directory entry */ |
| 762 | datap = dirent_next (dirp->wdirp); |
| 763 | if (datap) { |
| 764 | size_t n; |
| 765 | int error; |
| 766 | |
| 767 | /* Attempt to convert file name to multi-byte string */ |
| 768 | error = dirent_wcstombs_s( |
| 769 | &n, entry->d_name, PATH_MAX + 1, datap->cFileName, PATH_MAX + 1); |
| 770 | |
| 771 | /* |
| 772 | * If the file name cannot be represented by a multi-byte string, |
| 773 | * then attempt to use old 8+3 file name. This allows traditional |
| 774 | * Unix-code to access some file names despite of unicode |
| 775 | * characters, although file names may seem unfamiliar to the user. |
| 776 | * |
| 777 | * Be ware that the code below cannot come up with a short file |
| 778 | * name unless the file system provides one. At least |
| 779 | * VirtualBox shared folders fail to do this. |
| 780 | */ |
| 781 | if (error && datap->cAlternateFileName[0] != '\0') { |
| 782 | error = dirent_wcstombs_s( |
| 783 | &n, entry->d_name, PATH_MAX + 1, |
| 784 | datap->cAlternateFileName, PATH_MAX + 1); |
| 785 | } |
| 786 | |
| 787 | if (!error) { |
| 788 | DWORD attr; |
| 789 | |
| 790 | /* Length of file name excluding zero terminator */ |
| 791 | entry->d_namlen = n - 1; |
| 792 | |
| 793 | /* File attributes */ |
| 794 | attr = datap->dwFileAttributes; |
| 795 | if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) { |
| 796 | entry->d_type = DT_CHR; |
| 797 | } else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) { |
| 798 | entry->d_type = DT_DIR; |
| 799 | } else { |
| 800 | entry->d_type = DT_REG; |
| 801 | } |
| 802 | |
| 803 | /* Reset dummy fields */ |
| 804 | entry->d_ino = 0; |
| 805 | entry->d_off = 0; |
| 806 | entry->d_reclen = sizeof (struct dirent); |
| 807 | |
| 808 | } else { |
| 809 | |
| 810 | /* |
| 811 | * Cannot convert file name to multi-byte string so construct |
| 812 | * an erroneous directory entry and return that. Note that |
| 813 | * we cannot return NULL as that would stop the processing |
| 814 | * of directory entries completely. |
| 815 | */ |
| 816 | entry->d_name[0] = '?'; |
| 817 | entry->d_name[1] = '\0'; |
| 818 | entry->d_namlen = 1; |
| 819 | entry->d_type = DT_UNKNOWN; |
| 820 | entry->d_ino = 0; |
| 821 | entry->d_off = -1; |
| 822 | entry->d_reclen = 0; |
| 823 | |
| 824 | } |
| 825 | |
| 826 | /* Return pointer to directory entry */ |
| 827 | *result = entry; |
| 828 | |
| 829 | } else { |
| 830 | |
| 831 | /* No more directory entries */ |
| 832 | *result = NULL; |
| 833 | |
| 834 | } |
| 835 | |
| 836 | return /*OK*/0; |
| 837 | } |
| 838 | |
| 839 | /* |
| 840 | * Close directory stream. |
| 841 | */ |
| 842 | static int |
| 843 | closedir( |
| 844 | DIR *dirp) |
| 845 | { |
| 846 | int ok; |
| 847 | if (dirp) { |
| 848 | |
| 849 | /* Close wide-character directory stream */ |
| 850 | ok = _wclosedir (dirp->wdirp); |
| 851 | dirp->wdirp = NULL; |
| 852 | |
| 853 | /* Release multi-byte character version */ |
| 854 | free (dirp); |
| 855 | |
| 856 | } else { |
| 857 | |
| 858 | /* Invalid directory stream */ |
| 859 | dirent_set_errno (EBADF); |
| 860 | ok = /*failure*/-1; |
| 861 | |
| 862 | } |
| 863 | return ok; |
| 864 | } |
| 865 | |
| 866 | /* |
| 867 | * Rewind directory stream to beginning. |
| 868 | */ |
| 869 | static void |
| 870 | rewinddir( |
| 871 | DIR* dirp) |
| 872 | { |
| 873 | /* Rewind wide-character string directory stream */ |
| 874 | _wrewinddir (dirp->wdirp); |
| 875 | } |
| 876 | |
| 877 | /* |
| 878 | * Scan directory for entries. |
| 879 | */ |
| 880 | static int |
| 881 | scandir( |
| 882 | const char *dirname, |
| 883 | struct dirent ***namelist, |
| 884 | int (*filter)(const struct dirent*), |
| 885 | int (*compare)(const struct dirent**, const struct dirent**)) |
| 886 | { |
| 887 | struct dirent **files = NULL; |
| 888 | size_t size = 0; |
| 889 | size_t allocated = 0; |
| 890 | const size_t init_size = 1; |
| 891 | DIR *dir = NULL; |
| 892 | struct dirent *entry; |
| 893 | struct dirent *tmp = NULL; |
| 894 | size_t i; |
| 895 | int result = 0; |
| 896 | |
| 897 | /* Open directory stream */ |
| 898 | dir = opendir (dirname); |
| 899 | if (dir) { |
| 900 | |
| 901 | /* Read directory entries to memory */ |
| 902 | while (1) { |
| 903 | |
| 904 | /* Enlarge pointer table to make room for another pointer */ |
| 905 | if (size >= allocated) { |
| 906 | void *p; |
| 907 | size_t num_entries; |
| 908 | |
| 909 | /* Compute number of entries in the enlarged pointer table */ |
| 910 | if (size < init_size) { |
| 911 | /* Allocate initial pointer table */ |
| 912 | num_entries = init_size; |
| 913 | } else { |
| 914 | /* Double the size */ |
| 915 | num_entries = size * 2; |
| 916 | } |
| 917 | |
| 918 | /* Allocate first pointer table or enlarge existing table */ |
| 919 | p = realloc (files, sizeof (void*) * num_entries); |
| 920 | if (p != NULL) { |
| 921 | /* Got the memory */ |
| 922 | files = (dirent**) p; |
| 923 | allocated = num_entries; |
| 924 | } else { |
| 925 | /* Out of memory */ |
| 926 | result = -1; |
| 927 | break; |
| 928 | } |
| 929 | |
| 930 | } |
| 931 | |
| 932 | /* Allocate room for temporary directory entry */ |
| 933 | if (tmp == NULL) { |
| 934 | tmp = (struct dirent*) malloc (sizeof (struct dirent)); |
| 935 | if (tmp == NULL) { |
| 936 | /* Cannot allocate temporary directory entry */ |
| 937 | result = -1; |
| 938 | break; |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | /* Read directory entry to temporary area */ |
| 943 | if (readdir_r (dir, tmp, &entry) == /*OK*/0) { |
| 944 | |
| 945 | /* Did we get an entry? */ |
| 946 | if (entry != NULL) { |
| 947 | int pass; |
| 948 | |
| 949 | /* Determine whether to include the entry in result */ |
| 950 | if (filter) { |
| 951 | /* Let the filter function decide */ |
| 952 | pass = filter (tmp); |
| 953 | } else { |
| 954 | /* No filter function, include everything */ |
| 955 | pass = 1; |
| 956 | } |
| 957 | |
| 958 | if (pass) { |
| 959 | /* Store the temporary entry to pointer table */ |
| 960 | files[size++] = tmp; |
| 961 | tmp = NULL; |
| 962 | |
| 963 | /* Keep up with the number of files */ |
| 964 | result++; |
| 965 | } |
| 966 | |
| 967 | } else { |
| 968 | |
| 969 | /* |
| 970 | * End of directory stream reached => sort entries and |
| 971 | * exit. |
| 972 | */ |
| 973 | qsort (files, size, sizeof (void*), |
| 974 | (int (*) (const void*, const void*)) compare); |
| 975 | break; |
| 976 | |
| 977 | } |
| 978 | |
| 979 | } else { |
| 980 | /* Error reading directory entry */ |
| 981 | result = /*Error*/ -1; |
| 982 | break; |
| 983 | } |
| 984 | |
| 985 | } |
| 986 | |
| 987 | } else { |
| 988 | /* Cannot open directory */ |
| 989 | result = /*Error*/ -1; |
| 990 | } |
| 991 | |
| 992 | /* Release temporary directory entry */ |
| 993 | free (tmp); |
| 994 | |
| 995 | /* Release allocated memory on error */ |
| 996 | if (result < 0) { |
| 997 | for (i = 0; i < size; i++) { |
| 998 | free (files[i]); |
| 999 | } |
| 1000 | free (files); |
| 1001 | files = NULL; |
| 1002 | } |
| 1003 | |
| 1004 | /* Close directory stream */ |
| 1005 | if (dir) { |
| 1006 | closedir (dir); |
| 1007 | } |
| 1008 | |
| 1009 | /* Pass pointer table to caller */ |
| 1010 | if (namelist) { |
| 1011 | *namelist = files; |
| 1012 | } |
| 1013 | return result; |
| 1014 | } |
| 1015 | |
| 1016 | /* Alphabetical sorting */ |
| 1017 | static int |
| 1018 | alphasort( |
| 1019 | const struct dirent **a, const struct dirent **b) |
| 1020 | { |
| 1021 | return strcoll ((*a)->d_name, (*b)->d_name); |
| 1022 | } |
| 1023 | |
| 1024 | /* Sort versions */ |
| 1025 | static int |
| 1026 | versionsort( |
| 1027 | const struct dirent **a, const struct dirent **b) |
| 1028 | { |
| 1029 | /* FIXME: implement strverscmp and use that */ |
| 1030 | return alphasort (a, b); |
| 1031 | } |
| 1032 | |
| 1033 | /* Convert multi-byte string to wide character string */ |
| 1034 | static int |
| 1035 | dirent_mbstowcs_s( |
| 1036 | size_t *pReturnValue, |
| 1037 | wchar_t *wcstr, |
| 1038 | size_t sizeInWords, |
| 1039 | const char *mbstr, |
| 1040 | size_t count) |
| 1041 | { |
| 1042 | int error; |
| 1043 | |
| 1044 | #if defined(_MSC_VER) && _MSC_VER >= 1400 |
| 1045 | |
| 1046 | /* Microsoft Visual Studio 2005 or later */ |
| 1047 | error = mbstowcs_s (pReturnValue, wcstr, sizeInWords, mbstr, count); |
| 1048 | |
| 1049 | #else |
| 1050 | |
| 1051 | /* Older Visual Studio or non-Microsoft compiler */ |
| 1052 | size_t n; |
| 1053 | |
| 1054 | /* Convert to wide-character string (or count characters) */ |
| 1055 | n = mbstowcs (wcstr, mbstr, sizeInWords); |
| 1056 | if (!wcstr || n < count) { |
| 1057 | |
| 1058 | /* Zero-terminate output buffer */ |
| 1059 | if (wcstr && sizeInWords) { |
| 1060 | if (n >= sizeInWords) { |
| 1061 | n = sizeInWords - 1; |
| 1062 | } |
| 1063 | wcstr[n] = 0; |
| 1064 | } |
| 1065 | |
| 1066 | /* Length of resulting multi-byte string WITH zero terminator */ |
| 1067 | if (pReturnValue) { |
| 1068 | *pReturnValue = n + 1; |
| 1069 | } |
| 1070 | |
| 1071 | /* Success */ |
| 1072 | error = 0; |
| 1073 | |
| 1074 | } else { |
| 1075 | |
| 1076 | /* Could not convert string */ |
| 1077 | error = 1; |
| 1078 | |
| 1079 | } |
| 1080 | |
| 1081 | #endif |
| 1082 | return error; |
| 1083 | } |
| 1084 | |
| 1085 | /* Convert wide-character string to multi-byte string */ |
| 1086 | static int |
| 1087 | dirent_wcstombs_s( |
| 1088 | size_t *pReturnValue, |
| 1089 | char *mbstr, |
| 1090 | size_t sizeInBytes, /* max size of mbstr */ |
| 1091 | const wchar_t *wcstr, |
| 1092 | size_t count) |
| 1093 | { |
| 1094 | int error; |
| 1095 | |
| 1096 | #if defined(_MSC_VER) && _MSC_VER >= 1400 |
| 1097 | |
| 1098 | /* Microsoft Visual Studio 2005 or later */ |
| 1099 | error = wcstombs_s (pReturnValue, mbstr, sizeInBytes, wcstr, count); |
| 1100 | |
| 1101 | #else |
| 1102 | |
| 1103 | /* Older Visual Studio or non-Microsoft compiler */ |
| 1104 | size_t n; |
| 1105 | |
| 1106 | /* Convert to multi-byte string (or count the number of bytes needed) */ |
| 1107 | n = wcstombs (mbstr, wcstr, sizeInBytes); |
| 1108 | if (!mbstr || n < count) { |
| 1109 | |
| 1110 | /* Zero-terminate output buffer */ |
| 1111 | if (mbstr && sizeInBytes) { |
| 1112 | if (n >= sizeInBytes) { |
| 1113 | n = sizeInBytes - 1; |
| 1114 | } |
| 1115 | mbstr[n] = '\0'; |
| 1116 | } |
| 1117 | |
| 1118 | /* Length of resulting multi-bytes string WITH zero-terminator */ |
| 1119 | if (pReturnValue) { |
| 1120 | *pReturnValue = n + 1; |
| 1121 | } |
| 1122 | |
| 1123 | /* Success */ |
| 1124 | error = 0; |
| 1125 | |
| 1126 | } else { |
| 1127 | |
| 1128 | /* Cannot convert string */ |
| 1129 | error = 1; |
| 1130 | |
| 1131 | } |
| 1132 | |
| 1133 | #endif |
| 1134 | return error; |
| 1135 | } |
| 1136 | |
| 1137 | /* Set errno variable */ |
| 1138 | static void |
| 1139 | dirent_set_errno( |
| 1140 | int error) |
| 1141 | { |
| 1142 | #if defined(_MSC_VER) && _MSC_VER >= 1400 |
| 1143 | |
| 1144 | /* Microsoft Visual Studio 2005 and later */ |
| 1145 | _set_errno (error); |
| 1146 | |
| 1147 | #else |
| 1148 | |
| 1149 | /* Non-Microsoft compiler or older Microsoft compiler */ |
| 1150 | errno = error; |
| 1151 | |
| 1152 | #endif |
| 1153 | } |
| 1154 | |
| 1155 | |
| 1156 | #ifdef __cplusplus |
| 1157 | } |
| 1158 | #endif |
| 1159 | #endif /*DIRENT_H*/ |