vs/include/dirent.h

Sun, 10 Nov 2024 14:06:03 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 10 Nov 2024 14:06:03 +0100
changeset 78
b3d267334140
parent 75
ba4bc497c6a7
permissions
-rw-r--r--

improve the dist Makefile target

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

mercurial