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>
14 * $Date: 2002/11/09 17:44:09 $
25 #define WIN32_LEAN_AND_MEAN
26 #include <windows.h> /* for GetFileAttributes */
34 * Returns a pointer to a DIR structure appropriately filled in to begin
35 * searching a directory.
38 opendir (const CHAR *szPath)
42 CHAR szFullPath[MAX_PATH];
52 if (szPath[0] == '\0')
58 /* Attempt to determine if the given path really is a directory. */
59 rc = GetFileAttributes (szPath);
62 /* call GetLastError for more error info */
66 if (!(rc & FILE_ATTRIBUTE_DIRECTORY))
68 /* Error, entry exists but not a directory. */
73 /* Make an absolute pathname. */
74 _fullpath (szFullPath, szPath, MAX_PATH);
76 /* Allocate enough space to store DIR structure and the complete
77 * directory path given. */
78 nd = (DIR *) malloc (sizeof (DIR) + strlen (szFullPath) + strlen (SLASH) +
83 /* Error, out of memory. */
88 /* Create the search expression. */
89 strcpy (nd->dd_name, szFullPath);
91 /* Add on a slash if the path does not end with one. */
92 if (nd->dd_name[0] != '\0' &&
93 nd->dd_name[strlen (nd->dd_name) - 1] != '/' &&
94 nd->dd_name[strlen (nd->dd_name) - 1] != '\\')
96 strcat (nd->dd_name, SLASH);
99 /* Add on the search pattern */
100 strcat (nd->dd_name, SUFFIX);
102 /* Initialize handle to -1 so that a premature closedir doesn't try
103 * to call _findclose on it. */
106 /* Initialize the status. */
109 /* Initialize the dirent structure. ino and reclen are invalid under
110 * Win32, and name simply points at the appropriate part of the
111 * findfirst_t structure. */
112 nd->dd_dir.d_ino = 0;
113 nd->dd_dir.d_reclen = 0;
114 nd->dd_dir.d_namlen = 0;
115 nd->dd_dir.d_name = nd->dd_dta.name;
124 * Return a pointer to a dirent structure filled with the information on the
125 * next entry in the directory.
132 /* Check for valid DIR struct. */
136 return (struct dirent *) 0;
139 if (dirp->dd_dir.d_name != dirp->dd_dta.name)
141 /* The structure does not seem to be set up correctly. */
143 return (struct dirent *) 0;
146 if (dirp->dd_stat < 0)
148 /* We have already returned all files in the directory
149 * (or the structure has an invalid dd_stat). */
150 return (struct dirent *) 0;
152 else if (dirp->dd_stat == 0)
154 /* We haven't started the search yet. */
155 /* Start the search */
156 dirp->dd_handle = _findfirst (dirp->dd_name, &(dirp->dd_dta));
158 if (dirp->dd_handle == -1)
160 /* Whoops! Seems there are no files in that
171 /* Get the next search entry. */
172 if (_findnext (dirp->dd_handle, &(dirp->dd_dta)))
174 /* We are off the end or otherwise error. */
175 _findclose (dirp->dd_handle);
176 dirp->dd_handle = -1;
181 /* Update the status to indicate the correct
187 if (dirp->dd_stat > 0)
189 /* Successfully got an entry. Everything about the file is
190 * already appropriately filled in except the length of the
192 dirp->dd_dir.d_namlen = strlen (dirp->dd_dir.d_name);
193 return &dirp->dd_dir;
196 return (struct dirent *) 0;
203 * Frees up resources allocated by opendir.
206 closedir (DIR * dirp)
219 if (dirp->dd_handle != -1)
221 rc = _findclose (dirp->dd_handle);
224 /* Delete the dir structure. */
233 * Return to the beginning of the directory "stream". We simply call findclose
234 * and then reset things like an opendir.
237 rewinddir (DIR * dirp)
247 if (dirp->dd_handle != -1)
249 _findclose (dirp->dd_handle);
252 dirp->dd_handle = -1;
259 * Returns the "position" in the "directory stream" which can be used with
260 * seekdir to go back to an old entry. We simply return the value in stat.
272 return dirp->dd_stat;
278 * Seek to an entry previously returned by telldir. We rewind the directory
279 * and call readdir repeatedly until either dd_stat is the position number
280 * or -1 (off the end). This is not perfect, in that the directory may
281 * have changed while we weren't looking. But that is probably the case with
285 seekdir (DIR * dirp, long lPos)
297 /* Seeking to an invalid position. */
304 if (dirp->dd_handle != -1)
306 _findclose (dirp->dd_handle);
308 dirp->dd_handle = -1;
313 /* Rewind and read forward to the appropriate index. */
316 while ((dirp->dd_stat < lPos) && readdir (dirp))