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