Remove ACE_OS::readdir_r
[ACE_TAO.git] / ACE / ace / OS_NS_dirent.cpp
bloba5efb466c06ea97e7390b82cdbe0bae138bdfe28
1 #include "ace/OS_NS_dirent.h"
5 #if !defined (ACE_HAS_INLINED_OSCALLS)
6 # include "ace/OS_NS_dirent.inl"
7 #endif /* ACE_HAS_INLINED_OSCALLS */
9 #include "ace/OS_NS_errno.h"
10 #include "ace/OS_NS_string.h"
11 #include "ace/Log_Category.h"
12 #include "ace/OS_NS_stdlib.h"
14 #if defined (ACE_HAS_ALLOC_HOOKS)
15 # include "ace/Malloc_Base.h"
16 #endif /* ACE_HAS_ALLOC_HOOKS */
18 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
20 #if defined (ACE_LACKS_CLOSEDIR)
21 void
22 ACE_OS::closedir_emulation (ACE_DIR *d)
24 #if defined (ACE_WIN32)
25 if (d->current_handle_ != INVALID_HANDLE_VALUE)
27 ::FindClose (d->current_handle_);
28 d->current_handle_ = INVALID_HANDLE_VALUE;
31 d->started_reading_ = 0;
32 if (d->dirent_ != 0)
34 ACE_OS::free (d->dirent_->d_name);
35 ACE_OS::free (d->dirent_);
37 #else /* ACE_WIN32 */
38 ACE_UNUSED_ARG (d);
39 #endif /* ACE_WIN32 */
41 #endif /* ACE_LACKS_CLOSEDIR */
43 #if defined (ACE_LACKS_OPENDIR)
44 ACE_DIR *
45 ACE_OS::opendir_emulation (const ACE_TCHAR *filename)
47 #if defined (ACE_WIN32)
48 # if !defined (INVALID_FILE_ATTRIBUTES)
49 # define INVALID_FILE_ATTRIBUTES 0xFFFFFFFF
50 # endif
52 ACE_DIR *dir = 0;
53 ACE_TCHAR extra[3] = {0,0,0};
55 // Check if filename is a directory.
56 DWORD fileAttribute = ACE_TEXT_GetFileAttributes (filename);
57 if (fileAttribute == INVALID_FILE_ATTRIBUTES
58 || !(fileAttribute & FILE_ATTRIBUTE_DIRECTORY))
59 return 0;
62 Note: the semantics of the win32 function FindFirstFile take the
63 basename(filename) as a pattern to be matched within the dirname(filename).
64 This is contrary to the behavior of the posix function readdir which treats
65 basename(filename) as a directory to be opened and read.
67 For this reason, we append a slash-star or backslash-star to the supplied
68 filename so the result is that FindFirstFile will do what we need.
70 According to the documentation for FindFirstFile, either a '/' or a '\' may
71 be used as a directory name separator.
73 Of course, it is necessary to ensure that this is only done if the trailing
74 filespec is not already there.
76 Phil Mesnier
79 size_t const lastchar = ACE_OS::strlen (filename);
80 if (lastchar > 0)
82 if (filename[lastchar-1] != '*')
84 if (filename[lastchar-1] != '/' && filename[lastchar-1] != '\\')
85 ACE_OS::strcpy (extra, ACE_TEXT ("/*"));
86 else
87 ACE_OS::strcpy (extra, ACE_TEXT ("*"));
91 ACE_NEW_RETURN (dir, ACE_DIR, 0);
92 ACE_NEW_RETURN (dir->directory_name_,
93 ACE_TCHAR[lastchar + ACE_OS::strlen (extra) + 1],
94 0);
95 ACE_OS::strcpy (dir->directory_name_, filename);
96 if (extra[0])
97 ACE_OS::strcat (dir->directory_name_, extra);
98 dir->current_handle_ = INVALID_HANDLE_VALUE;
99 dir->started_reading_ = 0;
100 dir->dirent_ = 0;
101 return dir;
102 #else /* WIN32 */
103 ACE_UNUSED_ARG (filename);
104 ACE_NOTSUP_RETURN (0);
105 #endif /* WIN32 */
107 #endif /* ACE_LACKS_CLOSEDIR */
109 #if defined (ACE_LACKS_READDIR)
110 struct ACE_DIRENT *
111 ACE_OS::readdir_emulation (ACE_DIR *d)
113 #if defined (ACE_WIN32)
114 if (d->dirent_ != 0)
116 ACE_OS::free (d->dirent_->d_name);
117 ACE_OS::free (d->dirent_);
118 d->dirent_ = 0;
121 if (!d->started_reading_)
123 d->current_handle_ = ACE_TEXT_FindFirstFile (d->directory_name_,
124 &d->fdata_);
125 d->started_reading_ = 1;
127 else
129 int const retval = ACE_TEXT_FindNextFile (d->current_handle_, &d->fdata_);
130 if (retval == 0)
132 // Make sure to close the handle explicitly to avoid a leak!
133 ::FindClose (d->current_handle_);
134 d->current_handle_ = INVALID_HANDLE_VALUE;
138 if (d->current_handle_ != INVALID_HANDLE_VALUE)
140 d->dirent_ = (ACE_DIRENT *)
141 ACE_OS::malloc (sizeof (ACE_DIRENT));
143 if (d->dirent_ != 0)
145 d->dirent_->d_name = (ACE_TCHAR*)
146 ACE_OS::malloc ((ACE_OS::strlen (d->fdata_.cFileName) + 1)
147 * sizeof (ACE_TCHAR));
148 ACE_OS::strcpy (d->dirent_->d_name, d->fdata_.cFileName);
149 d->dirent_->d_reclen = sizeof (ACE_DIRENT);
152 return d->dirent_;
154 else
155 return 0;
156 #else /* ACE_WIN32 */
157 ACE_UNUSED_ARG (d);
158 ACE_NOTSUP_RETURN (0);
159 #endif /* ACE_WIN32 */
161 #endif /* ACE_LACKS_READDIR */
163 #if !defined (ACE_HAS_SCANDIR)
165 ACE_OS::scandir_emulation (const ACE_TCHAR *dirname,
166 ACE_DIRENT **namelist[],
167 ACE_SCANDIR_SELECTOR selector,
168 ACE_SCANDIR_COMPARATOR comparator)
170 ACE_DIR *dirp = ACE_OS::opendir (dirname);
172 if (dirp == 0)
173 return -1;
174 // A sanity check here. "namelist" had better not be zero.
175 else if (namelist == 0)
176 return -1;
178 ACE_DIRENT **vector = 0;
179 ACE_DIRENT *dp = 0;
180 int arena_size = 0;
181 int nfiles = 0;
182 int fail = 0;
184 for (dp = ACE_OS::readdir (dirp);
185 dp != 0;
186 dp = ACE_OS::readdir (dirp))
188 if (selector && (*selector)(dp) == 0)
189 continue;
191 // If we get here, we have a dirent that the user likes.
192 if (nfiles == arena_size)
194 ACE_DIRENT **newv = 0;
195 int new_arena_size;
196 if (arena_size == 0)
197 new_arena_size = 10;
198 else
199 new_arena_size = arena_size * 2;
201 #if defined (ACE_HAS_ALLOC_HOOKS)
202 newv = (ACE_DIRENT **) ACE_Allocator::instance()->malloc (new_arena_size * sizeof (ACE_DIRENT *));
203 if (newv && vector)
205 ACE_OS::memcpy (newv, vector, arena_size * sizeof (ACE_DIRENT *));
207 #else
208 newv = (ACE_DIRENT **) ACE_OS::realloc (vector,
209 new_arena_size * sizeof (ACE_DIRENT *));
210 #endif /* ACE_HAS_ALLOC_HOOKS */
212 arena_size = new_arena_size;
214 if (newv == 0)
216 fail = 1;
217 break;
219 vector = newv;
222 #if defined (ACE_LACKS_STRUCT_DIR)
223 #if defined (ACE_HAS_ALLOC_HOOKS)
224 ACE_DIRENT *newdp = (ACE_DIRENT *) ACE_Allocator::instance()->malloc (sizeof (ACE_DIRENT));
225 #else
226 ACE_DIRENT *newdp = (ACE_DIRENT *) ACE_OS::malloc (sizeof (ACE_DIRENT));
227 #endif /* ACE_HAS_ALLOC_HOOKS */
228 #else
229 size_t dsize =
230 sizeof (ACE_DIRENT) +
231 ((ACE_OS::strlen (dp->d_name) + 1) * sizeof (ACE_TCHAR));
232 #if defined (ACE_HAS_ALLOC_HOOKS)
233 ACE_DIRENT *newdp = (ACE_DIRENT *) ACE_Allocator::instance()->malloc (dsize);
234 #else
235 ACE_DIRENT *newdp = (ACE_DIRENT *) ACE_OS::malloc (dsize);
236 #endif /* ACE_HAS_ALLOC_HOOKS */
237 #endif /* ACE_LACKS_STRUCT_DIR */
239 if (newdp == 0)
241 fail = 1;
242 break;
245 #if defined (ACE_LACKS_STRUCT_DIR)
246 #if defined (ACE_HAS_ALLOC_HOOKS)
247 newdp->d_name = (ACE_TCHAR*) ACE_Allocator::instance()->malloc ((ACE_OS::strlen (dp->d_name) + 1) * sizeof (ACE_TCHAR));
248 #else
249 newdp->d_name = (ACE_TCHAR*) ACE_OS::malloc ((ACE_OS::strlen (dp->d_name) + 1) * sizeof (ACE_TCHAR));
250 #endif /* ACE_HAS_ALLOC_HOOKS */
252 if (newdp->d_name == 0)
254 fail = 1;
255 #if defined (ACE_HAS_ALLOC_HOOKS)
256 ACE_Allocator::instance()->free (newdp);
257 #else
258 ACE_OS::free (newdp);
259 #endif /* ACE_HAS_ALLOC_HOOKS */
260 break;
263 // Don't use memcpy here since d_name is now a pointer
264 newdp->d_ino = dp->d_ino;
265 newdp->d_off = dp->d_off;
266 newdp->d_reclen = dp->d_reclen;
267 ACE_OS::strcpy (newdp->d_name, dp->d_name);
268 vector[nfiles++] = newdp;
269 #else
270 vector[nfiles++] = (ACE_DIRENT *) ACE_OS::memcpy (newdp, dp, dsize);
271 #endif /* ACE_LACKS_STRUCT_DIR */
274 if (fail)
276 ACE_OS::closedir (dirp);
277 while (vector && nfiles-- > 0)
279 #if defined (ACE_LACKS_STRUCT_DIR)
280 #if defined (ACE_HAS_ALLOC_HOOKS)
281 ACE_Allocator::instance()->free (vector[nfiles]->d_name);
282 #else
283 ACE_OS::free (vector[nfiles]->d_name);
284 #endif /* ACE_HAS_ALLOC_HOOKS */
285 #endif /* ACE_LACKS_STRUCT_DIR */
286 #if defined (ACE_HAS_ALLOC_HOOKS)
287 ACE_Allocator::instance()->free (vector[nfiles]);
288 #else
289 ACE_OS::free (vector[nfiles]);
290 #endif /* ACE_HAS_ALLOC_HOOKS */
292 #if defined (ACE_HAS_ALLOC_HOOKS)
293 ACE_Allocator::instance()->free (vector);
294 #else
295 ACE_OS::free (vector);
296 #endif /* ACE_HAS_ALLOC_HOOKS */
297 return -1;
300 ACE_OS::closedir (dirp);
302 *namelist = vector;
304 if (comparator)
305 ACE_OS::qsort (*namelist,
306 nfiles,
307 sizeof (ACE_DIRENT *),
308 (ACE_COMPARE_FUNC) comparator);
310 return nfiles;
312 #endif /* !ACE_HAS_SCANDIR */
314 ACE_END_VERSIONED_NAMESPACE_DECL