Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

dirent_portable.h 29KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. /*
  2. * This file was originally: "dirent for Visual C++" from: http://softagalleria.net/dirent.php (version 1.20.1)
  3. * However I've modified it to <dirent_portable.h> by adding:
  4. *
  5. * a fallback to <dirent.h> if _WIN32 is not defined
  6. * two missing methods: scandir(...) and alphasort(...)
  7. * and some other minor modifications (see below)
  8. *
  9. *
  10. * Original license from http://softagalleria.net/dirent.php
  11. *
  12. *========================================================================
  13. *
  14. * dirent.h - dirent API for Microsoft Visual Studio
  15. *
  16. * Copyright (C) 2006-2012 Toni Ronkko
  17. *
  18. * Permission is hereby granted, free of charge, to any person obtaining
  19. * a copy of this software and associated documentation files (the
  20. * ``Software''), to deal in the Software without restriction, including
  21. * without limitation the rights to use, copy, modify, merge, publish,
  22. * distribute, sublicense, and/or sell copies of the Software, and to
  23. * permit persons to whom the Software is furnished to do so, subject to
  24. * the following conditions:
  25. *
  26. * The above copyright notice and this permission notice shall be included
  27. * in all copies or substantial portions of the Software.
  28. *
  29. * THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
  30. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  32. * IN NO EVENT SHALL TONI RONKKO BE LIABLE FOR ANY CLAIM, DAMAGES OR
  33. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  34. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  35. * OTHER DEALINGS IN THE SOFTWARE.
  36. *
  37. * $Id: dirent.h,v 1.20 2014/03/19 17:52:23 tronkko Exp $
  38. *
  39. * =========================================================================
  40. * Added:
  41. * -> some undefs to prevent possible compiler warnings
  42. * -> the scandir(...) and alphasort(...) methods
  43. * -> the optional DIRENT_USES_UTF8_CHARS definition (needed for browsing with long UTF8 paths, instead of short ASCII paths).
  44. * WARNING: in my tests the usage of the DIRENT_USES_UTF8_CHARS is not fully functional (patches are welcome)
  45. * All these additions have been made to made <dirent_portable.h> usage for Windows consistent
  46. * with what I get using <direct.h> under my Ubuntu Linux OS.
  47. * =========================================================================
  48. *
  49. * The code of the scandir(...) method come from the musl library (http://www.musl-libc.org/)
  50. * (MIT licensed, Copyright © 2005-2014 Rich Felker, et al.).
  51. *
  52. * The code of the alphasort(...) method and of all the other minor modifications is in the public domain.
  53. *
  54. */
  55. #if (!defined(_WIN32) && !defined(_WIN64))
  56. # include <dirent.h>
  57. #else // #if (!defined(_WIN32) && !defined(_WIN64))
  58. #ifndef DIRENT_H
  59. #define DIRENT_H
  60. /*
  61. * Define architecture flags so we don't need to include windows.h.
  62. * Avoiding windows.h makes it simpler to use windows sockets in conjunction
  63. * with dirent.h.
  64. */
  65. #if !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && defined(_M_IX86)
  66. # define _X86_
  67. #endif
  68. #if !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && defined(_M_AMD64)
  69. #define _AMD64_
  70. #endif
  71. #include <stdio.h>
  72. #include <stdarg.h>
  73. #include <windef.h>
  74. #include <winbase.h>
  75. #include <wchar.h>
  76. #include <string.h>
  77. #include <stdlib.h>
  78. #include <malloc.h>
  79. #include <sys/types.h>
  80. #include <sys/stat.h>
  81. #include <errno.h>
  82. //#define DIRENT_USES_UTF8_CHARS // Test only
  83. /* Indicates that d_type field is available in dirent structure */
  84. #define _DIRENT_HAVE_D_TYPE
  85. /* Indicates that d_namlen field is available in dirent structure */
  86. #define _DIRENT_HAVE_D_NAMLEN
  87. /* Entries missing from MSVC 6.0 */
  88. #if !defined(FILE_ATTRIBUTE_DEVICE)
  89. # define FILE_ATTRIBUTE_DEVICE 0x40
  90. #endif
  91. /* File type and permission flags for stat() */
  92. #if !defined(S_IFMT)
  93. # define S_IFMT _S_IFMT /* File type mask */
  94. #endif
  95. #if !defined(S_IFDIR)
  96. # define S_IFDIR _S_IFDIR /* Directory */
  97. #endif
  98. #if !defined(S_IFCHR)
  99. # define S_IFCHR _S_IFCHR /* Character device */
  100. #endif
  101. #if !defined(S_IFFIFO)
  102. # define S_IFFIFO _S_IFFIFO /* Pipe */
  103. #endif
  104. #if !defined(S_IFREG)
  105. # define S_IFREG _S_IFREG /* Regular file */
  106. #endif
  107. #if !defined(S_IREAD)
  108. # define S_IREAD _S_IREAD /* Read permission */
  109. #endif
  110. #if !defined(S_IWRITE)
  111. # define S_IWRITE _S_IWRITE /* Write permission */
  112. #endif
  113. #if !defined(S_IEXEC)
  114. # define S_IEXEC _S_IEXEC /* Execute permission */
  115. #endif
  116. #if !defined(S_IFIFO)
  117. # define S_IFIFO _S_IFIFO /* Pipe */
  118. #endif
  119. #if !defined(S_IFBLK)
  120. # define S_IFBLK 0 /* Block device */
  121. #endif
  122. #if !defined(S_IFLNK)
  123. # define S_IFLNK 0 /* Link */
  124. #endif
  125. #if !defined(S_IFSOCK)
  126. # define S_IFSOCK 0 /* Socket */
  127. #endif
  128. #if defined(_MSC_VER)
  129. # define S_IRUSR S_IREAD /* Read user */
  130. # define S_IWUSR S_IWRITE /* Write user */
  131. # define S_IXUSR 0 /* Execute user */
  132. # define S_IRGRP 0 /* Read group */
  133. # define S_IWGRP 0 /* Write group */
  134. # define S_IXGRP 0 /* Execute group */
  135. # define S_IROTH 0 /* Read others */
  136. # define S_IWOTH 0 /* Write others */
  137. # define S_IXOTH 0 /* Execute others */
  138. #endif
  139. /* Maximum length of file name */
  140. #ifdef DIRENT_USES_UTF8_CHARS
  141. // utf8 strings can have up to 4 bytes per char
  142. # undef PATH_MAX
  143. # define PATH_MAX (MAX_PATH*4)
  144. # undef FILENAME_MAX
  145. # define FILENAME_MAX MAX_PATH
  146. # undef NAME_MAX
  147. # define NAME_MAX FILENAME_MAX
  148. #else //DIRENT_USES_UTF8_CHARS
  149. # if !defined(PATH_MAX)
  150. # define PATH_MAX MAX_PATH
  151. # endif
  152. # if !defined(FILENAME_MAX)
  153. # define FILENAME_MAX MAX_PATH
  154. # endif
  155. # if !defined(NAME_MAX)
  156. # define NAME_MAX FILENAME_MAX
  157. # endif
  158. #endif //DIRENT_USES_UTF8_CHARS
  159. /* File type flags for d_type */
  160. #define DT_UNKNOWN 0
  161. #define DT_REG S_IFREG
  162. #define DT_DIR S_IFDIR
  163. #define DT_FIFO S_IFIFO
  164. #define DT_SOCK S_IFSOCK
  165. #define DT_CHR S_IFCHR
  166. #define DT_BLK S_IFBLK
  167. #define DT_LNK S_IFLNK
  168. /* Macros for converting between st_mode and d_type */
  169. #define IFTODT(mode) ((mode) & S_IFMT)
  170. #define DTTOIF(type) (type)
  171. /*
  172. * File type macros. Note that block devices, sockets and links cannot be
  173. * distinguished on Windows and the macros S_ISBLK, S_ISSOCK and S_ISLNK are
  174. * only defined for compatibility. These macros should always return false
  175. * on Windows.
  176. */
  177. // Added some undefs to prevent possible compiler warnings
  178. #undef S_ISFIFO
  179. #undef S_ISDIR
  180. #undef S_ISREG
  181. #undef S_ISLNK
  182. #undef S_ISSOCK
  183. #undef S_ISSOCK
  184. #undef S_ISCHR
  185. #undef S_ISBLK
  186. #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
  187. #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
  188. #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
  189. #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
  190. #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
  191. #define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
  192. #define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
  193. /* Return the exact length of d_namlen without zero terminator */
  194. #define _D_EXACT_NAMLEN(p) ((p)->d_namlen)
  195. /* Return number of bytes needed to store d_namlen */
  196. #define _D_ALLOC_NAMLEN(p) (PATH_MAX)
  197. #ifdef __cplusplus
  198. extern "C" {
  199. #endif
  200. /* Wide-character version */
  201. struct _wdirent {
  202. long d_ino; /* Always zero */
  203. unsigned short d_reclen; /* Structure size */
  204. size_t d_namlen; /* Length of name without \0 */
  205. int d_type; /* File type */
  206. wchar_t d_name[PATH_MAX]; /* File name */
  207. };
  208. typedef struct _wdirent _wdirent;
  209. struct _WDIR {
  210. struct _wdirent ent; /* Current directory entry */
  211. WIN32_FIND_DATAW data; /* Private file data */
  212. int cached; /* True if data is valid */
  213. HANDLE handle; /* Win32 search handle */
  214. wchar_t *patt; /* Initial directory name */
  215. };
  216. typedef struct _WDIR _WDIR;
  217. static _WDIR *_wopendir (const wchar_t *dirname);
  218. static struct _wdirent *_wreaddir (_WDIR *dirp);
  219. static int _wclosedir (_WDIR *dirp);
  220. static void _wrewinddir (_WDIR* dirp);
  221. /* For compatibility with Symbian */
  222. #define wdirent _wdirent
  223. #define WDIR _WDIR
  224. #define wopendir _wopendir
  225. #define wreaddir _wreaddir
  226. #define wclosedir _wclosedir
  227. #define wrewinddir _wrewinddir
  228. /* Multi-byte character versions */
  229. struct dirent {
  230. long d_ino; /* Always zero */
  231. unsigned short d_reclen; /* Structure size */
  232. size_t d_namlen; /* Length of name without \0 */
  233. int d_type; /* File type */
  234. char d_name[PATH_MAX]; /* File name */
  235. };
  236. typedef struct dirent dirent;
  237. struct DIR {
  238. struct dirent ent;
  239. struct _WDIR *wdirp;
  240. };
  241. typedef struct DIR DIR;
  242. static DIR *opendir (const char *dirname);
  243. static struct dirent *readdir (DIR *dirp);
  244. static int closedir (DIR *dirp);
  245. static void rewinddir (DIR* dirp);
  246. /* Internal utility functions */
  247. static WIN32_FIND_DATAW *dirent_first (_WDIR *dirp);
  248. static WIN32_FIND_DATAW *dirent_next (_WDIR *dirp);
  249. static int dirent_mbstowcs_s(
  250. size_t *pReturnValue,
  251. wchar_t *wcstr,
  252. size_t sizeInWords,
  253. const char *mbstr,
  254. size_t count);
  255. static int dirent_wcstombs_s(
  256. size_t *pReturnValue,
  257. char *mbstr,
  258. size_t sizeInBytes,
  259. const wchar_t *wcstr,
  260. size_t count);
  261. static void dirent_set_errno (int error);
  262. /*
  263. * Open directory stream DIRNAME for read and return a pointer to the
  264. * internal working area that is used to retrieve individual directory
  265. * entries.
  266. */
  267. static _WDIR*
  268. _wopendir(
  269. const wchar_t *dirname)
  270. {
  271. _WDIR *dirp = NULL;
  272. int error;
  273. /* Must have directory name */
  274. if (dirname == NULL || dirname[0] == '\0') {
  275. dirent_set_errno (ENOENT);
  276. return NULL;
  277. }
  278. /* Allocate new _WDIR structure */
  279. dirp = (_WDIR*) malloc (sizeof (struct _WDIR));
  280. if (dirp != NULL) {
  281. DWORD n;
  282. /* Reset _WDIR structure */
  283. dirp->handle = INVALID_HANDLE_VALUE;
  284. dirp->patt = NULL;
  285. dirp->cached = 0;
  286. /* Compute the length of full path plus zero terminator */
  287. n = GetFullPathNameW (dirname, 0, NULL, NULL);
  288. /* Allocate room for absolute directory name and search pattern */
  289. dirp->patt = (wchar_t*) malloc (sizeof (wchar_t) * n + 16);
  290. if (dirp->patt) {
  291. /*
  292. * Convert relative directory name to an absolute one. This
  293. * allows rewinddir() to function correctly even when current
  294. * working directory is changed between opendir() and rewinddir().
  295. */
  296. n = GetFullPathNameW (dirname, n, dirp->patt, NULL);
  297. if (n > 0) {
  298. wchar_t *p;
  299. /* Append search pattern \* to the directory name */
  300. p = dirp->patt + n;
  301. if (dirp->patt < p) {
  302. switch (p[-1]) {
  303. case '\\':
  304. case '/':
  305. case ':':
  306. /* Directory ends in path separator, e.g. c:\temp\ */
  307. /*NOP*/;
  308. break;
  309. default:
  310. /* Directory name doesn't end in path separator */
  311. *p++ = '\\';
  312. }
  313. }
  314. *p++ = '*';
  315. *p = '\0';
  316. /* Open directory stream and retrieve the first entry */
  317. if (dirent_first (dirp)) {
  318. /* Directory stream opened successfully */
  319. error = 0;
  320. } else {
  321. /* Cannot retrieve first entry */
  322. error = 1;
  323. dirent_set_errno (ENOENT);
  324. }
  325. } else {
  326. /* Cannot retrieve full path name */
  327. dirent_set_errno (ENOENT);
  328. error = 1;
  329. }
  330. } else {
  331. /* Cannot allocate memory for search pattern */
  332. error = 1;
  333. }
  334. } else {
  335. /* Cannot allocate _WDIR structure */
  336. error = 1;
  337. }
  338. /* Clean up in case of error */
  339. if (error && dirp) {
  340. _wclosedir (dirp);
  341. dirp = NULL;
  342. }
  343. return dirp;
  344. }
  345. /*
  346. * Read next directory entry. The directory entry is returned in dirent
  347. * structure in the d_name field. Individual directory entries returned by
  348. * this function include regular files, sub-directories, pseudo-directories
  349. * "." and ".." as well as volume labels, hidden files and system files.
  350. */
  351. static struct _wdirent*
  352. _wreaddir(
  353. _WDIR *dirp)
  354. {
  355. WIN32_FIND_DATAW *datap;
  356. struct _wdirent *entp;
  357. /* Read next directory entry */
  358. datap = dirent_next (dirp);
  359. if (datap) {
  360. size_t n;
  361. DWORD attr;
  362. /* Pointer to directory entry to return */
  363. entp = &dirp->ent;
  364. /*
  365. * Copy file name as wide-character string. If the file name is too
  366. * long to fit in to the destination buffer, then truncate file name
  367. * to PATH_MAX characters and zero-terminate the buffer.
  368. */
  369. n = 0;
  370. while (n + 1 < PATH_MAX && datap->cFileName[n] != 0) {
  371. entp->d_name[n] = datap->cFileName[n];
  372. n++;
  373. }
  374. dirp->ent.d_name[n] = 0;
  375. /* Length of file name excluding zero terminator */
  376. entp->d_namlen = n;
  377. /* File type */
  378. attr = datap->dwFileAttributes;
  379. if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
  380. entp->d_type = DT_CHR;
  381. } else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
  382. entp->d_type = DT_DIR;
  383. } else {
  384. entp->d_type = DT_REG;
  385. }
  386. /* Reset dummy fields */
  387. entp->d_ino = 0;
  388. entp->d_reclen = sizeof (struct _wdirent);
  389. } else {
  390. /* Last directory entry read */
  391. entp = NULL;
  392. }
  393. return entp;
  394. }
  395. /*
  396. * Close directory stream opened by opendir() function. This invalidates the
  397. * DIR structure as well as any directory entry read previously by
  398. * _wreaddir().
  399. */
  400. static int
  401. _wclosedir(
  402. _WDIR *dirp)
  403. {
  404. int ok;
  405. if (dirp) {
  406. /* Release search handle */
  407. if (dirp->handle != INVALID_HANDLE_VALUE) {
  408. FindClose (dirp->handle);
  409. dirp->handle = INVALID_HANDLE_VALUE;
  410. }
  411. /* Release search pattern */
  412. if (dirp->patt) {
  413. free (dirp->patt);
  414. dirp->patt = NULL;
  415. }
  416. /* Release directory structure */
  417. free (dirp);
  418. ok = /*success*/0;
  419. } else {
  420. /* Invalid directory stream */
  421. dirent_set_errno (EBADF);
  422. ok = /*failure*/-1;
  423. }
  424. return ok;
  425. }
  426. /*
  427. * Rewind directory stream such that _wreaddir() returns the very first
  428. * file name again.
  429. */
  430. static void
  431. _wrewinddir(
  432. _WDIR* dirp)
  433. {
  434. if (dirp) {
  435. /* Release existing search handle */
  436. if (dirp->handle != INVALID_HANDLE_VALUE) {
  437. FindClose (dirp->handle);
  438. }
  439. /* Open new search handle */
  440. dirent_first (dirp);
  441. }
  442. }
  443. /* Get first directory entry (internal) */
  444. static WIN32_FIND_DATAW*
  445. dirent_first(
  446. _WDIR *dirp)
  447. {
  448. WIN32_FIND_DATAW *datap;
  449. /* Open directory and retrieve the first entry */
  450. dirp->handle = FindFirstFileW (dirp->patt, &dirp->data);
  451. if (dirp->handle != INVALID_HANDLE_VALUE) {
  452. /* a directory entry is now waiting in memory */
  453. datap = &dirp->data;
  454. dirp->cached = 1;
  455. } else {
  456. /* Failed to re-open directory: no directory entry in memory */
  457. dirp->cached = 0;
  458. datap = NULL;
  459. }
  460. return datap;
  461. }
  462. /* Get next directory entry (internal) */
  463. static WIN32_FIND_DATAW*
  464. dirent_next(
  465. _WDIR *dirp)
  466. {
  467. WIN32_FIND_DATAW *p;
  468. /* Get next directory entry */
  469. if (dirp->cached != 0) {
  470. /* A valid directory entry already in memory */
  471. p = &dirp->data;
  472. dirp->cached = 0;
  473. } else if (dirp->handle != INVALID_HANDLE_VALUE) {
  474. /* Get the next directory entry from stream */
  475. if (FindNextFileW (dirp->handle, &dirp->data) != FALSE) {
  476. /* Got a file */
  477. p = &dirp->data;
  478. } else {
  479. /* The very last entry has been processed or an error occured */
  480. FindClose (dirp->handle);
  481. dirp->handle = INVALID_HANDLE_VALUE;
  482. p = NULL;
  483. }
  484. } else {
  485. /* End of directory stream reached */
  486. p = NULL;
  487. }
  488. return p;
  489. }
  490. /*
  491. * Open directory stream using plain old C-string.
  492. */
  493. static DIR*
  494. opendir(
  495. const char *dirname)
  496. {
  497. struct DIR *dirp;
  498. int error;
  499. /* Must have directory name */
  500. if (dirname == NULL || dirname[0] == '\0') {
  501. dirent_set_errno (ENOENT);
  502. return NULL;
  503. }
  504. /* Allocate memory for DIR structure */
  505. dirp = (DIR*) malloc (sizeof (struct DIR));
  506. if (dirp) {
  507. wchar_t wname[PATH_MAX];
  508. size_t n;
  509. /* Convert directory name to wide-character string */
  510. error = dirent_mbstowcs_s (&n, wname, PATH_MAX, dirname, PATH_MAX);
  511. if (!error) {
  512. /* Open directory stream using wide-character name */
  513. dirp->wdirp = _wopendir (wname);
  514. if (dirp->wdirp) {
  515. /* Directory stream opened */
  516. error = 0;
  517. } else {
  518. /* Failed to open directory stream */
  519. error = 1;
  520. }
  521. } else {
  522. /*
  523. * Cannot convert file name to wide-character string. This
  524. * occurs if the string contains invalid multi-byte sequences or
  525. * the output buffer is too small to contain the resulting
  526. * string.
  527. */
  528. error = 1;
  529. }
  530. } else {
  531. /* Cannot allocate DIR structure */
  532. error = 1;
  533. }
  534. /* Clean up in case of error */
  535. if (error && dirp) {
  536. free (dirp);
  537. dirp = NULL;
  538. }
  539. return dirp;
  540. }
  541. /*
  542. * Read next directory entry.
  543. *
  544. * When working with text consoles, please note that file names returned by
  545. * readdir() are represented in the default ANSI code page while any output to
  546. * console is typically formatted on another code page. Thus, non-ASCII
  547. * characters in file names will not usually display correctly on console. The
  548. * problem can be fixed in two ways: (1) change the character set of console
  549. * to 1252 using chcp utility and use Lucida Console font, or (2) use
  550. * _cprintf function when writing to console. The _cprinf() will re-encode
  551. * ANSI strings to the console code page so many non-ASCII characters will
  552. * display correcly.
  553. */
  554. static struct dirent*
  555. readdir(
  556. DIR *dirp)
  557. {
  558. WIN32_FIND_DATAW *datap;
  559. struct dirent *entp;
  560. /* Read next directory entry */
  561. datap = dirent_next (dirp->wdirp);
  562. if (datap) {
  563. size_t n;
  564. int error;
  565. /* Attempt to convert file name to multi-byte string */
  566. error = dirent_wcstombs_s(
  567. &n, dirp->ent.d_name, PATH_MAX, datap->cFileName, PATH_MAX);
  568. /*
  569. * If the file name cannot be represented by a multi-byte string,
  570. * then attempt to use old 8+3 file name. This allows traditional
  571. * Unix-code to access some file names despite of unicode
  572. * characters, although file names may seem unfamiliar to the user.
  573. *
  574. * Be ware that the code below cannot come up with a short file
  575. * name unless the file system provides one. At least
  576. * VirtualBox shared folders fail to do this.
  577. */
  578. if (error && datap->cAlternateFileName[0] != '\0') {
  579. error = dirent_wcstombs_s(
  580. &n, dirp->ent.d_name, PATH_MAX,
  581. datap->cAlternateFileName, PATH_MAX);
  582. }
  583. if (!error) {
  584. DWORD attr;
  585. /* Initialize directory entry for return */
  586. entp = &dirp->ent;
  587. /* Length of file name excluding zero terminator */
  588. entp->d_namlen = n - 1;
  589. /* File attributes */
  590. attr = datap->dwFileAttributes;
  591. if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
  592. entp->d_type = DT_CHR;
  593. } else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
  594. entp->d_type = DT_DIR;
  595. } else {
  596. entp->d_type = DT_REG;
  597. }
  598. /* Reset dummy fields */
  599. entp->d_ino = 0;
  600. entp->d_reclen = sizeof (struct dirent);
  601. } else {
  602. /*
  603. * Cannot convert file name to multi-byte string so construct
  604. * an errornous directory entry and return that. Note that
  605. * we cannot return NULL as that would stop the processing
  606. * of directory entries completely.
  607. */
  608. entp = &dirp->ent;
  609. entp->d_name[0] = '?';
  610. entp->d_name[1] = '\0';
  611. entp->d_namlen = 1;
  612. entp->d_type = DT_UNKNOWN;
  613. entp->d_ino = 0;
  614. entp->d_reclen = 0;
  615. }
  616. } else {
  617. /* No more directory entries */
  618. entp = NULL;
  619. }
  620. return entp;
  621. }
  622. /*
  623. * Close directory stream.
  624. */
  625. static int
  626. closedir(
  627. DIR *dirp)
  628. {
  629. int ok;
  630. if (dirp) {
  631. /* Close wide-character directory stream */
  632. ok = _wclosedir (dirp->wdirp);
  633. dirp->wdirp = NULL;
  634. /* Release multi-byte character version */
  635. free (dirp);
  636. } else {
  637. /* Invalid directory stream */
  638. dirent_set_errno (EBADF);
  639. ok = /*failure*/-1;
  640. }
  641. return ok;
  642. }
  643. /*
  644. * Rewind directory stream to beginning.
  645. */
  646. static void
  647. rewinddir(
  648. DIR* dirp)
  649. {
  650. /* Rewind wide-character string directory stream */
  651. _wrewinddir (dirp->wdirp);
  652. }
  653. /* Convert multi-byte string to wide character string */
  654. static int
  655. dirent_mbstowcs_s(
  656. size_t *pReturnValue,
  657. wchar_t *wcstr,
  658. size_t sizeInWords,
  659. const char *mbstr,
  660. size_t count)
  661. {
  662. int error;
  663. #ifdef DIRENT_USES_UTF8_CHARS
  664. // we don't use "count" at all: we assume mstr is zero terminated:
  665. size_t n = (size_t) MultiByteToWideChar (CP_UTF8, 0, mbstr, -1, wcstr, 0);//sizeInWords);
  666. if (n==0) {
  667. error = 1;
  668. if (sizeInWords>0) wcstr[0]=L'\0';
  669. if (pReturnValue) *pReturnValue = 0;
  670. }
  671. else if (n<=sizeInWords) {
  672. error = MultiByteToWideChar (CP_UTF8, 0, mbstr, -1, wcstr, n) == 0 ? 1 : 0;
  673. if (pReturnValue) *pReturnValue = n;
  674. }
  675. else {
  676. // Buffer too low:
  677. if (sizeInWords>0) {
  678. if (sizeInWords>1) MultiByteToWideChar (CP_UTF8, 0, mbstr, -1, wcstr, sizeInWords-1);
  679. wcstr[sizeInWords-1] = L'\0';
  680. }
  681. if (pReturnValue) *pReturnValue = sizeInWords;
  682. error = 1;
  683. }
  684. /*
  685. if (!wcstr || n < count) {
  686. // Zero-terminate output buffer
  687. if (wcstr && sizeInWords) {
  688. if (n >= sizeInWords) {
  689. n = sizeInWords - 1;
  690. }
  691. wcstr[n] = 0;
  692. }
  693. // Length of resuting multi-byte string WITH zero terminator
  694. if (pReturnValue) {
  695. *pReturnValue = n + 1;
  696. }
  697. // Success
  698. error = 0;
  699. } else {
  700. // Could not convert string
  701. error = 1;
  702. }
  703. */
  704. #else //DIRENT_USES_UTF8_CHARS
  705. #if defined(_MSC_VER) && _MSC_VER >= 1400
  706. /* Microsoft Visual Studio 2005 or later */
  707. error = mbstowcs_s (pReturnValue, wcstr, sizeInWords, mbstr, count);
  708. #else
  709. /* Older Visual Studio or non-Microsoft compiler */
  710. size_t n;
  711. /* Convert to wide-character string (or count characters) */
  712. n = mbstowcs (wcstr, mbstr, sizeInWords);
  713. if (!wcstr || n < count) {
  714. /* Zero-terminate output buffer */
  715. if (wcstr && sizeInWords) {
  716. if (n >= sizeInWords) {
  717. n = sizeInWords - 1;
  718. }
  719. wcstr[n] = 0;
  720. }
  721. /* Length of resuting multi-byte string WITH zero terminator */
  722. if (pReturnValue) {
  723. *pReturnValue = n + 1;
  724. }
  725. /* Success */
  726. error = 0;
  727. } else {
  728. /* Could not convert string */
  729. error = 1;
  730. }
  731. #endif
  732. #endif //DIRENT_USES_UTF8_CHARS
  733. return error;
  734. }
  735. /* Convert wide-character string to multi-byte string */
  736. static int
  737. dirent_wcstombs_s(
  738. size_t *pReturnValue,
  739. char *mbstr,
  740. size_t sizeInBytes, /* max size of mbstr */
  741. const wchar_t *wcstr,
  742. size_t count)
  743. {
  744. int error;
  745. #ifdef DIRENT_USES_UTF8_CHARS
  746. // we don't use "count" at all: we assume wcstr is zero terminated:
  747. size_t n = (size_t) WideCharToMultiByte (CP_UTF8, 0, wcstr, -1, mbstr, 0,NULL,NULL);//sizeInBytes, NULL, NULL);
  748. if (n==0) {
  749. error = 1;
  750. if (sizeInBytes>0) mbstr[0]='\0';
  751. if (pReturnValue) *pReturnValue = 0;
  752. }
  753. else if (n<=sizeInBytes) {
  754. error = WideCharToMultiByte (CP_UTF8, 0, wcstr, -1, mbstr, n, NULL, NULL) == 0 ? 1 : 0;
  755. if (pReturnValue) *pReturnValue = n;
  756. }
  757. else {
  758. // Buffer too low:
  759. if (sizeInBytes>0) {
  760. if (sizeInBytes>1) WideCharToMultiByte (CP_UTF8, 0, wcstr, -1, mbstr, sizeInBytes-1, NULL, NULL);
  761. mbstr[sizeInBytes-1] = '\0';
  762. }
  763. if (pReturnValue) *pReturnValue = sizeInBytes;
  764. error = 1;
  765. }
  766. /*
  767. if (!mbstr || n < count) {
  768. // Zero-terminate output buffer
  769. if (mbstr && sizeInBytes) {
  770. if (n >= sizeInBytes) {
  771. n = sizeInBytes - 1;
  772. }
  773. mbstr[n] = '\0';
  774. }
  775. // Lenght of resulting multi-bytes string WITH zero-terminator
  776. if (pReturnValue) {
  777. *pReturnValue = n + 1;
  778. }
  779. // Success
  780. error = 0;
  781. } else {
  782. // Cannot convert string
  783. error = 1;
  784. }
  785. */
  786. #else //DIRENT_USES_UTF8_CHARS
  787. #if defined(_MSC_VER) && _MSC_VER >= 1400
  788. /* Microsoft Visual Studio 2005 or later */
  789. error = wcstombs_s (pReturnValue, mbstr, sizeInBytes, wcstr, count);
  790. #else
  791. /* Older Visual Studio or non-Microsoft compiler */
  792. size_t n;
  793. /* Convert to multi-byte string (or count the number of bytes needed) */
  794. n = wcstombs (mbstr, wcstr, sizeInBytes);
  795. if (!mbstr || n < count) {
  796. /* Zero-terminate output buffer */
  797. if (mbstr && sizeInBytes) {
  798. if (n >= sizeInBytes) {
  799. n = sizeInBytes - 1;
  800. }
  801. mbstr[n] = '\0';
  802. }
  803. /* Lenght of resulting multi-bytes string WITH zero-terminator */
  804. if (pReturnValue) {
  805. *pReturnValue = n + 1;
  806. }
  807. /* Success */
  808. error = 0;
  809. } else {
  810. /* Cannot convert string */
  811. error = 1;
  812. }
  813. #endif
  814. #endif //DIRENT_USES_UTF8_CHARS
  815. return error;
  816. }
  817. /* Set errno variable */
  818. static void
  819. dirent_set_errno(
  820. int error)
  821. {
  822. #if defined(_MSC_VER) && _MSC_VER >= 1400
  823. /* Microsoft Visual Studio 2005 and later */
  824. _set_errno (error);
  825. #else
  826. /* Non-Microsoft compiler or older Microsoft compiler */
  827. errno = error;
  828. #endif
  829. }
  830. // The code of this single method comes from the musl library
  831. // (MIT licensed, Copyright © 2005-2014 Rich Felker, et al.)
  832. inline static int scandir(const char *path, struct dirent ***res,
  833. int (*sel)(const struct dirent *),
  834. int (*cmp)(const struct dirent **, const struct dirent **))
  835. {
  836. DIR *d = opendir(path);
  837. struct dirent *de, **names=0, **tmp;
  838. size_t cnt=0, len=0;
  839. int old_errno = errno;
  840. if (!d) return -1;
  841. while ((errno=0), (de = readdir(d))) {
  842. if (sel && !sel(de)) continue;
  843. if (cnt >= len) {
  844. len = 2*len+1;
  845. if (len > SIZE_MAX/sizeof *names) break;
  846. tmp = (dirent**)realloc(names, len * sizeof *names);
  847. if (!tmp) break;
  848. names = tmp;
  849. }
  850. names[cnt] = (dirent*)malloc(de->d_reclen);
  851. if (!names[cnt]) break;
  852. memcpy(names[cnt++], de, de->d_reclen);
  853. }
  854. closedir(d);
  855. if (errno) {
  856. if (names) while (cnt-->0) free(names[cnt]);
  857. free(names);
  858. return -1;
  859. }
  860. errno = old_errno;
  861. if (cmp) qsort(names, cnt, sizeof *names, (int (*)(const void *, const void *))cmp);
  862. *res = names;
  863. return cnt;
  864. }
  865. // alphasort: Function to compare two `struct dirent's alphabetically.
  866. inline static int alphasort (const struct dirent **e1,const struct dirent **e2) {
  867. return strcmp((*e1)->d_name,(*e2)->d_name);
  868. }
  869. #ifdef __cplusplus
  870. }
  871. #endif
  872. #endif /*DIRENT_H*/
  873. #endif //#if (!defined(_WIN32) && !defined(_WIN64))