00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "config.h"
00019
00020 #include <stdlib.h>
00021 #include <stdio.h>
00022
00023 #ifdef HAVE_ERRNO_H
00024 # include <errno.h>
00025 #else
00026 static int errno;
00027
00028 # define EFAULT 12
00029 # define ENOTDIR 12
00030 # define ENOENT 12
00031 # define ENOMEM 12
00032 # define EINVAL 12
00033 #endif
00034 #include <string.h>
00035 #ifndef UNDER_CE
00036 # include <io.h>
00037 # include <direct.h>
00038 #else
00039 # define FILENAME_MAX (260)
00040 #endif
00041
00042 #define WIN32_LEAN_AND_MEAN
00043 #include <windows.h>
00044
00045 #include <tchar.h>
00046 #define SUFFIX "*"
00047 #define SLASH "\\"
00048
00049 struct dirent
00050 {
00051 long d_ino;
00052 unsigned short d_reclen;
00053 unsigned short d_namlen;
00054 char d_name[FILENAME_MAX];
00055 };
00056
00057 typedef struct
00058 {
00059
00060 WIN32_FIND_DATA dd_dta;
00061
00062
00063
00064
00065 struct dirent dd_dir;
00066
00067
00068 HANDLE dd_handle;
00069
00070
00071
00072
00073
00074
00075
00076 int dd_stat;
00077
00078
00079 char dd_name[1];
00080 } DIR;
00081
00082
00083
00084
00085
00086
00087
00088 DIR *
00089 vlc_opendir (const CHAR *szPath)
00090 {
00091 DIR *nd;
00092 unsigned int rc;
00093 CHAR szFullPath[MAX_PATH];
00094
00095 errno = 0;
00096
00097 if (!szPath)
00098 {
00099 errno = EFAULT;
00100 return (DIR *) 0;
00101 }
00102
00103 if (szPath[0] == '\0')
00104 {
00105 errno = ENOTDIR;
00106 return (DIR *) 0;
00107 }
00108
00109
00110 #ifdef UNICODE
00111 {
00112 wchar_t szPathTmp[MAX_PATH];
00113 mbstowcs( szPathTmp, szPath, MAX_PATH );
00114 szPathTmp[MAX_PATH-1] = 0;
00115 rc = GetFileAttributes (szPathTmp);
00116 }
00117 #else
00118 rc = GetFileAttributes (szPath);
00119 #endif
00120 if (rc == (unsigned int)-1)
00121 {
00122
00123 errno = ENOENT;
00124 return (DIR *) 0;
00125 }
00126 if (!(rc & FILE_ATTRIBUTE_DIRECTORY))
00127 {
00128
00129 errno = ENOTDIR;
00130 return (DIR *) 0;
00131 }
00132
00133
00134 #if defined( UNDER_CE )
00135 if (szPath[0] == '\\' || szPath[0] == '/')
00136 {
00137 sprintf (szFullPath, "%s", szPath);
00138 szFullPath[0] = '\\';
00139 }
00140 else
00141 {
00142 wchar_t szFullPathTmp[MAX_PATH];
00143 if (GetModuleFileName( NULL, szFullPathTmp, MAX_PATH ) )
00144 {
00145 wcstombs( szFullPath, szFullPathTmp, MAX_PATH );
00146 szFullPath[MAX_PATH-1] = 0;
00147 }
00148 else
00149 {
00150
00151 sprintf (szFullPath, "\\%s", szPath );
00152 }
00153 }
00154 #else
00155 _fullpath (szFullPath, szPath, MAX_PATH);
00156 #endif
00157
00158
00159
00160 nd = (DIR *) malloc (sizeof (DIR) + strlen (szFullPath) + sizeof (SLASH) +
00161 sizeof (SUFFIX));
00162
00163 if (!nd)
00164 {
00165
00166 errno = ENOMEM;
00167 return (DIR *) 0;
00168 }
00169
00170
00171 strcpy (nd->dd_name, szFullPath);
00172
00173
00174 if (nd->dd_name[0] != '\0' &&
00175 nd->dd_name[strlen (nd->dd_name) - 1] != '/' &&
00176 nd->dd_name[strlen (nd->dd_name) - 1] != '\\')
00177 {
00178 strcat (nd->dd_name, SLASH);
00179 }
00180
00181
00182 strcat (nd->dd_name, SUFFIX);
00183
00184
00185
00186 nd->dd_handle = INVALID_HANDLE_VALUE;
00187
00188
00189 nd->dd_stat = 0;
00190
00191
00192
00193
00194 nd->dd_dir.d_ino = 0;
00195 nd->dd_dir.d_reclen = 0;
00196 nd->dd_dir.d_namlen = 0;
00197 memset (nd->dd_dir.d_name, 0, FILENAME_MAX);
00198
00199 return nd;
00200 }
00201
00202
00203
00204
00205
00206
00207
00208
00209 struct dirent *
00210 vlc_readdir (DIR * dirp)
00211 {
00212 errno = 0;
00213
00214
00215 if (!dirp)
00216 {
00217 errno = EFAULT;
00218 return (struct dirent *) 0;
00219 }
00220
00221 if (dirp->dd_stat < 0)
00222 {
00223
00224
00225 return (struct dirent *) 0;
00226 }
00227 else if (dirp->dd_stat == 0)
00228 {
00229 #ifdef UNICODE
00230 wchar_t dd_name[MAX_PATH];
00231 mbstowcs( dd_name, dirp->dd_name, MAX_PATH );
00232 dd_name[MAX_PATH-1] = 0;
00233 #else
00234 char *dd_name = dirp->dd_name;
00235 #endif
00236
00237
00238 dirp->dd_handle = FindFirstFile (dd_name, &(dirp->dd_dta));
00239
00240 if (dirp->dd_handle == INVALID_HANDLE_VALUE)
00241 {
00242
00243
00244 dirp->dd_stat = -1;
00245 }
00246 else
00247 {
00248 dirp->dd_stat = 1;
00249 }
00250 }
00251 else
00252 {
00253
00254 if (!FindNextFile ((HANDLE)dirp->dd_handle, &(dirp->dd_dta)))
00255 {
00256
00257 FindClose ((HANDLE)dirp->dd_handle);
00258 dirp->dd_handle = INVALID_HANDLE_VALUE;
00259 dirp->dd_stat = -1;
00260 }
00261 else
00262 {
00263
00264
00265 dirp->dd_stat++;
00266 }
00267 }
00268
00269 if (dirp->dd_stat > 0)
00270 {
00271
00272
00273 #ifdef UNICODE
00274 char d_name[MAX_PATH];
00275 wcstombs( d_name, dirp->dd_dta.cFileName, MAX_PATH );
00276 d_name[MAX_PATH-1] = 0;
00277 #else
00278 char *d_name = dirp->dd_dta.cFileName;
00279 #endif
00280
00281 strcpy (dirp->dd_dir.d_name, d_name);
00282 dirp->dd_dir.d_namlen = strlen (dirp->dd_dir.d_name);
00283 return &dirp->dd_dir;
00284 }
00285
00286 return (struct dirent *) 0;
00287 }
00288
00289
00290
00291
00292
00293
00294
00295 int
00296 vlc_closedir (DIR * dirp)
00297 {
00298 int rc;
00299
00300 errno = 0;
00301 rc = 0;
00302
00303 if (!dirp)
00304 {
00305 errno = EFAULT;
00306 return -1;
00307 }
00308
00309 if (dirp->dd_handle != INVALID_HANDLE_VALUE)
00310 {
00311 rc = FindClose ((HANDLE)dirp->dd_handle);
00312 }
00313
00314
00315 free (dirp);
00316
00317 return rc;
00318 }
00319
00320
00321
00322
00323
00324
00325
00326 void
00327 vlc_rewinddir (DIR * dirp)
00328 {
00329 errno = 0;
00330
00331 if (!dirp)
00332 {
00333 errno = EFAULT;
00334 return;
00335 }
00336
00337 if (dirp->dd_handle != INVALID_HANDLE_VALUE)
00338 {
00339 FindClose ((HANDLE)dirp->dd_handle);
00340 }
00341
00342 dirp->dd_handle = INVALID_HANDLE_VALUE;
00343 dirp->dd_stat = 0;
00344 }
00345
00346
00347
00348
00349
00350
00351
00352 long
00353 vlc_telldir (DIR * dirp)
00354 {
00355 errno = 0;
00356
00357 if (!dirp)
00358 {
00359 errno = EFAULT;
00360 return -1;
00361 }
00362 return dirp->dd_stat;
00363 }
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374 void
00375 vlc_seekdir (DIR * dirp, long lPos)
00376 {
00377 errno = 0;
00378
00379 if (!dirp)
00380 {
00381 errno = EFAULT;
00382 return;
00383 }
00384
00385 if (lPos < -1)
00386 {
00387
00388 errno = EINVAL;
00389 return;
00390 }
00391 else if (lPos == -1)
00392 {
00393
00394 if (dirp->dd_handle != INVALID_HANDLE_VALUE)
00395 {
00396 FindClose ((HANDLE)dirp->dd_handle);
00397 }
00398 dirp->dd_handle = INVALID_HANDLE_VALUE;
00399 dirp->dd_stat = -1;
00400 }
00401 else
00402 {
00403
00404 vlc_rewinddir (dirp);
00405
00406 while ((dirp->dd_stat < lPos) && vlc_readdir (dirp))
00407 ;
00408 }
00409 }