4 * Derived from DIRLIB.C by Matt J. Weinstein
5 * This note appears in the DIRLIB.H
6 * DIRLIB.H by M. J. Weinstein Released to public domain 1-Jan-89
8 * Updated by Jeremy Bettis <jeremy@hksys.com>
9 * Significantly revised and rewinddir, seekdir and telldir added by Colin
10 * Peters <colin@fu.is.saga-u.ac.jp>
12 * Resource leaks fixed by <steve.lhomme@free.fr>
28 #define SUFFIX _T("*")
29 #define SLASH _T("\\")
33 #define WIN32_LEAN_AND_MEAN
34 #include <windows.h> /* for GetFileAttributes */
39 * Returns a pointer to a DIR structure appropriately filled in to begin
40 * searching a directory.
43 _topendir (const _TCHAR * szPath)
47 _TCHAR szFullPath[MAX_PATH];
56 if (szPath[0] == _T ('\0')) {
61 /* Attempt to determine if the given path really is a directory. */
62 rc = GetFileAttributes (szPath);
63 if (rc == (unsigned int) -1) {
64 /* call GetLastError for more error info */
68 if (!(rc & FILE_ATTRIBUTE_DIRECTORY)) {
69 /* Error, entry exists but not a directory. */
74 /* Make an absolute pathname. */
75 _tfullpath (szFullPath, szPath, MAX_PATH);
77 /* Allocate enough space to store DIR structure and the complete
78 * directory path given. */
79 nd = (_TDIR *) malloc (sizeof (_TDIR) + (_tcslen (szFullPath) +
80 _tcslen (SLASH) + _tcslen (SUFFIX) + 1) * sizeof (_TCHAR));
83 /* Error, out of memory. */
88 /* Create the search expression. */
89 _tcscpy (nd->dd_name, szFullPath);
91 /* Add on a slash if the path does not end with one. */
92 if (nd->dd_name[0] != _T ('\0') &&
93 nd->dd_name[_tcslen (nd->dd_name) - 1] != _T ('/') &&
94 nd->dd_name[_tcslen (nd->dd_name) - 1] != _T ('\\')) {
95 _tcscat (nd->dd_name, SLASH);
98 /* Add on the search pattern */
99 _tcscat (nd->dd_name, SUFFIX);
101 /* Initialize handle to -1 so that a premature closedir doesn't try
102 * to call _findclose on it. */
105 /* Initialize the status. */
108 /* Initialize the dirent structure. ino and reclen are invalid under
109 * Win32, and name simply points at the appropriate part of the
110 * findfirst_t structure. */
111 nd->dd_dir.d_ino = 0;
112 nd->dd_dir.d_reclen = 0;
113 nd->dd_dir.d_namlen = 0;
114 // Added by jcsston 02/04/2004, memset was writing to a bad pointer
115 nd->dd_dir.d_name = malloc (FILENAME_MAX);
117 memset (nd->dd_dir.d_name, 0, FILENAME_MAX);
126 * Return a pointer to a dirent structure filled with the information on the
127 * next entry in the directory.
130 _treaddir (_TDIR * dirp)
134 /* Check for valid DIR struct. */
137 return (struct _tdirent *) 0;
140 if (dirp->dd_stat < 0) {
141 /* We have already returned all files in the directory
142 * (or the structure has an invalid dd_stat). */
143 return (struct _tdirent *) 0;
144 } else if (dirp->dd_stat == 0) {
145 /* We haven't started the search yet. */
146 /* Start the search */
147 dirp->dd_handle = _tfindfirst (dirp->dd_name, &(dirp->dd_dta));
149 if (dirp->dd_handle == -1) {
150 /* Whoops! Seems there are no files in that
157 /* Get the next search entry. */
158 if (_tfindnext (dirp->dd_handle, &(dirp->dd_dta))) {
159 /* We are off the end or otherwise error.
160 _findnext sets errno to ENOENT if no more file
162 DWORD winerr = GetLastError ();
164 if (winerr == ERROR_NO_MORE_FILES)
166 _findclose (dirp->dd_handle);
167 dirp->dd_handle = -1;
170 /* Update the status to indicate the correct
176 if (dirp->dd_stat > 0) {
177 /* Successfully got an entry. Everything about the file is
178 * already appropriately filled in except the length of the
180 dirp->dd_dir.d_namlen = _tcslen (dirp->dd_dta.name);
181 _tcscpy (dirp->dd_dir.d_name, dirp->dd_dta.name);
182 return &dirp->dd_dir;
185 return (struct _tdirent *) 0;
192 * Frees up resources allocated by opendir.
195 _tclosedir (_TDIR * dirp)
207 if (dirp->dd_handle != -1) {
208 rc = _findclose (dirp->dd_handle);
211 if (dirp->dd_dir.d_name)
212 free (dirp->dd_dir.d_name);
214 /* Delete the dir structure. */
223 * Return to the beginning of the directory "stream". We simply call findclose
224 * and then reset things like an opendir.
227 _trewinddir (_TDIR * dirp)
236 if (dirp->dd_handle != -1) {
237 _findclose (dirp->dd_handle);
240 dirp->dd_handle = -1;
247 * Returns the "position" in the "directory stream" which can be used with
248 * seekdir to go back to an old entry. We simply return the value in stat.
251 _ttelldir (_TDIR * dirp)
259 return dirp->dd_stat;
265 * Seek to an entry previously returned by telldir. We rewind the directory
266 * and call readdir repeatedly until either dd_stat is the position number
267 * or -1 (off the end). This is not perfect, in that the directory may
268 * have changed while we weren't looking. But that is probably the case with
272 _tseekdir (_TDIR * dirp, long lPos)
282 /* Seeking to an invalid position. */
285 } else if (lPos == -1) {
287 if (dirp->dd_handle != -1) {
288 _findclose (dirp->dd_handle);
290 dirp->dd_handle = -1;
293 /* Rewind and read forward to the appropriate index. */
296 while ((dirp->dd_stat < lPos) && _treaddir (dirp));