| 1 |
// |
| 2 |
// 03/10/2006 James Haley |
| 3 |
// |
| 4 |
// For this module only: |
| 5 |
// This code is public domain. No change sufficient enough to constitute a |
| 6 |
// significant or original work has been made, and thus it remains as such. |
| 7 |
// |
| 8 |
// |
| 9 |
// DESCRIPTION: |
| 10 |
// |
| 11 |
// Implementation of POSIX opendir for Visual C++. |
| 12 |
// Derived from the MinGW C Library Extensions Source (released to the |
| 13 |
// public domain). As with other Win32 modules, don't include most DOOM |
| 14 |
// headers into this or conflicts will occur. |
| 15 |
// |
| 16 |
// Original Header: |
| 17 |
// |
| 18 |
// * dirent.c |
| 19 |
// * This file has no copyright assigned and is placed in the Public Domain. |
| 20 |
// * This file is a part of the mingw-runtime package. |
| 21 |
// * No warranty is given; refer to the file DISCLAIMER within the package. |
| 22 |
// * |
| 23 |
// * Derived from DIRLIB.C by Matt J. Weinstein |
| 24 |
// * This note appears in the DIRLIB.H |
| 25 |
// * DIRLIB.H by M. J. Weinstein Released to public domain 1-Jan-89 |
| 26 |
// * |
| 27 |
// * Updated by Jeremy Bettis <[email protected]> |
| 28 |
// * Significantly revised and rewinddir, seekdir and telldir added by Colin |
| 29 |
// * Peters <[email protected]> |
| 30 |
// |
| 31 |
|
| 32 |
#ifndef _MSC_VER |
| 33 |
#error i_opndir.c is for Microsoft Visual C++ only |
| 34 |
#endif |
| 35 |
|
| 36 |
#include <stdlib.h> |
| 37 |
#include <errno.h> |
| 38 |
#include <string.h> |
| 39 |
|
| 40 |
#define WIN32_LEAN_AND_MEAN |
| 41 |
#include <windows.h> /* for GetFileAttributes */ |
| 42 |
|
| 43 |
#include <tchar.h> |
| 44 |
#define SUFFIX _T("*") |
| 45 |
#define SLASH _T("\\") |
| 46 |
|
| 47 |
#include "win_opendir.h" |
| 48 |
|
| 49 |
// |
| 50 |
// opendir |
| 51 |
// |
| 52 |
// Returns a pointer to a DIR structure appropriately filled in to begin |
| 53 |
// searching a directory. |
| 54 |
// |
| 55 |
DIR *opendir(const _TCHAR *szPath) |
| 56 |
{ |
| 57 |
DIR *nd; |
| 58 |
unsigned int rc; |
| 59 |
_TCHAR szFullPath[MAX_PATH]; |
| 60 |
|
| 61 |
errno = 0; |
| 62 |
|
| 63 |
if(!szPath) |
| 64 |
{ |
| 65 |
errno = EFAULT; |
| 66 |
return (DIR *)0; |
| 67 |
} |
| 68 |
|
| 69 |
if(szPath[0] == _T('\0')) |
| 70 |
{ |
| 71 |
errno = ENOTDIR; |
| 72 |
return (DIR *)0; |
| 73 |
} |
| 74 |
|
| 75 |
/* Attempt to determine if the given path really is a directory. */ |
| 76 |
rc = GetFileAttributes(szPath); |
| 77 |
if(rc == (unsigned int)-1) |
| 78 |
{ |
| 79 |
/* call GetLastError for more error info */ |
| 80 |
errno = ENOENT; |
| 81 |
return (DIR *)0; |
| 82 |
} |
| 83 |
if(!(rc & FILE_ATTRIBUTE_DIRECTORY)) |
| 84 |
{ |
| 85 |
/* Error, entry exists but not a directory. */ |
| 86 |
errno = ENOTDIR; |
| 87 |
return (DIR *)0; |
| 88 |
} |
| 89 |
|
| 90 |
/* Make an absolute pathname. */ |
| 91 |
_tfullpath(szFullPath, szPath, MAX_PATH); |
| 92 |
|
| 93 |
/* Allocate enough space to store DIR structure and the complete |
| 94 |
* directory path given. */ |
| 95 |
nd = (DIR *)(malloc(sizeof(DIR) + (_tcslen(szFullPath) |
| 96 |
+ _tcslen(SLASH) |
| 97 |
+ _tcslen(SUFFIX) + 1) |
| 98 |
* sizeof(_TCHAR))); |
| 99 |
|
| 100 |
if(!nd) |
| 101 |
{ |
| 102 |
/* Error, out of memory. */ |
| 103 |
errno = ENOMEM; |
| 104 |
return (DIR *)0; |
| 105 |
} |
| 106 |
|
| 107 |
/* Create the search expression. */ |
| 108 |
_tcscpy(nd->dd_name, szFullPath); |
| 109 |
|
| 110 |
/* Add on a slash if the path does not end with one. */ |
| 111 |
if(nd->dd_name[0] != _T('\0') |
| 112 |
&& _tcsrchr(nd->dd_name, _T('/')) != nd->dd_name |
| 113 |
+ _tcslen(nd->dd_name) - 1 |
| 114 |
&& _tcsrchr(nd->dd_name, _T('\\')) != nd->dd_name |
| 115 |
+ _tcslen(nd->dd_name) - 1) |
| 116 |
{ |
| 117 |
_tcscat(nd->dd_name, SLASH); |
| 118 |
} |
| 119 |
|
| 120 |
/* Add on the search pattern */ |
| 121 |
_tcscat(nd->dd_name, SUFFIX); |
| 122 |
|
| 123 |
/* Initialize handle to -1 so that a premature closedir doesn't try |
| 124 |
* to call _findclose on it. */ |
| 125 |
nd->dd_handle = -1; |
| 126 |
|
| 127 |
/* Initialize the status. */ |
| 128 |
nd->dd_stat = 0; |
| 129 |
|
| 130 |
/* Initialize the dirent structure. ino and reclen are invalid under |
| 131 |
* Win32, and name simply points at the appropriate part of the |
| 132 |
* findfirst_t structure. */ |
| 133 |
nd->dd_dir.d_ino = 0; |
| 134 |
nd->dd_dir.d_reclen = 0; |
| 135 |
nd->dd_dir.d_namlen = 0; |
| 136 |
memset(nd->dd_dir.d_name, 0, FILENAME_MAX); |
| 137 |
|
| 138 |
return nd; |
| 139 |
} |
| 140 |
|
| 141 |
// |
| 142 |
// readdir |
| 143 |
// |
| 144 |
// Return a pointer to a dirent structure filled with the information on the |
| 145 |
// next entry in the directory. |
| 146 |
// |
| 147 |
struct dirent *readdir(DIR *dirp) |
| 148 |
{ |
| 149 |
errno = 0; |
| 150 |
|
| 151 |
/* Check for valid DIR struct. */ |
| 152 |
if(!dirp) |
| 153 |
{ |
| 154 |
errno = EFAULT; |
| 155 |
return (struct dirent *)0; |
| 156 |
} |
| 157 |
|
| 158 |
if (dirp->dd_stat < 0) |
| 159 |
{ |
| 160 |
/* We have already returned all files in the directory |
| 161 |
* (or the structure has an invalid dd_stat). */ |
| 162 |
return (struct dirent *)0; |
| 163 |
} |
| 164 |
else if (dirp->dd_stat == 0) |
| 165 |
{ |
| 166 |
/* We haven't started the search yet. */ |
| 167 |
/* Start the search */ |
| 168 |
dirp->dd_handle = _tfindfirst(dirp->dd_name, &(dirp->dd_dta)); |
| 169 |
|
| 170 |
if(dirp->dd_handle == -1) |
| 171 |
{ |
| 172 |
/* Whoops! Seems there are no files in that |
| 173 |
* directory. */ |
| 174 |
dirp->dd_stat = -1; |
| 175 |
} |
| 176 |
else |
| 177 |
{ |
| 178 |
dirp->dd_stat = 1; |
| 179 |
} |
| 180 |
} |
| 181 |
else |
| 182 |
{ |
| 183 |
/* Get the next search entry. */ |
| 184 |
if(_tfindnext(dirp->dd_handle, &(dirp->dd_dta))) |
| 185 |
{ |
| 186 |
/* We are off the end or otherwise error. |
| 187 |
_findnext sets errno to ENOENT if no more file |
| 188 |
Undo this. */ |
| 189 |
DWORD winerr = GetLastError(); |
| 190 |
if(winerr == ERROR_NO_MORE_FILES) |
| 191 |
errno = 0; |
| 192 |
_findclose(dirp->dd_handle); |
| 193 |
dirp->dd_handle = -1; |
| 194 |
dirp->dd_stat = -1; |
| 195 |
} |
| 196 |
else |
| 197 |
{ |
| 198 |
/* Update the status to indicate the correct |
| 199 |
* number. */ |
| 200 |
dirp->dd_stat++; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
if (dirp->dd_stat > 0) |
| 205 |
{ |
| 206 |
/* Successfully got an entry. Everything about the file is |
| 207 |
* already appropriately filled in except the length of the |
| 208 |
* file name. */ |
| 209 |
dirp->dd_dir.d_namlen = _tcslen(dirp->dd_dta.name); |
| 210 |
_tcscpy(dirp->dd_dir.d_name, dirp->dd_dta.name); |
| 211 |
return &dirp->dd_dir; |
| 212 |
} |
| 213 |
|
| 214 |
return (struct dirent *)0; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
// |
| 219 |
// closedir |
| 220 |
// |
| 221 |
// Frees up resources allocated by opendir. |
| 222 |
// |
| 223 |
int closedir(DIR *dirp) |
| 224 |
{ |
| 225 |
int rc; |
| 226 |
|
| 227 |
errno = 0; |
| 228 |
rc = 0; |
| 229 |
|
| 230 |
if(!dirp) |
| 231 |
{ |
| 232 |
errno = EFAULT; |
| 233 |
return -1; |
| 234 |
} |
| 235 |
|
| 236 |
if(dirp->dd_handle != -1) |
| 237 |
{ |
| 238 |
rc = _findclose(dirp->dd_handle); |
| 239 |
} |
| 240 |
|
| 241 |
/* Delete the dir structure. */ |
| 242 |
free(dirp); |
| 243 |
|
| 244 |
return rc; |
| 245 |
} |
| 246 |
|
| 247 |
// |
| 248 |
// rewinddir |
| 249 |
// |
| 250 |
// Return to the beginning of the directory "stream". We simply call findclose |
| 251 |
// and then reset things like an opendir. |
| 252 |
// |
| 253 |
void rewinddir(DIR * dirp) |
| 254 |
{ |
| 255 |
errno = 0; |
| 256 |
|
| 257 |
if(!dirp) |
| 258 |
{ |
| 259 |
errno = EFAULT; |
| 260 |
return; |
| 261 |
} |
| 262 |
|
| 263 |
if(dirp->dd_handle != -1) |
| 264 |
{ |
| 265 |
_findclose(dirp->dd_handle); |
| 266 |
} |
| 267 |
|
| 268 |
dirp->dd_handle = -1; |
| 269 |
dirp->dd_stat = 0; |
| 270 |
} |
| 271 |
|
| 272 |
// |
| 273 |
// telldir |
| 274 |
// |
| 275 |
// Returns the "position" in the "directory stream" which can be used with |
| 276 |
// seekdir to go back to an old entry. We simply return the value in stat. |
| 277 |
// |
| 278 |
long telldir(DIR *dirp) |
| 279 |
{ |
| 280 |
errno = 0; |
| 281 |
|
| 282 |
if(!dirp) |
| 283 |
{ |
| 284 |
errno = EFAULT; |
| 285 |
return -1; |
| 286 |
} |
| 287 |
return dirp->dd_stat; |
| 288 |
} |
| 289 |
|
| 290 |
// |
| 291 |
// seekdir |
| 292 |
// |
| 293 |
// Seek to an entry previously returned by telldir. We rewind the directory |
| 294 |
// and call readdir repeatedly until either dd_stat is the position number |
| 295 |
// or -1 (off the end). This is not perfect, in that the directory may |
| 296 |
// have changed while we weren't looking. But that is probably the case with |
| 297 |
// any such system. |
| 298 |
// |
| 299 |
void seekdir(DIR *dirp, long lPos) |
| 300 |
{ |
| 301 |
errno = 0; |
| 302 |
|
| 303 |
if(!dirp) |
| 304 |
{ |
| 305 |
errno = EFAULT; |
| 306 |
return; |
| 307 |
} |
| 308 |
|
| 309 |
if(lPos < -1) |
| 310 |
{ |
| 311 |
/* Seeking to an invalid position. */ |
| 312 |
errno = EINVAL; |
| 313 |
return; |
| 314 |
} |
| 315 |
else if(lPos == -1) |
| 316 |
{ |
| 317 |
/* Seek past end. */ |
| 318 |
if(dirp->dd_handle != -1) |
| 319 |
{ |
| 320 |
_findclose(dirp->dd_handle); |
| 321 |
} |
| 322 |
dirp->dd_handle = -1; |
| 323 |
dirp->dd_stat = -1; |
| 324 |
} |
| 325 |
else |
| 326 |
{ |
| 327 |
/* Rewind and read forward to the appropriate index. */ |
| 328 |
rewinddir(dirp); |
| 329 |
|
| 330 |
while((dirp->dd_stat < lPos) && readdir(dirp)) |
| 331 |
; /* do-nothing loop */ |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
// EOF |
| 336 |
|