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/10 18:04:23 $
25 /* FIXME: anything clever to put here? */
38 #define WIN32_LEAN_AND_MEAN
39 #include <windows.h> /* for GetFileAttributes */
49 * Returns a pointer to a DIR structure appropriately filled in to begin
50 * searching a directory.
53 opendir (const CHAR *szPath)
57 CHAR szFullPath[MAX_PATH];
67 if (szPath[0] == '\0')
73 /* Attempt to determine if the given path really is a directory. */
74 rc = GetFileAttributes (szPath);
77 /* call GetLastError for more error info */
81 if (!(rc & FILE_ATTRIBUTE_DIRECTORY))
83 /* Error, entry exists but not a directory. */
88 /* Make an absolute pathname. */
89 #if defined( UNDER_CE )
90 if (szPath[0] == '\\' || szPath[0] == '/')
92 snprintf (szFullPath, MAX_PATH, "%s", szPath);
97 snprintf (szFullPath, MAX_PATH, "\\%s", szPath );
100 _fullpath (szFullPath, szPath, MAX_PATH);
103 /* Allocate enough space to store DIR structure and the complete
104 * directory path given. */
105 nd = (DIR *) malloc (sizeof (DIR) + strlen (szFullPath) + strlen (SLASH) +
110 /* Error, out of memory. */
115 /* Create the search expression. */
116 strcpy (nd->dd_name, szFullPath);
118 /* Add on a slash if the path does not end with one. */
119 if (nd->dd_name[0] != '\0' &&
120 nd->dd_name[strlen (nd->dd_name) - 1] != '/' &&
121 nd->dd_name[strlen (nd->dd_name) - 1] != '\\')
123 strcat (nd->dd_name, SLASH);
126 /* Add on the search pattern */
127 strcat (nd->dd_name, SUFFIX);
129 /* Initialize handle to -1 so that a premature closedir doesn't try
130 * to call FindClose on it. */
133 /* Initialize the status. */
136 /* Initialize the dirent structure. ino and reclen are invalid under
137 * Win32, and name simply points at the appropriate part of the
138 * findfirst_t structure. */
139 nd->dd_dir.d_ino = 0;
140 nd->dd_dir.d_reclen = 0;
141 nd->dd_dir.d_namlen = 0;
142 nd->dd_dir.d_name = nd->dd_dta.cFileName;
151 * Return a pointer to a dirent structure filled with the information on the
152 * next entry in the directory.
159 /* Check for valid DIR struct. */
163 return (struct dirent *) 0;
166 if (dirp->dd_dir.d_name != dirp->dd_dta.cFileName)
168 /* The structure does not seem to be set up correctly. */
170 return (struct dirent *) 0;
173 if (dirp->dd_stat < 0)
175 /* We have already returned all files in the directory
176 * (or the structure has an invalid dd_stat). */
177 return (struct dirent *) 0;
179 else if (dirp->dd_stat == 0)
181 /* We haven't started the search yet. */
182 /* Start the search */
183 dirp->dd_handle = FindFirstFile (dirp->dd_name, &(dirp->dd_dta));
185 if (dirp->dd_handle == -1)
187 /* Whoops! Seems there are no files in that
198 /* Get the next search entry. */
199 if (FindNextFile (dirp->dd_handle, &(dirp->dd_dta)))
201 /* We are off the end or otherwise error. */
202 FindClose (dirp->dd_handle);
203 dirp->dd_handle = -1;
208 /* Update the status to indicate the correct
214 if (dirp->dd_stat > 0)
216 /* Successfully got an entry. Everything about the file is
217 * already appropriately filled in except the length of the
219 dirp->dd_dir.d_namlen = strlen (dirp->dd_dir.d_name);
220 return &dirp->dd_dir;
223 return (struct dirent *) 0;
230 * Frees up resources allocated by opendir.
233 closedir (DIR * dirp)
246 if (dirp->dd_handle != -1)
248 rc = FindClose (dirp->dd_handle);
251 /* Delete the dir structure. */
260 * Return to the beginning of the directory "stream". We simply call findclose
261 * and then reset things like an opendir.
264 rewinddir (DIR * dirp)
274 if (dirp->dd_handle != -1)
276 FindClose (dirp->dd_handle);
279 dirp->dd_handle = -1;
286 * Returns the "position" in the "directory stream" which can be used with
287 * seekdir to go back to an old entry. We simply return the value in stat.
299 return dirp->dd_stat;
305 * Seek to an entry previously returned by telldir. We rewind the directory
306 * and call readdir repeatedly until either dd_stat is the position number
307 * or -1 (off the end). This is not perfect, in that the directory may
308 * have changed while we weren't looking. But that is probably the case with
312 seekdir (DIR * dirp, long lPos)
324 /* Seeking to an invalid position. */
331 if (dirp->dd_handle != -1)
333 FindClose (dirp->dd_handle);
335 dirp->dd_handle = -1;
340 /* Rewind and read forward to the appropriate index. */
343 while ((dirp->dd_stat < lPos) && readdir (dirp))