Hex Artifact Content
Not logged in

Artifact e1e5f5c210c295a2c0786b5c96ecc7df393c99d7:


0000: 2f 2a 0a 20 2a 20 64 69 72 65 6e 74 2e 68 20 2d  /*. * dirent.h -
0010: 20 64 69 72 65 6e 74 20 41 50 49 20 66 6f 72 20   dirent API for 
0020: 4d 69 63 72 6f 73 6f 66 74 20 56 69 73 75 61 6c  Microsoft Visual
0030: 20 53 74 75 64 69 6f 0a 20 2a 0a 20 2a 20 43 6f   Studio. *. * Co
0040: 70 79 72 69 67 68 74 20 28 43 29 20 32 30 30 36  pyright (C) 2006
0050: 2d 32 30 31 32 20 54 6f 6e 69 20 52 6f 6e 6b 6b  -2012 Toni Ronkk
0060: 6f 0a 20 2a 0a 20 2a 20 50 65 72 6d 69 73 73 69  o. *. * Permissi
0070: 6f 6e 20 69 73 20 68 65 72 65 62 79 20 67 72 61  on is hereby gra
0080: 6e 74 65 64 2c 20 66 72 65 65 20 6f 66 20 63 68  nted, free of ch
0090: 61 72 67 65 2c 20 74 6f 20 61 6e 79 20 70 65 72  arge, to any per
00a0: 73 6f 6e 20 6f 62 74 61 69 6e 69 6e 67 0a 20 2a  son obtaining. *
00b0: 20 61 20 63 6f 70 79 20 6f 66 20 74 68 69 73 20   a copy of this 
00c0: 73 6f 66 74 77 61 72 65 20 61 6e 64 20 61 73 73  software and ass
00d0: 6f 63 69 61 74 65 64 20 64 6f 63 75 6d 65 6e 74  ociated document
00e0: 61 74 69 6f 6e 20 66 69 6c 65 73 20 28 74 68 65  ation files (the
00f0: 0a 20 2a 20 60 60 53 6f 66 74 77 61 72 65 27 27  . * ``Software''
0100: 29 2c 20 74 6f 20 64 65 61 6c 20 69 6e 20 74 68  ), to deal in th
0110: 65 20 53 6f 66 74 77 61 72 65 20 77 69 74 68 6f  e Software witho
0120: 75 74 20 72 65 73 74 72 69 63 74 69 6f 6e 2c 20  ut restriction, 
0130: 69 6e 63 6c 75 64 69 6e 67 0a 20 2a 20 77 69 74  including. * wit
0140: 68 6f 75 74 20 6c 69 6d 69 74 61 74 69 6f 6e 20  hout limitation 
0150: 74 68 65 20 72 69 67 68 74 73 20 74 6f 20 75 73  the rights to us
0160: 65 2c 20 63 6f 70 79 2c 20 6d 6f 64 69 66 79 2c  e, copy, modify,
0170: 20 6d 65 72 67 65 2c 20 70 75 62 6c 69 73 68 2c   merge, publish,
0180: 0a 20 2a 20 64 69 73 74 72 69 62 75 74 65 2c 20  . * distribute, 
0190: 73 75 62 6c 69 63 65 6e 73 65 2c 20 61 6e 64 2f  sublicense, and/
01a0: 6f 72 20 73 65 6c 6c 20 63 6f 70 69 65 73 20 6f  or sell copies o
01b0: 66 20 74 68 65 20 53 6f 66 74 77 61 72 65 2c 20  f the Software, 
01c0: 61 6e 64 20 74 6f 0a 20 2a 20 70 65 72 6d 69 74  and to. * permit
01d0: 20 70 65 72 73 6f 6e 73 20 74 6f 20 77 68 6f 6d   persons to whom
01e0: 20 74 68 65 20 53 6f 66 74 77 61 72 65 20 69 73   the Software is
01f0: 20 66 75 72 6e 69 73 68 65 64 20 74 6f 20 64 6f   furnished to do
0200: 20 73 6f 2c 20 73 75 62 6a 65 63 74 20 74 6f 0a   so, subject to.
0210: 20 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67   * the following
0220: 20 63 6f 6e 64 69 74 69 6f 6e 73 3a 0a 20 2a 0a   conditions:. *.
0230: 20 2a 20 54 68 65 20 61 62 6f 76 65 20 63 6f 70   * The above cop
0240: 79 72 69 67 68 74 20 6e 6f 74 69 63 65 20 61 6e  yright notice an
0250: 64 20 74 68 69 73 20 70 65 72 6d 69 73 73 69 6f  d this permissio
0260: 6e 20 6e 6f 74 69 63 65 20 73 68 61 6c 6c 20 62  n notice shall b
0270: 65 20 69 6e 63 6c 75 64 65 64 0a 20 2a 20 69 6e  e included. * in
0280: 20 61 6c 6c 20 63 6f 70 69 65 73 20 6f 72 20 73   all copies or s
0290: 75 62 73 74 61 6e 74 69 61 6c 20 70 6f 72 74 69  ubstantial porti
02a0: 6f 6e 73 20 6f 66 20 74 68 65 20 53 6f 66 74 77  ons of the Softw
02b0: 61 72 65 2e 0a 20 2a 0a 20 2a 20 54 48 45 20 53  are.. *. * THE S
02c0: 4f 46 54 57 41 52 45 20 49 53 20 50 52 4f 56 49  OFTWARE IS PROVI
02d0: 44 45 44 20 60 60 41 53 20 49 53 27 27 2c 20 57  DED ``AS IS'', W
02e0: 49 54 48 4f 55 54 20 57 41 52 52 41 4e 54 59 20  ITHOUT WARRANTY 
02f0: 4f 46 20 41 4e 59 20 4b 49 4e 44 2c 20 45 58 50  OF ANY KIND, EXP
0300: 52 45 53 53 0a 20 2a 20 4f 52 20 49 4d 50 4c 49  RESS. * OR IMPLI
0310: 45 44 2c 20 49 4e 43 4c 55 44 49 4e 47 20 42 55  ED, INCLUDING BU
0320: 54 20 4e 4f 54 20 4c 49 4d 49 54 45 44 20 54 4f  T NOT LIMITED TO
0330: 20 54 48 45 20 57 41 52 52 41 4e 54 49 45 53 20   THE WARRANTIES 
0340: 4f 46 0a 20 2a 20 4d 45 52 43 48 41 4e 54 41 42  OF. * MERCHANTAB
0350: 49 4c 49 54 59 2c 20 46 49 54 4e 45 53 53 20 46  ILITY, FITNESS F
0360: 4f 52 20 41 20 50 41 52 54 49 43 55 4c 41 52 20  OR A PARTICULAR 
0370: 50 55 52 50 4f 53 45 20 41 4e 44 20 4e 4f 4e 49  PURPOSE AND NONI
0380: 4e 46 52 49 4e 47 45 4d 45 4e 54 2e 0a 20 2a 20  NFRINGEMENT.. * 
0390: 49 4e 20 4e 4f 20 45 56 45 4e 54 20 53 48 41 4c  IN NO EVENT SHAL
03a0: 4c 20 54 4f 4e 49 20 52 4f 4e 4b 4b 4f 20 42 45  L TONI RONKKO BE
03b0: 20 4c 49 41 42 4c 45 20 46 4f 52 20 41 4e 59 20   LIABLE FOR ANY 
03c0: 43 4c 41 49 4d 2c 20 44 41 4d 41 47 45 53 20 4f  CLAIM, DAMAGES O
03d0: 52 0a 20 2a 20 4f 54 48 45 52 20 4c 49 41 42 49  R. * OTHER LIABI
03e0: 4c 49 54 59 2c 20 57 48 45 54 48 45 52 20 49 4e  LITY, WHETHER IN
03f0: 20 41 4e 20 41 43 54 49 4f 4e 20 4f 46 20 43 4f   AN ACTION OF CO
0400: 4e 54 52 41 43 54 2c 20 54 4f 52 54 20 4f 52 20  NTRACT, TORT OR 
0410: 4f 54 48 45 52 57 49 53 45 2c 0a 20 2a 20 41 52  OTHERWISE,. * AR
0420: 49 53 49 4e 47 20 46 52 4f 4d 2c 20 4f 55 54 20  ISING FROM, OUT 
0430: 4f 46 20 4f 52 20 49 4e 20 43 4f 4e 4e 45 43 54  OF OR IN CONNECT
0440: 49 4f 4e 20 57 49 54 48 20 54 48 45 20 53 4f 46  ION WITH THE SOF
0450: 54 57 41 52 45 20 4f 52 20 54 48 45 20 55 53 45  TWARE OR THE USE
0460: 20 4f 52 0a 20 2a 20 4f 54 48 45 52 20 44 45 41   OR. * OTHER DEA
0470: 4c 49 4e 47 53 20 49 4e 20 54 48 45 20 53 4f 46  LINGS IN THE SOF
0480: 54 57 41 52 45 2e 0a 20 2a 0a 20 2a 0a 20 2a 20  TWARE.. *. *. * 
0490: 56 65 72 73 69 6f 6e 20 31 2e 31 33 2c 20 44 65  Version 1.13, De
04a0: 63 20 31 32 20 32 30 31 32 2c 20 54 6f 6e 69 20  c 12 2012, Toni 
04b0: 52 6f 6e 6b 6b 6f 0a 20 2a 20 55 73 65 20 74 72  Ronkko. * Use tr
04c0: 61 64 69 74 69 6f 6e 61 6c 20 38 2b 33 20 66 69  aditional 8+3 fi
04d0: 6c 65 20 6e 61 6d 65 20 69 66 20 74 68 65 20 6e  le name if the n
04e0: 61 6d 65 20 63 61 6e 6e 6f 74 20 62 65 20 72 65  ame cannot be re
04f0: 70 72 65 73 65 6e 74 65 64 20 69 6e 20 74 68 65  presented in the
0500: 0a 20 2a 20 64 65 66 61 75 6c 74 20 41 4e 53 49  . * default ANSI
0510: 20 63 6f 64 65 20 70 61 67 65 2e 20 20 4e 6f 77   code page.  Now
0520: 20 63 6f 6d 70 69 6c 65 73 20 61 67 61 69 6e 20   compiles again 
0530: 77 69 74 68 20 4d 53 56 43 20 36 2e 30 2e 20 20  with MSVC 6.0.  
0540: 54 68 61 6e 6b 73 20 74 6f 0a 20 2a 20 4b 6f 6e  Thanks to. * Kon
0550: 73 74 61 6e 74 69 6e 20 4b 68 6f 6d 6f 75 74 6f  stantin Khomouto
0560: 76 20 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 20  v for testing.. 
0570: 2a 0a 20 2a 20 56 65 72 73 69 6f 6e 20 31 2e 31  *. * Version 1.1
0580: 32 2e 31 2c 20 4f 63 74 20 31 20 32 30 31 32 2c  2.1, Oct 1 2012,
0590: 20 54 6f 6e 69 20 52 6f 6e 6b 6b 6f 0a 20 2a 20   Toni Ronkko. * 
05a0: 42 75 67 20 66 69 78 3a 20 72 65 6e 61 6d 65 64  Bug fix: renamed
05b0: 20 77 69 64 65 2d 63 68 61 72 61 63 74 65 72 20   wide-character 
05c0: 44 49 52 20 73 74 72 75 63 74 75 72 65 20 5f 77  DIR structure _w
05d0: 44 49 52 20 74 6f 20 5f 57 44 49 52 20 28 77 69  DIR to _WDIR (wi
05e0: 74 68 0a 20 2a 20 63 61 70 69 74 61 6c 20 57 29  th. * capital W)
05f0: 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61 69   in order to mai
0600: 6e 74 61 69 6e 20 63 6f 6d 70 61 74 69 62 69 6c  ntain compatibil
0610: 69 74 79 20 77 69 74 68 20 4d 69 6e 67 57 2e 0a  ity with MingW..
0620: 20 2a 0a 20 2a 20 56 65 72 73 69 6f 6e 20 31 2e   *. * Version 1.
0630: 31 32 2c 20 53 65 70 20 33 30 20 32 30 31 32 2c  12, Sep 30 2012,
0640: 20 54 6f 6e 69 20 52 6f 6e 6b 6b 6f 0a 20 2a 20   Toni Ronkko. * 
0650: 44 65 66 69 6e 65 20 50 41 54 48 5f 4d 41 58 20  Define PATH_MAX 
0660: 61 6e 64 20 4e 41 4d 45 5f 4d 41 58 2e 20 20 41  and NAME_MAX.  A
0670: 64 64 65 64 20 77 69 64 65 2d 63 68 61 72 61 63  dded wide-charac
0680: 74 65 72 20 76 61 72 69 61 6e 74 73 20 5f 77 44  ter variants _wD
0690: 49 52 2c 20 0a 20 2a 20 5f 77 64 69 72 65 6e 74  IR, . * _wdirent
06a0: 2c 20 5f 77 6f 70 65 6e 64 69 72 28 29 2c 20 5f  , _wopendir(), _
06b0: 77 72 65 61 64 64 69 72 28 29 2c 20 5f 77 63 6c  wreaddir(), _wcl
06c0: 6f 73 65 64 69 72 28 29 20 61 6e 64 20 5f 77 72  osedir() and _wr
06d0: 65 77 69 6e 64 64 69 72 28 29 2e 0a 20 2a 20 54  ewinddir().. * T
06e0: 68 61 6e 6b 73 20 74 6f 20 45 64 67 61 72 20 42  hanks to Edgar B
06f0: 75 65 72 6b 6c 65 20 61 6e 64 20 4a 61 6e 20 4e  uerkle and Jan N
0700: 69 6a 74 6d 61 6e 73 20 66 6f 72 20 69 64 65 61  ijtmans for idea
0710: 73 20 61 6e 64 20 63 6f 64 65 2e 0a 20 2a 0a 20  s and code.. *. 
0720: 2a 20 44 6f 20 6e 6f 74 20 69 6e 63 6c 75 64 65  * Do not include
0730: 20 77 69 6e 64 6f 77 73 2e 68 2e 20 20 54 68 69   windows.h.  Thi
0740: 73 20 61 6c 6c 6f 77 73 20 64 69 72 65 6e 74 2e  s allows dirent.
0750: 68 20 74 6f 20 62 65 20 69 6e 74 65 67 72 61 74  h to be integrat
0760: 65 64 20 6d 6f 72 65 0a 20 2a 20 65 61 73 69 6c  ed more. * easil
0770: 79 20 69 6e 74 6f 20 70 72 6f 67 72 61 6d 73 20  y into programs 
0780: 75 73 69 6e 67 20 77 69 6e 73 6f 63 6b 2e 20 20  using winsock.  
0790: 54 68 61 6e 6b 73 20 74 6f 20 46 65 72 6e 61 6e  Thanks to Fernan
07a0: 64 6f 20 41 7a 61 6c 64 65 67 75 69 2e 0a 20 2a  do Azaldegui.. *
07b0: 0a 20 2a 20 56 65 72 73 69 6f 6e 20 31 2e 31 31  . * Version 1.11
07c0: 2c 20 4d 61 72 20 31 35 2c 20 32 30 31 31 2c 20  , Mar 15, 2011, 
07d0: 54 6f 6e 69 20 52 6f 6e 6b 6b 6f 0a 20 2a 20 44  Toni Ronkko. * D
07e0: 65 66 69 6e 65 64 20 46 49 4c 45 5f 41 54 54 52  efined FILE_ATTR
07f0: 49 42 55 54 45 5f 44 45 56 49 43 45 20 66 6f 72  IBUTE_DEVICE for
0800: 20 4d 53 56 43 20 36 2e 30 2e 0a 20 2a 0a 20 2a   MSVC 6.0.. *. *
0810: 20 56 65 72 73 69 6f 6e 20 31 2e 31 30 2c 20 41   Version 1.10, A
0820: 75 67 20 31 31 2c 20 32 30 31 30 2c 20 54 6f 6e  ug 11, 2010, Ton
0830: 69 20 52 6f 6e 6b 6b 6f 0a 20 2a 20 41 64 64 65  i Ronkko. * Adde
0840: 64 20 64 5f 74 79 70 65 20 61 6e 64 20 64 5f 6e  d d_type and d_n
0850: 61 6d 6c 65 6e 20 66 69 65 6c 64 73 20 74 6f 20  amlen fields to 
0860: 64 69 72 65 6e 74 20 73 74 72 75 63 74 75 72 65  dirent structure
0870: 2e 20 20 54 68 65 20 66 6f 72 6d 65 72 20 69 73  .  The former is
0880: 0a 20 2a 20 65 73 70 65 63 69 61 6c 6c 79 20 75  . * especially u
0890: 73 65 66 75 6c 20 66 6f 72 20 64 65 74 65 72 6d  seful for determ
08a0: 69 6e 69 6e 67 20 77 68 65 74 68 65 72 20 64 69  ining whether di
08b0: 72 65 63 74 6f 72 79 20 65 6e 74 72 79 20 72 65  rectory entry re
08c0: 70 72 65 73 65 6e 74 73 20 61 0a 20 2a 20 66 69  presents a. * fi
08d0: 6c 65 20 6f 72 20 61 20 64 69 72 65 63 74 6f 72  le or a director
08e0: 79 2e 20 20 46 6f 72 20 6d 6f 72 65 20 69 6e 66  y.  For more inf
08f0: 6f 72 6d 61 74 69 6f 6e 2c 20 73 65 65 0a 20 2a  ormation, see. *
0900: 20 68 74 74 70 3a 2f 2f 77 77 77 2e 64 65 6c 6f   http://www.delo
0910: 72 69 65 2e 63 6f 6d 2f 67 6e 75 2f 64 6f 63 73  rie.com/gnu/docs
0920: 2f 67 6c 69 62 63 2f 6c 69 62 63 5f 32 37 30 2e  /glibc/libc_270.
0930: 68 74 6d 6c 0a 20 2a 0a 20 2a 20 49 6d 70 72 6f  html. *. * Impro
0940: 76 65 64 20 63 6f 6e 66 6f 72 6d 61 6e 63 65 20  ved conformance 
0950: 74 6f 20 74 68 65 20 73 74 61 6e 64 61 72 64 73  to the standards
0960: 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  .  For example, 
0970: 65 72 72 6e 6f 20 69 73 20 6e 6f 77 20 73 65 74  errno is now set
0980: 0a 20 2a 20 70 72 6f 70 65 72 6c 79 20 6f 6e 20  . * properly on 
0990: 66 61 69 6c 75 72 65 20 61 6e 64 20 61 73 73 65  failure and asse
09a0: 72 74 28 29 20 69 73 20 6e 65 76 65 72 20 75 73  rt() is never us
09b0: 65 64 2e 20 20 54 68 61 6e 6b 73 20 74 6f 20 50  ed.  Thanks to P
09c0: 65 74 65 72 20 42 72 6f 63 6b 61 6d 0a 20 2a 20  eter Brockam. * 
09d0: 66 6f 72 20 73 75 67 67 65 73 74 69 6f 6e 73 2e  for suggestions.
09e0: 0a 20 2a 0a 20 2a 20 46 69 78 65 64 20 61 20 62  . *. * Fixed a b
09f0: 75 67 20 69 6e 20 72 65 77 69 6e 64 64 69 72 28  ug in rewinddir(
0a00: 29 3a 20 77 68 65 6e 20 75 73 69 6e 67 20 72 65  ): when using re
0a10: 6c 61 74 69 76 65 20 64 69 72 65 63 74 6f 72 79  lative directory
0a20: 20 6e 61 6d 65 73 2c 20 63 68 61 6e 67 65 0a 20   names, change. 
0a30: 2a 20 6f 66 20 77 6f 72 6b 69 6e 67 20 64 69 72  * of working dir
0a40: 65 63 74 6f 72 79 20 6e 6f 20 6c 6f 6e 67 65 72  ectory no longer
0a50: 20 63 61 75 73 65 73 20 72 65 77 69 6e 64 64 69   causes rewinddi
0a60: 72 28 29 20 74 6f 20 66 61 69 6c 2e 0a 20 2a 0a  r() to fail.. *.
0a70: 20 2a 20 56 65 72 73 69 6f 6e 20 31 2e 39 2c 20   * Version 1.9, 
0a80: 44 65 63 20 31 35 2c 20 32 30 30 39 2c 20 4a 6f  Dec 15, 2009, Jo
0a90: 68 6e 20 43 75 6e 6e 69 6e 67 68 61 6d 0a 20 2a  hn Cunningham. *
0aa0: 20 41 64 64 65 64 20 72 65 77 69 6e 64 64 69 72   Added rewinddir
0ab0: 20 6d 65 6d 62 65 72 20 66 75 6e 63 74 69 6f 6e   member function
0ac0: 0a 20 2a 0a 20 2a 20 56 65 72 73 69 6f 6e 20 31  . *. * Version 1
0ad0: 2e 38 2c 20 4a 61 6e 20 31 38 2c 20 32 30 30 38  .8, Jan 18, 2008
0ae0: 2c 20 54 6f 6e 69 20 52 6f 6e 6b 6b 6f 0a 20 2a  , Toni Ronkko. *
0af0: 20 55 73 69 6e 67 20 46 69 6e 64 46 69 72 73 74   Using FindFirst
0b00: 46 69 6c 65 41 20 61 6e 64 20 57 49 4e 33 32 5f  FileA and WIN32_
0b10: 46 49 4e 44 5f 44 41 54 41 41 20 74 6f 20 61 76  FIND_DATAA to av
0b20: 6f 69 64 20 63 6f 6e 76 65 72 74 69 6e 67 20 73  oid converting s
0b30: 74 72 69 6e 67 0a 20 2a 20 62 65 74 77 65 65 6e  tring. * between
0b40: 20 6d 75 6c 74 69 2d 62 79 74 65 20 61 6e 64 20   multi-byte and 
0b50: 75 6e 69 63 6f 64 65 20 72 65 70 72 65 73 65 6e  unicode represen
0b60: 74 61 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 6d  tations.  This m
0b70: 61 6b 65 73 20 74 68 65 0a 20 2a 20 63 6f 64 65  akes the. * code
0b80: 20 73 69 6d 70 6c 65 72 20 61 6e 64 20 61 6c 73   simpler and als
0b90: 6f 20 61 6c 6c 6f 77 73 20 74 68 65 20 63 6f 64  o allows the cod
0ba0: 65 20 74 6f 20 62 65 20 63 6f 6d 70 69 6c 65 64  e to be compiled
0bb0: 20 75 6e 64 65 72 20 4d 69 6e 67 57 2e 20 20 54   under MingW.  T
0bc0: 68 61 6e 6b 73 0a 20 2a 20 74 6f 20 41 7a 72 69  hanks. * to Azri
0bd0: 65 6c 20 46 61 73 74 65 6e 20 66 6f 72 20 74 68  el Fasten for th
0be0: 65 20 73 75 67 67 65 73 74 69 6f 6e 2e 0a 20 2a  e suggestion.. *
0bf0: 0a 20 2a 20 4d 61 72 20 34 2c 20 32 30 30 37 2c  . * Mar 4, 2007,
0c00: 20 54 6f 6e 69 20 52 6f 6e 6b 6b 6f 0a 20 2a 20   Toni Ronkko. * 
0c10: 42 75 67 20 66 69 78 3a 20 64 75 65 20 74 6f 20  Bug fix: due to 
0c20: 74 68 65 20 73 74 72 6e 63 70 79 5f 73 28 29 20  the strncpy_s() 
0c30: 66 75 6e 63 74 69 6f 6e 20 74 68 69 73 20 66 69  function this fi
0c40: 6c 65 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64  le only compiled
0c50: 20 69 6e 0a 20 2a 20 56 69 73 75 61 6c 20 53 74   in. * Visual St
0c60: 75 64 69 6f 20 32 30 30 35 2e 20 20 55 73 69 6e  udio 2005.  Usin
0c70: 67 20 74 68 65 20 6e 65 77 20 73 74 72 69 6e 67  g the new string
0c80: 20 66 75 6e 63 74 69 6f 6e 73 20 6f 6e 6c 79 20   functions only 
0c90: 77 68 65 6e 20 74 68 65 0a 20 2a 20 63 6f 6d 70  when the. * comp
0ca0: 69 6c 65 72 20 76 65 72 73 69 6f 6e 20 61 6c 6c  iler version all
0cb0: 6f 77 73 2e 0a 20 2a 0a 20 2a 20 4e 6f 76 20 20  ows.. *. * Nov  
0cc0: 32 2c 20 32 30 30 36 2c 20 54 6f 6e 69 20 52 6f  2, 2006, Toni Ro
0cd0: 6e 6b 6b 6f 0a 20 2a 20 4d 61 6a 6f 72 20 75 70  nkko. * Major up
0ce0: 64 61 74 65 3a 20 72 65 6d 6f 76 65 64 20 73 75  date: removed su
0cf0: 70 70 6f 72 74 20 66 6f 72 20 57 61 74 63 6f 6d  pport for Watcom
0d00: 20 43 2c 20 4d 53 2d 44 4f 53 20 61 6e 64 20 54   C, MS-DOS and T
0d10: 75 72 62 6f 20 43 20 74 6f 0a 20 2a 20 73 69 6d  urbo C to. * sim
0d20: 70 6c 69 66 79 20 74 68 65 20 66 69 6c 65 2c 20  plify the file, 
0d30: 75 70 64 61 74 65 64 20 74 68 65 20 63 6f 64 65  updated the code
0d40: 20 74 6f 20 63 6f 6d 70 69 6c 65 20 63 6c 65 61   to compile clea
0d50: 6e 6c 79 20 6f 6e 20 56 69 73 75 61 6c 0a 20 2a  nly on Visual. *
0d60: 20 53 74 75 64 69 6f 20 32 30 30 35 20 77 69 74   Studio 2005 wit
0d70: 68 20 62 6f 74 68 20 75 6e 69 63 6f 64 65 20 61  h both unicode a
0d80: 6e 64 20 6d 75 6c 74 69 2d 62 79 74 65 20 63 68  nd multi-byte ch
0d90: 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 73 2c  aracter strings,
0da0: 0a 20 2a 20 72 65 6d 6f 76 65 64 20 72 65 77 69  . * removed rewi
0db0: 6e 64 64 69 72 28 29 20 61 73 20 69 74 20 68 61  nddir() as it ha
0dc0: 64 20 61 20 62 75 67 2e 0a 20 2a 0a 20 2a 20 41  d a bug.. *. * A
0dd0: 75 67 20 32 30 2c 20 32 30 30 36 2c 20 54 6f 6e  ug 20, 2006, Ton
0de0: 69 20 52 6f 6e 6b 6b 6f 0a 20 2a 20 52 65 6d 6f  i Ronkko. * Remo
0df0: 76 65 64 20 61 6c 6c 20 72 65 6d 61 72 6b 73 20  ved all remarks 
0e00: 61 62 6f 75 74 20 4d 53 56 43 20 31 2e 30 2c 20  about MSVC 1.0, 
0e10: 77 68 69 63 68 20 69 73 20 61 6e 74 69 71 75 65  which is antique
0e20: 64 20 6e 6f 77 2e 20 20 53 69 6d 70 6c 69 66 69  d now.  Simplifi
0e30: 65 64 0a 20 2a 20 63 6f 6d 6d 65 6e 74 73 20 62  ed. * comments b
0e40: 79 20 72 65 6d 6f 76 69 6e 67 20 53 47 4d 4c 20  y removing SGML 
0e50: 74 61 67 73 2e 0a 20 2a 0a 20 2a 20 4d 61 79 20  tags.. *. * May 
0e60: 31 34 20 32 30 30 32 2c 20 54 6f 6e 69 20 52 6f  14 2002, Toni Ro
0e70: 6e 6b 6b 6f 0a 20 2a 20 45 6d 62 65 64 64 65 64  nkko. * Embedded
0e80: 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 65   the function de
0e90: 66 69 6e 69 74 69 6f 6e 73 20 64 69 72 65 63 74  finitions direct
0ea0: 6c 79 20 74 6f 20 74 68 65 20 68 65 61 64 65 72  ly to the header
0eb0: 20 73 6f 20 74 68 61 74 20 6e 6f 0a 20 2a 20 73   so that no. * s
0ec0: 6f 75 72 63 65 20 6d 6f 64 75 6c 65 73 20 6e 65  ource modules ne
0ed0: 65 64 20 74 6f 20 62 65 20 69 6e 63 6c 75 64 65  ed to be include
0ee0: 64 20 69 6e 20 74 68 65 20 56 69 73 75 61 6c 20  d in the Visual 
0ef0: 53 74 75 64 69 6f 20 70 72 6f 6a 65 63 74 2e 20  Studio project. 
0f00: 20 52 65 6d 6f 76 65 64 0a 20 2a 20 61 6c 6c 20   Removed. * all 
0f10: 74 68 65 20 64 65 70 65 6e 64 65 6e 63 69 65 73  the dependencies
0f20: 20 74 6f 20 6f 74 68 65 72 20 70 72 6f 6a 65 63   to other projec
0f30: 74 73 20 73 6f 20 74 68 61 74 20 74 68 69 73 20  ts so that this 
0f40: 68 65 61 64 65 72 20 66 69 6c 65 20 63 61 6e 20  header file can 
0f50: 62 65 0a 20 2a 20 75 73 65 64 20 69 6e 64 65 70  be. * used indep
0f60: 65 6e 64 65 6e 74 6c 79 2e 0a 20 2a 0a 20 2a 20  endently.. *. * 
0f70: 4d 61 79 20 32 38 20 31 39 39 38 2c 20 54 6f 6e  May 28 1998, Ton
0f80: 69 20 52 6f 6e 6b 6b 6f 0a 20 2a 20 46 69 72 73  i Ronkko. * Firs
0f90: 74 20 76 65 72 73 69 6f 6e 2e 0a 20 2a 2a 2a 2a  t version.. ****
0fa0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0fb0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0fc0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0fd0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0fe0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69 66 6e 64  *********/.#ifnd
0ff0: 65 66 20 44 49 52 45 4e 54 5f 48 0a 23 64 65 66  ef DIRENT_H.#def
1000: 69 6e 65 20 44 49 52 45 4e 54 5f 48 0a 0a 23 69  ine DIRENT_H..#i
1010: 66 20 21 64 65 66 69 6e 65 64 28 5f 36 38 4b 5f  f !defined(_68K_
1020: 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f 4d  ) && !defined(_M
1030: 50 50 43 5f 29 20 26 26 20 21 64 65 66 69 6e 65  PPC_) && !define
1040: 64 28 5f 58 38 36 5f 29 20 26 26 20 21 64 65 66  d(_X86_) && !def
1050: 69 6e 65 64 28 5f 49 41 36 34 5f 29 20 26 26 20  ined(_IA64_) && 
1060: 21 64 65 66 69 6e 65 64 28 5f 41 4d 44 36 34 5f  !defined(_AMD64_
1070: 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 4d 5f  ) && defined(_M_
1080: 49 58 38 36 29 0a 23 20 20 20 64 65 66 69 6e 65  IX86).#   define
1090: 20 5f 58 38 36 5f 0a 23 65 6e 64 69 66 0a 23 69   _X86_.#endif.#i
10a0: 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e 68 3e  nclude <stdio.h>
10b0: 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 61 72  .#include <stdar
10c0: 67 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 77  g.h>.#include <w
10d0: 69 6e 64 65 66 2e 68 3e 0a 23 69 6e 63 6c 75 64  indef.h>.#includ
10e0: 65 20 3c 77 69 6e 62 61 73 65 2e 68 3e 0a 23 69  e <winbase.h>.#i
10f0: 6e 63 6c 75 64 65 20 3c 77 63 68 61 72 2e 68 3e  nclude <wchar.h>
1100: 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 72 69 6e  .#include <strin
1110: 67 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73  g.h>.#include <s
1120: 74 64 6c 69 62 2e 68 3e 0a 23 69 6e 63 6c 75 64  tdlib.h>.#includ
1130: 65 20 3c 6d 61 6c 6c 6f 63 2e 68 3e 0a 23 69 6e  e <malloc.h>.#in
1140: 63 6c 75 64 65 20 3c 73 79 73 2f 74 79 70 65 73  clude <sys/types
1150: 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79  .h>.#include <sy
1160: 73 2f 73 74 61 74 2e 68 3e 0a 23 69 6e 63 6c 75  s/stat.h>.#inclu
1170: 64 65 20 3c 65 72 72 6e 6f 2e 68 3e 0a 0a 2f 2a  de <errno.h>../*
1180: 20 49 6e 64 69 63 61 74 65 73 20 74 68 61 74 20   Indicates that 
1190: 64 5f 74 79 70 65 20 66 69 65 6c 64 20 69 73 20  d_type field is 
11a0: 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 64 69 72  available in dir
11b0: 65 6e 74 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ent structure */
11c0: 0a 23 64 65 66 69 6e 65 20 5f 44 49 52 45 4e 54  .#define _DIRENT
11d0: 5f 48 41 56 45 5f 44 5f 54 59 50 45 0a 0a 2f 2a  _HAVE_D_TYPE../*
11e0: 20 49 6e 64 69 63 61 74 65 73 20 74 68 61 74 20   Indicates that 
11f0: 64 5f 6e 61 6d 6c 65 6e 20 66 69 65 6c 64 20 69  d_namlen field i
1200: 73 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 64  s available in d
1210: 69 72 65 6e 74 20 73 74 72 75 63 74 75 72 65 20  irent structure 
1220: 2a 2f 0a 23 64 65 66 69 6e 65 20 5f 44 49 52 45  */.#define _DIRE
1230: 4e 54 5f 48 41 56 45 5f 44 5f 4e 41 4d 4c 45 4e  NT_HAVE_D_NAMLEN
1240: 0a 0a 2f 2a 20 45 6e 74 72 69 65 73 20 6d 69 73  ../* Entries mis
1250: 73 69 6e 67 20 66 72 6f 6d 20 4d 53 56 43 20 36  sing from MSVC 6
1260: 2e 30 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  .0 */.#if !defin
1270: 65 64 28 46 49 4c 45 5f 41 54 54 52 49 42 55 54  ed(FILE_ATTRIBUT
1280: 45 5f 44 45 56 49 43 45 29 0a 23 20 20 20 64 65  E_DEVICE).#   de
1290: 66 69 6e 65 20 46 49 4c 45 5f 41 54 54 52 49 42  fine FILE_ATTRIB
12a0: 55 54 45 5f 44 45 56 49 43 45 20 30 78 34 30 0a  UTE_DEVICE 0x40.
12b0: 23 65 6e 64 69 66 0a 0a 2f 2a 20 46 69 6c 65 20  #endif../* File 
12c0: 74 79 70 65 20 61 6e 64 20 70 65 72 6d 69 73 73  type and permiss
12d0: 69 6f 6e 20 66 6c 61 67 73 20 66 6f 72 20 73 74  ion flags for st
12e0: 61 74 28 29 20 2a 2f 0a 23 69 66 20 21 64 65 66  at() */.#if !def
12f0: 69 6e 65 64 28 53 5f 49 46 4d 54 29 0a 23 20 20  ined(S_IFMT).#  
1300: 20 64 65 66 69 6e 65 20 53 5f 49 46 4d 54 20 20   define S_IFMT  
1310: 20 5f 53 5f 49 46 4d 54 20 20 20 20 20 20 20 20   _S_IFMT        
1320: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1330: 46 69 6c 65 20 74 79 70 65 20 6d 61 73 6b 20 2a  File type mask *
1340: 2f 0a 23 65 6e 64 69 66 0a 23 69 66 20 21 64 65  /.#endif.#if !de
1350: 66 69 6e 65 64 28 53 5f 49 46 44 49 52 29 0a 23  fined(S_IFDIR).#
1360: 20 20 20 64 65 66 69 6e 65 20 53 5f 49 46 44 49     define S_IFDI
1370: 52 20 20 5f 53 5f 49 46 44 49 52 20 20 20 20 20  R  _S_IFDIR     
1380: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1390: 2a 20 44 69 72 65 63 74 6f 72 79 20 2a 2f 0a 23  * Directory */.#
13a0: 65 6e 64 69 66 0a 23 69 66 20 21 64 65 66 69 6e  endif.#if !defin
13b0: 65 64 28 53 5f 49 46 43 48 52 29 0a 23 20 20 20  ed(S_IFCHR).#   
13c0: 64 65 66 69 6e 65 20 53 5f 49 46 43 48 52 20 20  define S_IFCHR  
13d0: 5f 53 5f 49 46 43 48 52 20 20 20 20 20 20 20 20  _S_IFCHR        
13e0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
13f0: 68 61 72 61 63 74 65 72 20 64 65 76 69 63 65 20  haracter device 
1400: 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 20 21 64  */.#endif.#if !d
1410: 65 66 69 6e 65 64 28 53 5f 49 46 46 49 46 4f 29  efined(S_IFFIFO)
1420: 0a 23 20 20 20 64 65 66 69 6e 65 20 53 5f 49 46  .#   define S_IF
1430: 46 49 46 4f 20 5f 53 5f 49 46 46 49 46 4f 20 20  FIFO _S_IFFIFO  
1440: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1450: 20 2f 2a 20 50 69 70 65 20 2a 2f 0a 23 65 6e 64   /* Pipe */.#end
1460: 69 66 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  if.#if !defined(
1470: 53 5f 49 46 52 45 47 29 0a 23 20 20 20 64 65 66  S_IFREG).#   def
1480: 69 6e 65 20 53 5f 49 46 52 45 47 20 20 5f 53 5f  ine S_IFREG  _S_
1490: 49 46 52 45 47 20 20 20 20 20 20 20 20 20 20 20  IFREG           
14a0: 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 75           /* Regu
14b0: 6c 61 72 20 66 69 6c 65 20 2a 2f 0a 23 65 6e 64  lar file */.#end
14c0: 69 66 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  if.#if !defined(
14d0: 53 5f 49 52 45 41 44 29 0a 23 20 20 20 64 65 66  S_IREAD).#   def
14e0: 69 6e 65 20 53 5f 49 52 45 41 44 20 20 5f 53 5f  ine S_IREAD  _S_
14f0: 49 52 45 41 44 20 20 20 20 20 20 20 20 20 20 20  IREAD           
1500: 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 61 64           /* Read
1510: 20 70 65 72 6d 69 73 73 69 6f 6e 20 2a 2f 0a 23   permission */.#
1520: 65 6e 64 69 66 0a 23 69 66 20 21 64 65 66 69 6e  endif.#if !defin
1530: 65 64 28 53 5f 49 57 52 49 54 45 29 0a 23 20 20  ed(S_IWRITE).#  
1540: 20 64 65 66 69 6e 65 20 53 5f 49 57 52 49 54 45   define S_IWRITE
1550: 20 5f 53 5f 49 57 52 49 54 45 20 20 20 20 20 20   _S_IWRITE      
1560: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1570: 57 72 69 74 65 20 70 65 72 6d 69 73 73 69 6f 6e  Write permission
1580: 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 20 21   */.#endif.#if !
1590: 64 65 66 69 6e 65 64 28 53 5f 49 45 58 45 43 29  defined(S_IEXEC)
15a0: 0a 23 20 20 20 64 65 66 69 6e 65 20 53 5f 49 45  .#   define S_IE
15b0: 58 45 43 20 20 5f 53 5f 49 45 58 45 43 20 20 20  XEC  _S_IEXEC   
15c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d0: 20 2f 2a 20 45 78 65 63 75 74 65 20 70 65 72 6d   /* Execute perm
15e0: 69 73 73 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66  ission */.#endif
15f0: 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 5f  .#if !defined(S_
1600: 49 46 49 46 4f 29 0a 23 20 20 20 64 65 66 69 6e  IFIFO).#   defin
1610: 65 20 53 5f 49 46 49 46 4f 20 5f 53 5f 49 46 49  e S_IFIFO _S_IFI
1620: 46 4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  FO              
1630: 20 20 20 20 20 20 20 2f 2a 20 50 69 70 65 20 2a         /* Pipe *
1640: 2f 0a 23 65 6e 64 69 66 0a 23 69 66 20 21 64 65  /.#endif.#if !de
1650: 66 69 6e 65 64 28 53 5f 49 46 42 4c 4b 29 0a 23  fined(S_IFBLK).#
1660: 20 20 20 64 65 66 69 6e 65 20 53 5f 49 46 42 4c     define S_IFBL
1670: 4b 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20  K   0           
1680: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1690: 2a 20 42 6c 6f 63 6b 20 64 65 76 69 63 65 20 2a  * Block device *
16a0: 2f 0a 23 65 6e 64 69 66 0a 23 69 66 20 21 64 65  /.#endif.#if !de
16b0: 66 69 6e 65 64 28 53 5f 49 46 4c 4e 4b 29 0a 23  fined(S_IFLNK).#
16c0: 20 20 20 64 65 66 69 6e 65 20 53 5f 49 46 4c 4e     define S_IFLN
16d0: 4b 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20  K   0           
16e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
16f0: 2a 20 4c 69 6e 6b 20 2a 2f 0a 23 65 6e 64 69 66  * Link */.#endif
1700: 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 5f  .#if !defined(S_
1710: 49 46 53 4f 43 4b 29 0a 23 20 20 20 64 65 66 69  IFSOCK).#   defi
1720: 6e 65 20 53 5f 49 46 53 4f 43 4b 20 20 30 20 20  ne S_IFSOCK  0  
1730: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1740: 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 63 6b 65          /* Socke
1750: 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 23 69 66  t */.#endif..#if
1760: 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45   defined(_MSC_VE
1770: 52 29 0a 23 20 20 20 64 65 66 69 6e 65 20 53 5f  R).#   define S_
1780: 49 52 55 53 52 20 20 53 5f 49 52 45 41 44 20 20  IRUSR  S_IREAD  
1790: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17a0: 20 20 20 2f 2a 20 52 65 61 64 20 75 73 65 72 20     /* Read user 
17b0: 2a 2f 0a 23 20 20 20 64 65 66 69 6e 65 20 53 5f  */.#   define S_
17c0: 49 57 55 53 52 20 20 53 5f 49 57 52 49 54 45 20  IWUSR  S_IWRITE 
17d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17e0: 20 20 20 2f 2a 20 57 72 69 74 65 20 75 73 65 72     /* Write user
17f0: 20 2a 2f 0a 23 20 20 20 64 65 66 69 6e 65 20 53   */.#   define S
1800: 5f 49 58 55 53 52 20 20 30 20 20 20 20 20 20 20  _IXUSR  0       
1810: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1820: 20 20 20 20 2f 2a 20 45 78 65 63 75 74 65 20 75      /* Execute u
1830: 73 65 72 20 2a 2f 0a 23 20 20 20 64 65 66 69 6e  ser */.#   defin
1840: 65 20 53 5f 49 52 47 52 50 20 20 30 20 20 20 20  e S_IRGRP  0    
1850: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1860: 20 20 20 20 20 20 20 2f 2a 20 52 65 61 64 20 67         /* Read g
1870: 72 6f 75 70 20 2a 2f 0a 23 20 20 20 64 65 66 69  roup */.#   defi
1880: 6e 65 20 53 5f 49 57 47 52 50 20 20 30 20 20 20  ne S_IWGRP  0   
1890: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18a0: 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65          /* Write
18b0: 20 67 72 6f 75 70 20 2a 2f 0a 23 20 20 20 64 65   group */.#   de
18c0: 66 69 6e 65 20 53 5f 49 58 47 52 50 20 20 30 20  fine S_IXGRP  0 
18d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18e0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 65            /* Exe
18f0: 63 75 74 65 20 67 72 6f 75 70 20 2a 2f 0a 23 20  cute group */.# 
1900: 20 20 64 65 66 69 6e 65 20 53 5f 49 52 4f 54 48    define S_IROTH
1910: 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20    0             
1920: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1930: 20 52 65 61 64 20 6f 74 68 65 72 73 20 2a 2f 0a   Read others */.
1940: 23 20 20 20 64 65 66 69 6e 65 20 53 5f 49 57 4f  #   define S_IWO
1950: 54 48 20 20 30 20 20 20 20 20 20 20 20 20 20 20  TH  0           
1960: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1970: 2f 2a 20 57 72 69 74 65 20 6f 74 68 65 72 73 20  /* Write others 
1980: 2a 2f 0a 23 20 20 20 64 65 66 69 6e 65 20 53 5f  */.#   define S_
1990: 49 58 4f 54 48 20 20 30 20 20 20 20 20 20 20 20  IXOTH  0        
19a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19b0: 20 20 20 2f 2a 20 45 78 65 63 75 74 65 20 6f 74     /* Execute ot
19c0: 68 65 72 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a  hers */.#endif..
19d0: 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74  /* Maximum lengt
19e0: 68 20 6f 66 20 66 69 6c 65 20 6e 61 6d 65 20 2a  h of file name *
19f0: 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 50  /.#if !defined(P
1a00: 41 54 48 5f 4d 41 58 29 0a 23 20 20 20 64 65 66  ATH_MAX).#   def
1a10: 69 6e 65 20 50 41 54 48 5f 4d 41 58 20 4d 41 58  ine PATH_MAX MAX
1a20: 5f 50 41 54 48 0a 23 65 6e 64 69 66 0a 23 69 66  _PATH.#endif.#if
1a30: 20 21 64 65 66 69 6e 65 64 28 46 49 4c 45 4e 41   !defined(FILENA
1a40: 4d 45 5f 4d 41 58 29 0a 23 20 20 20 64 65 66 69  ME_MAX).#   defi
1a50: 6e 65 20 46 49 4c 45 4e 41 4d 45 5f 4d 41 58 20  ne FILENAME_MAX 
1a60: 4d 41 58 5f 50 41 54 48 0a 23 65 6e 64 69 66 0a  MAX_PATH.#endif.
1a70: 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 41 4d  #if !defined(NAM
1a80: 45 5f 4d 41 58 29 0a 23 20 20 20 64 65 66 69 6e  E_MAX).#   defin
1a90: 65 20 4e 41 4d 45 5f 4d 41 58 20 46 49 4c 45 4e  e NAME_MAX FILEN
1aa0: 41 4d 45 5f 4d 41 58 0a 23 65 6e 64 69 66 0a 0a  AME_MAX.#endif..
1ab0: 2f 2a 20 46 69 6c 65 20 74 79 70 65 20 66 6c 61  /* File type fla
1ac0: 67 73 20 66 6f 72 20 64 5f 74 79 70 65 20 2a 2f  gs for d_type */
1ad0: 0a 23 64 65 66 69 6e 65 20 44 54 5f 55 4e 4b 4e  .#define DT_UNKN
1ae0: 4f 57 4e 20 20 30 0a 23 64 65 66 69 6e 65 20 44  OWN  0.#define D
1af0: 54 5f 52 45 47 20 20 20 20 20 20 53 5f 49 46 52  T_REG      S_IFR
1b00: 45 47 0a 23 64 65 66 69 6e 65 20 44 54 5f 44 49  EG.#define DT_DI
1b10: 52 20 20 20 20 20 20 53 5f 49 46 44 49 52 0a 23  R      S_IFDIR.#
1b20: 64 65 66 69 6e 65 20 44 54 5f 46 49 46 4f 20 20  define DT_FIFO  
1b30: 20 20 20 53 5f 49 46 49 46 4f 0a 23 64 65 66 69     S_IFIFO.#defi
1b40: 6e 65 20 44 54 5f 53 4f 43 4b 20 20 20 20 20 53  ne DT_SOCK     S
1b50: 5f 49 46 53 4f 43 4b 0a 23 64 65 66 69 6e 65 20  _IFSOCK.#define 
1b60: 44 54 5f 43 48 52 20 20 20 20 20 20 53 5f 49 46  DT_CHR      S_IF
1b70: 43 48 52 0a 23 64 65 66 69 6e 65 20 44 54 5f 42  CHR.#define DT_B
1b80: 4c 4b 20 20 20 20 20 20 53 5f 49 46 42 4c 4b 0a  LK      S_IFBLK.
1b90: 0a 2f 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 63  ./* Macros for c
1ba0: 6f 6e 76 65 72 74 69 6e 67 20 62 65 74 77 65 65  onverting betwee
1bb0: 6e 20 73 74 5f 6d 6f 64 65 20 61 6e 64 20 64 5f  n st_mode and d_
1bc0: 74 79 70 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  type */.#define 
1bd0: 49 46 54 4f 44 54 28 6d 6f 64 65 29 20 28 28 6d  IFTODT(mode) ((m
1be0: 6f 64 65 29 20 26 20 53 5f 49 46 4d 54 29 0a 23  ode) & S_IFMT).#
1bf0: 64 65 66 69 6e 65 20 44 54 54 4f 49 46 28 74 79  define DTTOIF(ty
1c00: 70 65 29 20 28 74 79 70 65 29 0a 0a 2f 2a 0a 20  pe) (type)../*. 
1c10: 2a 20 46 69 6c 65 20 74 79 70 65 20 6d 61 63 72  * File type macr
1c20: 6f 73 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 62  os.  Note that b
1c30: 6c 6f 63 6b 20 64 65 76 69 63 65 73 2c 20 73 6f  lock devices, so
1c40: 63 6b 65 74 73 20 61 6e 64 20 6c 69 6e 6b 73 20  ckets and links 
1c50: 63 61 6e 6e 6f 74 20 62 65 0a 20 2a 20 64 69 73  cannot be. * dis
1c60: 74 69 6e 67 75 69 73 68 65 64 20 6f 6e 20 57 69  tinguished on Wi
1c70: 6e 64 6f 77 73 20 61 6e 64 20 74 68 65 20 6d 61  ndows and the ma
1c80: 63 72 6f 73 20 53 5f 49 53 42 4c 4b 2c 20 53 5f  cros S_ISBLK, S_
1c90: 49 53 53 4f 43 4b 20 61 6e 64 20 53 5f 49 53 4c  ISSOCK and S_ISL
1ca0: 4e 4b 20 61 72 65 0a 20 2a 20 6f 6e 6c 79 20 64  NK are. * only d
1cb0: 65 66 69 6e 65 64 20 66 6f 72 20 63 6f 6d 70 61  efined for compa
1cc0: 74 69 62 69 6c 69 74 79 2e 20 20 54 68 65 73 65  tibility.  These
1cd0: 20 6d 61 63 72 6f 73 20 73 68 6f 75 6c 64 20 61   macros should a
1ce0: 6c 77 61 79 73 20 72 65 74 75 72 6e 20 66 61 6c  lways return fal
1cf0: 73 65 0a 20 2a 20 6f 6e 20 57 69 6e 64 6f 77 73  se. * on Windows
1d00: 2e 0a 20 2a 2f 0a 23 64 65 66 69 6e 65 09 53 5f  .. */.#define.S_
1d10: 49 53 46 49 46 4f 28 6d 6f 64 65 29 20 28 28 28  ISFIFO(mode) (((
1d20: 6d 6f 64 65 29 20 26 20 53 5f 49 46 4d 54 29 20  mode) & S_IFMT) 
1d30: 3d 3d 20 53 5f 49 46 49 46 4f 29 0a 23 64 65 66  == S_IFIFO).#def
1d40: 69 6e 65 09 53 5f 49 53 44 49 52 28 6d 6f 64 65  ine.S_ISDIR(mode
1d50: 29 20 20 28 28 28 6d 6f 64 65 29 20 26 20 53 5f  )  (((mode) & S_
1d60: 49 46 4d 54 29 20 3d 3d 20 53 5f 49 46 44 49 52  IFMT) == S_IFDIR
1d70: 29 0a 23 64 65 66 69 6e 65 09 53 5f 49 53 52 45  ).#define.S_ISRE
1d80: 47 28 6d 6f 64 65 29 20 20 28 28 28 6d 6f 64 65  G(mode)  (((mode
1d90: 29 20 26 20 53 5f 49 46 4d 54 29 20 3d 3d 20 53  ) & S_IFMT) == S
1da0: 5f 49 46 52 45 47 29 0a 23 64 65 66 69 6e 65 09  _IFREG).#define.
1db0: 53 5f 49 53 4c 4e 4b 28 6d 6f 64 65 29 20 20 28  S_ISLNK(mode)  (
1dc0: 28 28 6d 6f 64 65 29 20 26 20 53 5f 49 46 4d 54  ((mode) & S_IFMT
1dd0: 29 20 3d 3d 20 53 5f 49 46 4c 4e 4b 29 0a 23 64  ) == S_IFLNK).#d
1de0: 65 66 69 6e 65 09 53 5f 49 53 53 4f 43 4b 28 6d  efine.S_ISSOCK(m
1df0: 6f 64 65 29 20 28 28 28 6d 6f 64 65 29 20 26 20  ode) (((mode) & 
1e00: 53 5f 49 46 4d 54 29 20 3d 3d 20 53 5f 49 46 53  S_IFMT) == S_IFS
1e10: 4f 43 4b 29 0a 23 64 65 66 69 6e 65 09 53 5f 49  OCK).#define.S_I
1e20: 53 43 48 52 28 6d 6f 64 65 29 20 20 28 28 28 6d  SCHR(mode)  (((m
1e30: 6f 64 65 29 20 26 20 53 5f 49 46 4d 54 29 20 3d  ode) & S_IFMT) =
1e40: 3d 20 53 5f 49 46 43 48 52 29 0a 23 64 65 66 69  = S_IFCHR).#defi
1e50: 6e 65 09 53 5f 49 53 42 4c 4b 28 6d 6f 64 65 29  ne.S_ISBLK(mode)
1e60: 20 20 28 28 28 6d 6f 64 65 29 20 26 20 53 5f 49    (((mode) & S_I
1e70: 46 4d 54 29 20 3d 3d 20 53 5f 49 46 42 4c 4b 29  FMT) == S_IFBLK)
1e80: 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20  ../* Return the 
1e90: 65 78 61 63 74 20 6c 65 6e 67 74 68 20 6f 66 20  exact length of 
1ea0: 64 5f 6e 61 6d 6c 65 6e 20 77 69 74 68 6f 75 74  d_namlen without
1eb0: 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72   zero terminator
1ec0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 5f 44 5f 45   */.#define _D_E
1ed0: 58 41 43 54 5f 4e 41 4d 4c 45 4e 28 70 29 20 28  XACT_NAMLEN(p) (
1ee0: 28 70 29 2d 3e 64 5f 6e 61 6d 6c 65 6e 29 0a 0a  (p)->d_namlen)..
1ef0: 2f 2a 20 52 65 74 75 72 6e 20 6e 75 6d 62 65 72  /* Return number
1f00: 20 6f 66 20 62 79 74 65 73 20 6e 65 65 64 65 64   of bytes needed
1f10: 20 74 6f 20 73 74 6f 72 65 20 64 5f 6e 61 6d 6c   to store d_naml
1f20: 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 5f 44  en */.#define _D
1f30: 5f 41 4c 4c 4f 43 5f 4e 41 4d 4c 45 4e 28 70 29  _ALLOC_NAMLEN(p)
1f40: 20 28 50 41 54 48 5f 4d 41 58 20 2b 20 31 29 0a   (PATH_MAX + 1).
1f50: 0a 0a 23 69 66 64 65 66 20 5f 5f 63 70 6c 75 73  ..#ifdef __cplus
1f60: 70 6c 75 73 0a 65 78 74 65 72 6e 20 22 43 22 20  plus.extern "C" 
1f70: 7b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 20 57 69  {.#endif.../* Wi
1f80: 64 65 2d 63 68 61 72 61 63 74 65 72 20 76 65 72  de-character ver
1f90: 73 69 6f 6e 20 2a 2f 0a 73 74 72 75 63 74 20 5f  sion */.struct _
1fa0: 77 64 69 72 65 6e 74 20 7b 0a 20 20 20 20 6c 6f  wdirent {.    lo
1fb0: 6e 67 20 64 5f 69 6e 6f 3b 20 20 20 20 20 20 20  ng d_ino;       
1fc0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fd0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 77            /* Alw
1fe0: 61 79 73 20 7a 65 72 6f 20 2a 2f 0a 20 20 20 20  ays zero */.    
1ff0: 75 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20 64  unsigned short d
2000: 5f 72 65 63 6c 65 6e 3b 20 20 20 20 20 20 20 20  _reclen;        
2010: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
2020: 74 72 75 63 74 75 72 65 20 73 69 7a 65 20 2a 2f  tructure size */
2030: 0a 20 20 20 20 73 69 7a 65 5f 74 20 64 5f 6e 61  .    size_t d_na
2040: 6d 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20  mlen;           
2050: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2060: 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 6e 61   /* Length of na
2070: 6d 65 20 77 69 74 68 6f 75 74 20 5c 30 20 2a 2f  me without \0 */
2080: 0a 20 20 20 20 69 6e 74 20 64 5f 74 79 70 65 3b  .    int d_type;
2090: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20b0: 20 2f 2a 20 46 69 6c 65 20 74 79 70 65 20 2a 2f   /* File type */
20c0: 0a 20 20 20 20 77 63 68 61 72 5f 74 20 64 5f 6e  .    wchar_t d_n
20d0: 61 6d 65 5b 50 41 54 48 5f 4d 41 58 20 2b 20 31  ame[PATH_MAX + 1
20e0: 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ];              
20f0: 20 2f 2a 20 46 69 6c 65 20 6e 61 6d 65 20 2a 2f   /* File name */
2100: 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  .};.typedef stru
2110: 63 74 20 5f 77 64 69 72 65 6e 74 20 5f 77 64 69  ct _wdirent _wdi
2120: 72 65 6e 74 3b 0a 0a 73 74 72 75 63 74 20 5f 57  rent;..struct _W
2130: 44 49 52 20 7b 0a 20 20 20 20 73 74 72 75 63 74  DIR {.    struct
2140: 20 5f 77 64 69 72 65 6e 74 20 65 6e 74 3b 20 20   _wdirent ent;  
2150: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2160: 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
2170: 20 64 69 72 65 63 74 6f 72 79 20 65 6e 74 72 79   directory entry
2180: 20 2a 2f 0a 20 20 20 20 57 49 4e 33 32 5f 46 49   */.    WIN32_FI
2190: 4e 44 5f 44 41 54 41 57 20 64 61 74 61 3b 20 20  ND_DATAW data;  
21a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21b0: 20 20 20 20 2f 2a 20 50 72 69 76 61 74 65 20 66      /* Private f
21c0: 69 6c 65 20 64 61 74 61 20 2a 2f 0a 20 20 20 20  ile data */.    
21d0: 69 6e 74 20 63 61 63 68 65 64 3b 20 20 20 20 20  int cached;     
21e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21f0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2200: 72 75 65 20 69 66 20 64 61 74 61 20 69 73 20 76  rue if data is v
2210: 61 6c 69 64 20 2a 2f 0a 20 20 20 20 48 41 4e 44  alid */.    HAND
2220: 4c 45 20 68 61 6e 64 6c 65 3b 20 20 20 20 20 20  LE handle;      
2230: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2240: 20 20 20 20 20 20 20 20 2f 2a 20 57 69 6e 33 32          /* Win32
2250: 20 73 65 61 72 63 68 20 68 61 6e 64 6c 65 20 2a   search handle *
2260: 2f 0a 20 20 20 20 77 63 68 61 72 5f 74 20 2a 70  /.    wchar_t *p
2270: 61 74 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  att;            
2280: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2290: 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 64 69 72    /* Initial dir
22a0: 65 63 74 6f 72 79 20 6e 61 6d 65 20 2a 2f 0a 7d  ectory name */.}
22b0: 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
22c0: 20 5f 57 44 49 52 20 5f 57 44 49 52 3b 0a 0a 73   _WDIR _WDIR;..s
22d0: 74 61 74 69 63 20 5f 57 44 49 52 20 2a 5f 77 6f  tatic _WDIR *_wo
22e0: 70 65 6e 64 69 72 20 28 63 6f 6e 73 74 20 77 63  pendir (const wc
22f0: 68 61 72 5f 74 20 2a 64 69 72 6e 61 6d 65 29 3b  har_t *dirname);
2300: 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 5f  .static struct _
2310: 77 64 69 72 65 6e 74 20 2a 5f 77 72 65 61 64 64  wdirent *_wreadd
2320: 69 72 20 28 5f 57 44 49 52 20 2a 64 69 72 70 29  ir (_WDIR *dirp)
2330: 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 5f 77 63  ;.static int _wc
2340: 6c 6f 73 65 64 69 72 20 28 5f 57 44 49 52 20 2a  losedir (_WDIR *
2350: 64 69 72 70 29 3b 0a 73 74 61 74 69 63 20 76 6f  dirp);.static vo
2360: 69 64 20 5f 77 72 65 77 69 6e 64 64 69 72 20 28  id _wrewinddir (
2370: 5f 57 44 49 52 2a 20 64 69 72 70 29 3b 0a 0a 0a  _WDIR* dirp);...
2380: 2f 2a 20 46 6f 72 20 63 6f 6d 70 61 74 69 62 69  /* For compatibi
2390: 6c 69 74 79 20 77 69 74 68 20 53 79 6d 62 69 61  lity with Symbia
23a0: 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 77 64 69  n */.#define wdi
23b0: 72 65 6e 74 20 5f 77 64 69 72 65 6e 74 0a 23 64  rent _wdirent.#d
23c0: 65 66 69 6e 65 20 57 44 49 52 20 5f 57 44 49 52  efine WDIR _WDIR
23d0: 0a 23 64 65 66 69 6e 65 20 77 6f 70 65 6e 64 69  .#define wopendi
23e0: 72 20 5f 77 6f 70 65 6e 64 69 72 0a 23 64 65 66  r _wopendir.#def
23f0: 69 6e 65 20 77 72 65 61 64 64 69 72 20 5f 77 72  ine wreaddir _wr
2400: 65 61 64 64 69 72 0a 23 64 65 66 69 6e 65 20 77  eaddir.#define w
2410: 63 6c 6f 73 65 64 69 72 20 5f 77 63 6c 6f 73 65  closedir _wclose
2420: 64 69 72 0a 23 64 65 66 69 6e 65 20 77 72 65 77  dir.#define wrew
2430: 69 6e 64 64 69 72 20 5f 77 72 65 77 69 6e 64 64  inddir _wrewindd
2440: 69 72 0a 0a 0a 2f 2a 20 4d 75 6c 74 69 2d 62 79  ir.../* Multi-by
2450: 74 65 20 63 68 61 72 61 63 74 65 72 20 76 65 72  te character ver
2460: 73 69 6f 6e 73 20 2a 2f 0a 73 74 72 75 63 74 20  sions */.struct 
2470: 64 69 72 65 6e 74 20 7b 0a 20 20 20 20 6c 6f 6e  dirent {.    lon
2480: 67 20 64 5f 69 6e 6f 3b 20 20 20 20 20 20 20 20  g d_ino;        
2490: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24a0: 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 77 61           /* Alwa
24b0: 79 73 20 7a 65 72 6f 20 2a 2f 0a 20 20 20 20 75  ys zero */.    u
24c0: 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20 64 5f  nsigned short d_
24d0: 72 65 63 6c 65 6e 3b 20 20 20 20 20 20 20 20 20  reclen;         
24e0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74             /* St
24f0: 72 75 63 74 75 72 65 20 73 69 7a 65 20 2a 2f 0a  ructure size */.
2500: 20 20 20 20 73 69 7a 65 5f 74 20 64 5f 6e 61 6d      size_t d_nam
2510: 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  len;            
2520: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2530: 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 6e 61 6d  /* Length of nam
2540: 65 20 77 69 74 68 6f 75 74 20 5c 30 20 2a 2f 0a  e without \0 */.
2550: 20 20 20 20 69 6e 74 20 64 5f 74 79 70 65 3b 20      int d_type; 
2560: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2570: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2580: 2f 2a 20 46 69 6c 65 20 74 79 70 65 20 2a 2f 0a  /* File type */.
2590: 20 20 20 20 63 68 61 72 20 64 5f 6e 61 6d 65 5b      char d_name[
25a0: 50 41 54 48 5f 4d 41 58 20 2b 20 31 5d 3b 20 20  PATH_MAX + 1];  
25b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25c0: 2f 2a 20 46 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a  /* File name */.
25d0: 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  };.typedef struc
25e0: 74 20 64 69 72 65 6e 74 20 64 69 72 65 6e 74 3b  t dirent dirent;
25f0: 0a 0a 73 74 72 75 63 74 20 44 49 52 20 7b 0a 20  ..struct DIR {. 
2600: 20 20 20 73 74 72 75 63 74 20 64 69 72 65 6e 74     struct dirent
2610: 20 65 6e 74 3b 0a 20 20 20 20 73 74 72 75 63 74   ent;.    struct
2620: 20 5f 57 44 49 52 20 2a 77 64 69 72 70 3b 0a 7d   _WDIR *wdirp;.}
2630: 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
2640: 20 44 49 52 20 44 49 52 3b 0a 0a 73 74 61 74 69   DIR DIR;..stati
2650: 63 20 44 49 52 20 2a 6f 70 65 6e 64 69 72 20 28  c DIR *opendir (
2660: 63 6f 6e 73 74 20 63 68 61 72 20 2a 64 69 72 6e  const char *dirn
2670: 61 6d 65 29 3b 0a 73 74 61 74 69 63 20 73 74 72  ame);.static str
2680: 75 63 74 20 64 69 72 65 6e 74 20 2a 72 65 61 64  uct dirent *read
2690: 64 69 72 20 28 44 49 52 20 2a 64 69 72 70 29 3b  dir (DIR *dirp);
26a0: 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6c 6f 73  .static int clos
26b0: 65 64 69 72 20 28 44 49 52 20 2a 64 69 72 70 29  edir (DIR *dirp)
26c0: 3b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  ;.static void re
26d0: 77 69 6e 64 64 69 72 20 28 44 49 52 2a 20 64 69  winddir (DIR* di
26e0: 72 70 29 3b 0a 0a 0a 2f 2a 20 49 6e 74 65 72 6e  rp);.../* Intern
26f0: 61 6c 20 75 74 69 6c 69 74 79 20 66 75 6e 63 74  al utility funct
2700: 69 6f 6e 73 20 2a 2f 0a 73 74 61 74 69 63 20 57  ions */.static W
2710: 49 4e 33 32 5f 46 49 4e 44 5f 44 41 54 41 57 20  IN32_FIND_DATAW 
2720: 2a 64 69 72 65 6e 74 5f 66 69 72 73 74 20 28 5f  *dirent_first (_
2730: 57 44 49 52 20 2a 64 69 72 70 29 3b 0a 73 74 61  WDIR *dirp);.sta
2740: 74 69 63 20 57 49 4e 33 32 5f 46 49 4e 44 5f 44  tic WIN32_FIND_D
2750: 41 54 41 57 20 2a 64 69 72 65 6e 74 5f 6e 65 78  ATAW *dirent_nex
2760: 74 20 28 5f 57 44 49 52 20 2a 64 69 72 70 29 3b  t (_WDIR *dirp);
2770: 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 64 69 72  ..static int dir
2780: 65 6e 74 5f 6d 62 73 74 6f 77 63 73 5f 73 28 0a  ent_mbstowcs_s(.
2790: 20 20 20 20 73 69 7a 65 5f 74 20 2a 70 52 65 74      size_t *pRet
27a0: 75 72 6e 56 61 6c 75 65 2c 0a 20 20 20 20 77 63  urnValue,.    wc
27b0: 68 61 72 5f 74 20 2a 77 63 73 74 72 2c 0a 20 20  har_t *wcstr,.  
27c0: 20 20 73 69 7a 65 5f 74 20 73 69 7a 65 49 6e 57    size_t sizeInW
27d0: 6f 72 64 73 2c 0a 20 20 20 20 63 6f 6e 73 74 20  ords,.    const 
27e0: 63 68 61 72 20 2a 6d 62 73 74 72 2c 0a 20 20 20  char *mbstr,.   
27f0: 20 73 69 7a 65 5f 74 20 63 6f 75 6e 74 29 3b 0a   size_t count);.
2800: 0a 73 74 61 74 69 63 20 69 6e 74 20 64 69 72 65  .static int dire
2810: 6e 74 5f 77 63 73 74 6f 6d 62 73 5f 73 28 0a 20  nt_wcstombs_s(. 
2820: 20 20 20 73 69 7a 65 5f 74 20 2a 70 52 65 74 75     size_t *pRetu
2830: 72 6e 56 61 6c 75 65 2c 0a 20 20 20 20 63 68 61  rnValue,.    cha
2840: 72 20 2a 6d 62 73 74 72 2c 0a 20 20 20 20 73 69  r *mbstr,.    si
2850: 7a 65 5f 74 20 73 69 7a 65 49 6e 42 79 74 65 73  ze_t sizeInBytes
2860: 2c 0a 20 20 20 20 63 6f 6e 73 74 20 77 63 68 61  ,.    const wcha
2870: 72 5f 74 20 2a 77 63 73 74 72 2c 0a 20 20 20 20  r_t *wcstr,.    
2880: 73 69 7a 65 5f 74 20 63 6f 75 6e 74 29 3b 0a 0a  size_t count);..
2890: 73 74 61 74 69 63 20 76 6f 69 64 20 64 69 72 65  static void dire
28a0: 6e 74 5f 73 65 74 5f 65 72 72 6e 6f 20 28 69 6e  nt_set_errno (in
28b0: 74 20 65 72 72 6f 72 29 3b 0a 0a 2f 2a 0a 20 2a  t error);../*. *
28c0: 20 4f 70 65 6e 20 64 69 72 65 63 74 6f 72 79 20   Open directory 
28d0: 73 74 72 65 61 6d 20 44 49 52 4e 41 4d 45 20 66  stream DIRNAME f
28e0: 6f 72 20 72 65 61 64 20 61 6e 64 20 72 65 74 75  or read and retu
28f0: 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
2900: 74 68 65 0a 20 2a 20 69 6e 74 65 72 6e 61 6c 20  the. * internal 
2910: 77 6f 72 6b 69 6e 67 20 61 72 65 61 20 74 68 61  working area tha
2920: 74 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 74  t is used to ret
2930: 72 69 65 76 65 20 69 6e 64 69 76 69 64 75 61 6c  rieve individual
2940: 20 64 69 72 65 63 74 6f 72 79 0a 20 2a 20 65 6e   directory. * en
2950: 74 72 69 65 73 2e 0a 20 2a 2f 0a 73 74 61 74 69  tries.. */.stati
2960: 63 20 5f 57 44 49 52 2a 0a 5f 77 6f 70 65 6e 64  c _WDIR*._wopend
2970: 69 72 28 0a 20 20 20 20 63 6f 6e 73 74 20 77 63  ir(.    const wc
2980: 68 61 72 5f 74 20 2a 64 69 72 6e 61 6d 65 29 0a  har_t *dirname).
2990: 7b 0a 20 20 20 20 5f 57 44 49 52 20 2a 64 69 72  {.    _WDIR *dir
29a0: 70 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e  p = NULL;.    in
29b0: 74 20 65 72 72 6f 72 3b 0a 0a 20 20 20 20 2f 2a  t error;..    /*
29c0: 20 4d 75 73 74 20 68 61 76 65 20 64 69 72 65 63   Must have direc
29d0: 74 6f 72 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 20  tory name */.   
29e0: 20 69 66 20 28 64 69 72 6e 61 6d 65 20 3d 3d 20   if (dirname == 
29f0: 4e 55 4c 4c 20 20 7c 7c 20 20 64 69 72 6e 61 6d  NULL  ||  dirnam
2a00: 65 5b 30 5d 20 3d 3d 20 27 5c 30 27 29 20 7b 0a  e[0] == '\0') {.
2a10: 20 20 20 20 20 20 20 20 64 69 72 65 6e 74 5f 73          dirent_s
2a20: 65 74 5f 65 72 72 6e 6f 20 28 45 4e 4f 45 4e 54  et_errno (ENOENT
2a30: 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
2a40: 6e 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a 0a 20  n NULL;.    }.. 
2a50: 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6e     /* Allocate n
2a60: 65 77 20 5f 57 44 49 52 20 73 74 72 75 63 74 75  ew _WDIR structu
2a70: 72 65 20 2a 2f 0a 20 20 20 20 64 69 72 70 20 3d  re */.    dirp =
2a80: 20 28 5f 57 44 49 52 2a 29 20 6d 61 6c 6c 6f 63   (_WDIR*) malloc
2a90: 20 28 73 69 7a 65 6f 66 20 28 73 74 72 75 63 74   (sizeof (struct
2aa0: 20 5f 57 44 49 52 29 29 3b 0a 20 20 20 20 69 66   _WDIR));.    if
2ab0: 20 28 64 69 72 70 20 21 3d 20 4e 55 4c 4c 29 20   (dirp != NULL) 
2ac0: 7b 0a 20 20 20 20 20 20 20 20 44 57 4f 52 44 20  {.        DWORD 
2ad0: 6e 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 52  n;..        /* R
2ae0: 65 73 65 74 20 5f 57 44 49 52 20 73 74 72 75 63  eset _WDIR struc
2af0: 74 75 72 65 20 2a 2f 0a 20 20 20 20 20 20 20 20  ture */.        
2b00: 64 69 72 70 2d 3e 68 61 6e 64 6c 65 20 3d 20 49  dirp->handle = I
2b10: 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41  NVALID_HANDLE_VA
2b20: 4c 55 45 3b 0a 20 20 20 20 20 20 20 20 64 69 72  LUE;.        dir
2b30: 70 2d 3e 70 61 74 74 20 3d 20 4e 55 4c 4c 3b 0a  p->patt = NULL;.
2b40: 20 20 20 20 20 20 20 20 64 69 72 70 2d 3e 63 61          dirp->ca
2b50: 63 68 65 64 20 3d 20 30 3b 0a 0a 20 20 20 20 20  ched = 0;..     
2b60: 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68     /* Compute th
2b70: 65 20 6c 65 6e 67 74 68 20 6f 66 20 66 75 6c 6c  e length of full
2b80: 20 70 61 74 68 20 70 6c 75 73 20 7a 65 72 6f 20   path plus zero 
2b90: 74 65 72 6d 69 6e 61 74 6f 72 20 2a 2f 0a 20 20  terminator */.  
2ba0: 20 20 20 20 20 20 6e 20 3d 20 47 65 74 46 75 6c        n = GetFul
2bb0: 6c 50 61 74 68 4e 61 6d 65 57 20 28 64 69 72 6e  lPathNameW (dirn
2bc0: 61 6d 65 2c 20 30 2c 20 4e 55 4c 4c 2c 20 4e 55  ame, 0, NULL, NU
2bd0: 4c 4c 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a  LL);..        /*
2be0: 20 41 6c 6c 6f 63 61 74 65 20 72 6f 6f 6d 20 66   Allocate room f
2bf0: 6f 72 20 61 62 73 6f 6c 75 74 65 20 64 69 72 65  or absolute dire
2c00: 63 74 6f 72 79 20 6e 61 6d 65 20 61 6e 64 20 73  ctory name and s
2c10: 65 61 72 63 68 20 70 61 74 74 65 72 6e 20 2a 2f  earch pattern */
2c20: 0a 20 20 20 20 20 20 20 20 64 69 72 70 2d 3e 70  .        dirp->p
2c30: 61 74 74 20 3d 20 28 77 63 68 61 72 5f 74 2a 29  att = (wchar_t*)
2c40: 20 6d 61 6c 6c 6f 63 20 28 73 69 7a 65 6f 66 20   malloc (sizeof 
2c50: 28 77 63 68 61 72 5f 74 29 20 2a 20 6e 20 2b 20  (wchar_t) * n + 
2c60: 31 36 29 3b 0a 20 20 20 20 20 20 20 20 69 66 20  16);.        if 
2c70: 28 64 69 72 70 2d 3e 70 61 74 74 29 20 7b 0a 0a  (dirp->patt) {..
2c80: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 0a 20              /*. 
2c90: 20 20 20 20 20 20 20 20 20 20 20 20 2a 20 43 6f              * Co
2ca0: 6e 76 65 72 74 20 72 65 6c 61 74 69 76 65 20 64  nvert relative d
2cb0: 69 72 65 63 74 6f 72 79 20 6e 61 6d 65 20 74 6f  irectory name to
2cc0: 20 61 6e 20 61 62 73 6f 6c 75 74 65 20 6f 6e 65   an absolute one
2cd0: 2e 20 20 54 68 69 73 0a 20 20 20 20 20 20 20 20  .  This.        
2ce0: 20 20 20 20 20 2a 20 61 6c 6c 6f 77 73 20 72 65       * allows re
2cf0: 77 69 6e 64 64 69 72 28 29 20 74 6f 20 66 75 6e  winddir() to fun
2d00: 63 74 69 6f 6e 20 63 6f 72 72 65 63 74 6c 79 20  ction correctly 
2d10: 65 76 65 6e 20 77 68 65 6e 20 63 75 72 72 65 6e  even when curren
2d20: 74 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 2a  t.             *
2d30: 20 77 6f 72 6b 69 6e 67 20 64 69 72 65 63 74 6f   working directo
2d40: 72 79 20 69 73 20 63 68 61 6e 67 65 64 20 62 65  ry is changed be
2d50: 74 77 65 65 6e 20 6f 70 65 6e 64 69 72 28 29 20  tween opendir() 
2d60: 61 6e 64 20 72 65 77 69 6e 64 64 69 72 28 29 2e  and rewinddir().
2d70: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f  .             */
2d80: 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 20 3d  .            n =
2d90: 20 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65   GetFullPathName
2da0: 57 20 28 64 69 72 6e 61 6d 65 2c 20 6e 2c 20 64  W (dirname, n, d
2db0: 69 72 70 2d 3e 70 61 74 74 2c 20 4e 55 4c 4c 29  irp->patt, NULL)
2dc0: 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  ;.            if
2dd0: 20 28 6e 20 3e 20 30 29 20 7b 0a 20 20 20 20 20   (n > 0) {.     
2de0: 20 20 20 20 20 20 20 20 20 20 20 77 63 68 61 72             wchar
2df0: 5f 74 20 2a 70 3b 0a 0a 20 20 20 20 20 20 20 20  _t *p;..        
2e00: 20 20 20 20 20 20 20 20 2f 2a 20 41 70 70 65 6e          /* Appen
2e10: 64 20 73 65 61 72 63 68 20 70 61 74 74 65 72 6e  d search pattern
2e20: 20 5c 2a 20 74 6f 20 74 68 65 20 64 69 72 65 63   \* to the direc
2e30: 74 6f 72 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 20  tory name */.   
2e40: 20 20 20 20 20 20 20 20 20 20 20 20 20 70 20 3d               p =
2e50: 20 64 69 72 70 2d 3e 70 61 74 74 20 2b 20 6e 3b   dirp->patt + n;
2e60: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2e70: 20 69 66 20 28 64 69 72 70 2d 3e 70 61 74 74 20   if (dirp->patt 
2e80: 3c 20 70 29 20 7b 0a 20 20 20 20 20 20 20 20 20  < p) {.         
2e90: 20 20 20 20 20 20 20 20 20 20 20 73 77 69 74 63             switc
2ea0: 68 20 28 70 5b 2d 31 5d 29 20 7b 0a 20 20 20 20  h (p[-1]) {.    
2eb0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec0: 63 61 73 65 20 27 5c 5c 27 3a 0a 20 20 20 20 20  case '\\':.     
2ed0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
2ee0: 61 73 65 20 27 2f 27 3a 0a 20 20 20 20 20 20 20  ase '/':.       
2ef0: 20 20 20 20 20 20 20 20 20 20 20 20 20 63 61 73               cas
2f00: 65 20 27 3a 27 3a 0a 20 20 20 20 20 20 20 20 20  e ':':.         
2f10: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2f20: 2a 20 44 69 72 65 63 74 6f 72 79 20 65 6e 64 73  * Directory ends
2f30: 20 69 6e 20 70 61 74 68 20 73 65 70 61 72 61 74   in path separat
2f40: 6f 72 2c 20 65 2e 67 2e 20 63 3a 5c 74 65 6d 70  or, e.g. c:\temp
2f50: 5c 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  \ */.           
2f60: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e               /*N
2f70: 4f 50 2a 2f 3b 0a 20 20 20 20 20 20 20 20 20 20  OP*/;.          
2f80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72                br
2f90: 65 61 6b 3b 0a 0a 20 20 20 20 20 20 20 20 20 20  eak;..          
2fa0: 20 20 20 20 20 20 20 20 20 20 64 65 66 61 75 6c            defaul
2fb0: 74 3a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t:.             
2fc0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69             /* Di
2fd0: 72 65 63 74 6f 72 79 20 6e 61 6d 65 20 64 6f 65  rectory name doe
2fe0: 73 6e 27 74 20 65 6e 64 20 69 6e 20 70 61 74 68  sn't end in path
2ff0: 20 73 65 70 61 72 61 74 6f 72 20 2a 2f 0a 20 20   separator */.  
3000: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3010: 20 20 20 20 20 20 2a 70 2b 2b 20 3d 20 27 5c 5c        *p++ = '\\
3020: 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ';.             
3030: 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
3040: 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
3050: 20 20 20 20 20 20 20 20 20 20 20 2a 70 2b 2b 20             *p++ 
3060: 3d 20 27 2a 27 3b 0a 20 20 20 20 20 20 20 20 20  = '*';.         
3070: 20 20 20 20 20 20 20 2a 70 20 3d 20 27 5c 30 27         *p = '\0'
3080: 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ;..             
3090: 20 20 20 2f 2a 20 4f 70 65 6e 20 64 69 72 65 63     /* Open direc
30a0: 74 6f 72 79 20 73 74 72 65 61 6d 20 61 6e 64 20  tory stream and 
30b0: 72 65 74 72 69 65 76 65 20 74 68 65 20 66 69 72  retrieve the fir
30c0: 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 20 20  st entry */.    
30d0: 20 20 20 20 20 20 20 20 20 20 20 20 69 66 20 28              if (
30e0: 64 69 72 65 6e 74 5f 66 69 72 73 74 20 28 64 69  dirent_first (di
30f0: 72 70 29 29 20 7b 0a 20 20 20 20 20 20 20 20 20  rp)) {.         
3100: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69             /* Di
3110: 72 65 63 74 6f 72 79 20 73 74 72 65 61 6d 20 6f  rectory stream o
3120: 70 65 6e 65 64 20 73 75 63 63 65 73 73 66 75 6c  pened successful
3130: 6c 79 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  ly */.          
3140: 20 20 20 20 20 20 20 20 20 20 65 72 72 6f 72 20            error 
3150: 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = 0;.           
3160: 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20       } else {.  
3170: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3180: 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 72 65 74 72    /* Cannot retr
3190: 69 65 76 65 20 66 69 72 73 74 20 65 6e 74 72 79  ieve first entry
31a0: 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
31b0: 20 20 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20          error = 
31c0: 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  1;.             
31d0: 20 20 20 20 20 20 20 64 69 72 65 6e 74 5f 73 65         dirent_se
31e0: 74 5f 65 72 72 6e 6f 20 28 45 4e 4f 45 4e 54 29  t_errno (ENOENT)
31f0: 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
3200: 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 20    }..           
3210: 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20   } else {.      
3220: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 6e            /* Can
3230: 6e 6f 74 20 72 65 74 72 69 65 76 65 20 66 75 6c  not retrieve ful
3240: 6c 20 70 61 74 68 20 6e 61 6d 65 20 2a 2f 0a 20  l path name */. 
3250: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64                 d
3260: 69 72 65 6e 74 5f 73 65 74 5f 65 72 72 6e 6f 20  irent_set_errno 
3270: 28 45 4e 4f 45 4e 54 29 3b 0a 20 20 20 20 20 20  (ENOENT);.      
3280: 20 20 20 20 20 20 20 20 20 20 65 72 72 6f 72 20            error 
3290: 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = 1;.           
32a0: 20 7d 0a 0a 20 20 20 20 20 20 20 20 7d 20 65 6c   }..        } el
32b0: 73 65 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20  se {.           
32c0: 20 2f 2a 20 43 61 6e 6e 6f 74 20 61 6c 6c 6f 63   /* Cannot alloc
32d0: 61 74 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20 73  ate memory for s
32e0: 65 61 72 63 68 20 70 61 74 74 65 72 6e 20 2a 2f  earch pattern */
32f0: 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 72 72  .            err
3300: 6f 72 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  or = 1;.        
3310: 7d 0a 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a  }..    } else {.
3320: 20 20 20 20 20 20 20 20 2f 2a 20 43 61 6e 6e 6f          /* Canno
3330: 74 20 61 6c 6c 6f 63 61 74 65 20 5f 57 44 49 52  t allocate _WDIR
3340: 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20   structure */.  
3350: 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 31 3b        error = 1;
3360: 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43  .    }..    /* C
3370: 6c 65 61 6e 20 75 70 20 69 6e 20 63 61 73 65 20  lean up in case 
3380: 6f 66 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20  of error */.    
3390: 69 66 20 28 65 72 72 6f 72 20 20 26 26 20 20 64  if (error  &&  d
33a0: 69 72 70 29 20 7b 0a 20 20 20 20 20 20 20 20 5f  irp) {.        _
33b0: 77 63 6c 6f 73 65 64 69 72 20 28 64 69 72 70 29  wclosedir (dirp)
33c0: 3b 0a 20 20 20 20 20 20 20 20 64 69 72 70 20 3d  ;.        dirp =
33d0: 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a 0a 20 20   NULL;.    }..  
33e0: 20 20 72 65 74 75 72 6e 20 64 69 72 70 3b 0a 7d    return dirp;.}
33f0: 0a 0a 2f 2a 0a 20 2a 20 52 65 61 64 20 6e 65 78  ../*. * Read nex
3400: 74 20 64 69 72 65 63 74 6f 72 79 20 65 6e 74 72  t directory entr
3410: 79 2e 20 20 54 68 65 20 64 69 72 65 63 74 6f 72  y.  The director
3420: 79 20 65 6e 74 72 79 20 69 73 20 72 65 74 75 72  y entry is retur
3430: 6e 65 64 20 69 6e 20 64 69 72 65 6e 74 0a 20 2a  ned in dirent. *
3440: 20 73 74 72 75 63 74 75 72 65 20 69 6e 20 74 68   structure in th
3450: 65 20 64 5f 6e 61 6d 65 20 66 69 65 6c 64 2e 20  e d_name field. 
3460: 20 49 6e 64 69 76 69 64 75 61 6c 20 64 69 72 65   Individual dire
3470: 63 74 6f 72 79 20 65 6e 74 72 69 65 73 20 72 65  ctory entries re
3480: 74 75 72 6e 65 64 20 62 79 0a 20 2a 20 74 68 69  turned by. * thi
3490: 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 63 6c 75  s function inclu
34a0: 64 65 20 72 65 67 75 6c 61 72 20 66 69 6c 65 73  de regular files
34b0: 2c 20 73 75 62 2d 64 69 72 65 63 74 6f 72 69 65  , sub-directorie
34c0: 73 2c 20 70 73 65 75 64 6f 2d 64 69 72 65 63 74  s, pseudo-direct
34d0: 6f 72 69 65 73 0a 20 2a 20 22 2e 22 20 61 6e 64  ories. * "." and
34e0: 20 22 2e 2e 22 20 61 73 20 77 65 6c 6c 20 61 73   ".." as well as
34f0: 20 76 6f 6c 75 6d 65 20 6c 61 62 65 6c 73 2c 20   volume labels, 
3500: 68 69 64 64 65 6e 20 66 69 6c 65 73 20 61 6e 64  hidden files and
3510: 20 73 79 73 74 65 6d 20 66 69 6c 65 73 2e 0a 20   system files.. 
3520: 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74  */.static struct
3530: 20 5f 77 64 69 72 65 6e 74 2a 0a 5f 77 72 65 61   _wdirent*._wrea
3540: 64 64 69 72 28 0a 20 20 20 20 5f 57 44 49 52 20  ddir(.    _WDIR 
3550: 2a 64 69 72 70 29 0a 7b 0a 20 20 20 20 57 49 4e  *dirp).{.    WIN
3560: 33 32 5f 46 49 4e 44 5f 44 41 54 41 57 20 2a 64  32_FIND_DATAW *d
3570: 61 74 61 70 3b 0a 20 20 20 20 73 74 72 75 63 74  atap;.    struct
3580: 20 5f 77 64 69 72 65 6e 74 20 2a 65 6e 74 70 3b   _wdirent *entp;
3590: 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 6e 65  ..    /* Read ne
35a0: 78 74 20 64 69 72 65 63 74 6f 72 79 20 65 6e 74  xt directory ent
35b0: 72 79 20 2a 2f 0a 20 20 20 20 64 61 74 61 70 20  ry */.    datap 
35c0: 3d 20 64 69 72 65 6e 74 5f 6e 65 78 74 20 28 64  = dirent_next (d
35d0: 69 72 70 29 3b 0a 20 20 20 20 69 66 20 28 64 61  irp);.    if (da
35e0: 74 61 70 29 20 7b 0a 20 20 20 20 20 20 20 20 73  tap) {.        s
35f0: 69 7a 65 5f 74 20 6e 3b 0a 20 20 20 20 20 20 20  ize_t n;.       
3600: 20 44 57 4f 52 44 20 61 74 74 72 3b 0a 20 20 20   DWORD attr;.   
3610: 20 20 20 20 20 0a 20 20 20 20 20 20 20 20 2f 2a       .        /*
3620: 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 69 72 65   Pointer to dire
3630: 63 74 6f 72 79 20 65 6e 74 72 79 20 74 6f 20 72  ctory entry to r
3640: 65 74 75 72 6e 20 2a 2f 0a 20 20 20 20 20 20 20  eturn */.       
3650: 20 65 6e 74 70 20 3d 20 26 64 69 72 70 2d 3e 65   entp = &dirp->e
3660: 6e 74 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20  nt;..        /* 
3670: 0a 20 20 20 20 20 20 20 20 20 2a 20 43 6f 70 79  .         * Copy
3680: 20 66 69 6c 65 20 6e 61 6d 65 20 61 73 20 77 69   file name as wi
3690: 64 65 2d 63 68 61 72 61 63 74 65 72 20 73 74 72  de-character str
36a0: 69 6e 67 2e 20 20 49 66 20 74 68 65 20 66 69 6c  ing.  If the fil
36b0: 65 20 6e 61 6d 65 20 69 73 20 74 6f 6f 0a 20 20  e name is too.  
36c0: 20 20 20 20 20 20 20 2a 20 6c 6f 6e 67 20 74 6f         * long to
36d0: 20 66 69 74 20 69 6e 20 74 6f 20 74 68 65 20 64   fit in to the d
36e0: 65 73 74 69 6e 61 74 69 6f 6e 20 62 75 66 66 65  estination buffe
36f0: 72 2c 20 74 68 65 6e 20 74 72 75 6e 63 61 74 65  r, then truncate
3700: 20 66 69 6c 65 20 6e 61 6d 65 0a 20 20 20 20 20   file name.     
3710: 20 20 20 20 2a 20 74 6f 20 50 41 54 48 5f 4d 41      * to PATH_MA
3720: 58 20 63 68 61 72 61 63 74 65 72 73 20 61 6e 64  X characters and
3730: 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 20   zero-terminate 
3740: 74 68 65 20 62 75 66 66 65 72 2e 0a 20 20 20 20  the buffer..    
3750: 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
3760: 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 77  n = 0;.        w
3770: 68 69 6c 65 20 28 6e 20 3c 20 50 41 54 48 5f 4d  hile (n < PATH_M
3780: 41 58 20 20 26 26 20 20 64 61 74 61 70 2d 3e 63  AX  &&  datap->c
3790: 46 69 6c 65 4e 61 6d 65 5b 6e 5d 20 21 3d 20 30  FileName[n] != 0
37a0: 29 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ) {.            
37b0: 65 6e 74 70 2d 3e 64 5f 6e 61 6d 65 5b 6e 5d 20  entp->d_name[n] 
37c0: 3d 20 64 61 74 61 70 2d 3e 63 46 69 6c 65 4e 61  = datap->cFileNa
37d0: 6d 65 5b 6e 5d 3b 0a 20 20 20 20 20 20 20 20 20  me[n];.         
37e0: 20 20 20 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20     n++;.        
37f0: 7d 0a 20 20 20 20 20 20 20 20 64 69 72 70 2d 3e  }.        dirp->
3800: 65 6e 74 2e 64 5f 6e 61 6d 65 5b 6e 5d 20 3d 20  ent.d_name[n] = 
3810: 30 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 4c  0;..        /* L
3820: 65 6e 67 74 68 20 6f 66 20 66 69 6c 65 20 6e 61  ength of file na
3830: 6d 65 20 65 78 63 6c 75 64 69 6e 67 20 7a 65 72  me excluding zer
3840: 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 2a 2f 0a  o terminator */.
3850: 20 20 20 20 20 20 20 20 65 6e 74 70 2d 3e 64 5f          entp->d_
3860: 6e 61 6d 6c 65 6e 20 3d 20 6e 3b 0a 0a 20 20 20  namlen = n;..   
3870: 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 79 70       /* File typ
3880: 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 74 74  e */.        att
3890: 72 20 3d 20 64 61 74 61 70 2d 3e 64 77 46 69 6c  r = datap->dwFil
38a0: 65 41 74 74 72 69 62 75 74 65 73 3b 0a 20 20 20  eAttributes;.   
38b0: 20 20 20 20 20 69 66 20 28 28 61 74 74 72 20 26       if ((attr &
38c0: 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f   FILE_ATTRIBUTE_
38d0: 44 45 56 49 43 45 29 20 21 3d 20 30 29 20 7b 0a  DEVICE) != 0) {.
38e0: 20 20 20 20 20 20 20 20 20 20 20 20 65 6e 74 70              entp
38f0: 2d 3e 64 5f 74 79 70 65 20 3d 20 44 54 5f 43 48  ->d_type = DT_CH
3900: 52 3b 0a 20 20 20 20 20 20 20 20 7d 20 65 6c 73  R;.        } els
3910: 65 20 69 66 20 28 28 61 74 74 72 20 26 20 46 49  e if ((attr & FI
3920: 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 44 49 52  LE_ATTRIBUTE_DIR
3930: 45 43 54 4f 52 59 29 20 21 3d 20 30 29 20 7b 0a  ECTORY) != 0) {.
3940: 20 20 20 20 20 20 20 20 20 20 20 20 65 6e 74 70              entp
3950: 2d 3e 64 5f 74 79 70 65 20 3d 20 44 54 5f 44 49  ->d_type = DT_DI
3960: 52 3b 0a 20 20 20 20 20 20 20 20 7d 20 65 6c 73  R;.        } els
3970: 65 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  e {.            
3980: 65 6e 74 70 2d 3e 64 5f 74 79 70 65 20 3d 20 44  entp->d_type = D
3990: 54 5f 52 45 47 3b 0a 20 20 20 20 20 20 20 20 7d  T_REG;.        }
39a0: 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73  ..        /* Res
39b0: 65 74 20 64 75 6d 6d 79 20 66 69 65 6c 64 73 20  et dummy fields 
39c0: 2a 2f 0a 20 20 20 20 20 20 20 20 65 6e 74 70 2d  */.        entp-
39d0: 3e 64 5f 69 6e 6f 20 3d 20 30 3b 0a 20 20 20 20  >d_ino = 0;.    
39e0: 20 20 20 20 65 6e 74 70 2d 3e 64 5f 72 65 63 6c      entp->d_recl
39f0: 65 6e 20 3d 20 73 69 7a 65 6f 66 20 28 73 74 72  en = sizeof (str
3a00: 75 63 74 20 5f 77 64 69 72 65 6e 74 29 3b 0a 0a  uct _wdirent);..
3a10: 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 0a 20 20      } else {..  
3a20: 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 64 69        /* Last di
3a30: 72 65 63 74 6f 72 79 20 65 6e 74 72 79 20 72 65  rectory entry re
3a40: 61 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 65 6e  ad */.        en
3a50: 74 70 20 3d 20 4e 55 4c 4c 3b 0a 0a 20 20 20 20  tp = NULL;..    
3a60: 7d 0a 0a 20 20 20 20 72 65 74 75 72 6e 20 65 6e  }..    return en
3a70: 74 70 3b 0a 7d 0a 0a 2f 2a 0a 20 2a 20 43 6c 6f  tp;.}../*. * Clo
3a80: 73 65 20 64 69 72 65 63 74 6f 72 79 20 73 74 72  se directory str
3a90: 65 61 6d 20 6f 70 65 6e 65 64 20 62 79 20 6f 70  eam opened by op
3aa0: 65 6e 64 69 72 28 29 20 66 75 6e 63 74 69 6f 6e  endir() function
3ab0: 2e 20 20 54 68 69 73 20 69 6e 76 61 6c 69 64 61  .  This invalida
3ac0: 74 65 73 20 74 68 65 0a 20 2a 20 44 49 52 20 73  tes the. * DIR s
3ad0: 74 72 75 63 74 75 72 65 20 61 73 20 77 65 6c 6c  tructure as well
3ae0: 20 61 73 20 61 6e 79 20 64 69 72 65 63 74 6f 72   as any director
3af0: 79 20 65 6e 74 72 79 20 72 65 61 64 20 70 72 65  y entry read pre
3b00: 76 69 6f 75 73 6c 79 20 62 79 0a 20 2a 20 5f 77  viously by. * _w
3b10: 72 65 61 64 64 69 72 28 29 2e 0a 20 2a 2f 0a 73  readdir().. */.s
3b20: 74 61 74 69 63 20 69 6e 74 0a 5f 77 63 6c 6f 73  tatic int._wclos
3b30: 65 64 69 72 28 0a 20 20 20 20 5f 57 44 49 52 20  edir(.    _WDIR 
3b40: 2a 64 69 72 70 29 0a 7b 0a 20 20 20 20 69 6e 74  *dirp).{.    int
3b50: 20 6f 6b 3b 0a 20 20 20 20 69 66 20 28 64 69 72   ok;.    if (dir
3b60: 70 29 20 7b 0a 0a 20 20 20 20 20 20 20 20 2f 2a  p) {..        /*
3b70: 20 52 65 6c 65 61 73 65 20 73 65 61 72 63 68 20   Release search 
3b80: 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 20 20 20 20  handle */.      
3b90: 20 20 69 66 20 28 64 69 72 70 2d 3e 68 61 6e 64    if (dirp->hand
3ba0: 6c 65 20 21 3d 20 49 4e 56 41 4c 49 44 5f 48 41  le != INVALID_HA
3bb0: 4e 44 4c 45 5f 56 41 4c 55 45 29 20 7b 0a 20 20  NDLE_VALUE) {.  
3bc0: 20 20 20 20 20 20 20 20 20 20 46 69 6e 64 43 6c            FindCl
3bd0: 6f 73 65 20 28 64 69 72 70 2d 3e 68 61 6e 64 6c  ose (dirp->handl
3be0: 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  e);.            
3bf0: 64 69 72 70 2d 3e 68 61 6e 64 6c 65 20 3d 20 49  dirp->handle = I
3c00: 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41  NVALID_HANDLE_VA
3c10: 4c 55 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a  LUE;.        }..
3c20: 20 20 20 20 20 20 20 20 2f 2a 20 52 65 6c 65 61          /* Relea
3c30: 73 65 20 73 65 61 72 63 68 20 70 61 74 74 65 72  se search patter
3c40: 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 20  n */.        if 
3c50: 28 64 69 72 70 2d 3e 70 61 74 74 29 20 7b 0a 20  (dirp->patt) {. 
3c60: 20 20 20 20 20 20 20 20 20 20 20 66 72 65 65 20             free 
3c70: 28 64 69 72 70 2d 3e 70 61 74 74 29 3b 0a 20 20  (dirp->patt);.  
3c80: 20 20 20 20 20 20 20 20 20 20 64 69 72 70 2d 3e            dirp->
3c90: 70 61 74 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  patt = NULL;.   
3ca0: 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20       }..        
3cb0: 2f 2a 20 52 65 6c 65 61 73 65 20 64 69 72 65 63  /* Release direc
3cc0: 74 6f 72 79 20 73 74 72 75 63 74 75 72 65 20 2a  tory structure *
3cd0: 2f 0a 20 20 20 20 20 20 20 20 66 72 65 65 20 28  /.        free (
3ce0: 64 69 72 70 29 3b 0a 20 20 20 20 20 20 20 20 6f  dirp);.        o
3cf0: 6b 20 3d 20 2f 2a 73 75 63 63 65 73 73 2a 2f 30  k = /*success*/0
3d00: 3b 0a 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a  ;..    } else {.
3d10: 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 76 61 6c          /* Inval
3d20: 69 64 20 64 69 72 65 63 74 6f 72 79 20 73 74 72  id directory str
3d30: 65 61 6d 20 2a 2f 0a 20 20 20 20 20 20 20 20 64  eam */.        d
3d40: 69 72 65 6e 74 5f 73 65 74 5f 65 72 72 6e 6f 20  irent_set_errno 
3d50: 28 45 42 41 44 46 29 3b 0a 20 20 20 20 20 20 20  (EBADF);.       
3d60: 20 6f 6b 20 3d 20 2f 2a 66 61 69 6c 75 72 65 2a   ok = /*failure*
3d70: 2f 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  /-1;.    }.    r
3d80: 65 74 75 72 6e 20 6f 6b 3b 0a 7d 0a 0a 2f 2a 0a  eturn ok;.}../*.
3d90: 20 2a 20 52 65 77 69 6e 64 20 64 69 72 65 63 74   * Rewind direct
3da0: 6f 72 79 20 73 74 72 65 61 6d 20 73 75 63 68 20  ory stream such 
3db0: 74 68 61 74 20 5f 77 72 65 61 64 64 69 72 28 29  that _wreaddir()
3dc0: 20 72 65 74 75 72 6e 73 20 74 68 65 20 76 65 72   returns the ver
3dd0: 79 20 66 69 72 73 74 0a 20 2a 20 66 69 6c 65 20  y first. * file 
3de0: 6e 61 6d 65 20 61 67 61 69 6e 2e 0a 20 2a 2f 0a  name again.. */.
3df0: 73 74 61 74 69 63 20 76 6f 69 64 0a 5f 77 72 65  static void._wre
3e00: 77 69 6e 64 64 69 72 28 0a 20 20 20 20 5f 57 44  winddir(.    _WD
3e10: 49 52 2a 20 64 69 72 70 29 0a 7b 0a 20 20 20 20  IR* dirp).{.    
3e20: 69 66 20 28 64 69 72 70 29 20 7b 0a 20 20 20 20  if (dirp) {.    
3e30: 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 65      /* Release e
3e40: 78 69 73 74 69 6e 67 20 73 65 61 72 63 68 20 68  xisting search h
3e50: 61 6e 64 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20  andle */.       
3e60: 20 69 66 20 28 64 69 72 70 2d 3e 68 61 6e 64 6c   if (dirp->handl
3e70: 65 20 21 3d 20 49 4e 56 41 4c 49 44 5f 48 41 4e  e != INVALID_HAN
3e80: 44 4c 45 5f 56 41 4c 55 45 29 20 7b 0a 20 20 20  DLE_VALUE) {.   
3e90: 20 20 20 20 20 20 20 20 20 46 69 6e 64 43 6c 6f           FindClo
3ea0: 73 65 20 28 64 69 72 70 2d 3e 68 61 6e 64 6c 65  se (dirp->handle
3eb0: 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  );.        }..  
3ec0: 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 6e 65        /* Open ne
3ed0: 77 20 73 65 61 72 63 68 20 68 61 6e 64 6c 65 20  w search handle 
3ee0: 2a 2f 0a 20 20 20 20 20 20 20 20 64 69 72 65 6e  */.        diren
3ef0: 74 5f 66 69 72 73 74 20 28 64 69 72 70 29 3b 0a  t_first (dirp);.
3f00: 20 20 20 20 7d 0a 7d 0a 0a 2f 2a 20 47 65 74 20      }.}../* Get 
3f10: 66 69 72 73 74 20 64 69 72 65 63 74 6f 72 79 20  first directory 
3f20: 65 6e 74 72 79 20 28 69 6e 74 65 72 6e 61 6c 29  entry (internal)
3f30: 20 2a 2f 0a 73 74 61 74 69 63 20 57 49 4e 33 32   */.static WIN32
3f40: 5f 46 49 4e 44 5f 44 41 54 41 57 2a 0a 64 69 72  _FIND_DATAW*.dir
3f50: 65 6e 74 5f 66 69 72 73 74 28 0a 20 20 20 20 5f  ent_first(.    _
3f60: 57 44 49 52 20 2a 64 69 72 70 29 0a 7b 0a 20 20  WDIR *dirp).{.  
3f70: 20 20 57 49 4e 33 32 5f 46 49 4e 44 5f 44 41 54    WIN32_FIND_DAT
3f80: 41 57 20 2a 64 61 74 61 70 3b 0a 0a 20 20 20 20  AW *datap;..    
3f90: 2f 2a 20 4f 70 65 6e 20 64 69 72 65 63 74 6f 72  /* Open director
3fa0: 79 20 61 6e 64 20 72 65 74 72 69 65 76 65 20 74  y and retrieve t
3fb0: 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 2a  he first entry *
3fc0: 2f 0a 20 20 20 20 64 69 72 70 2d 3e 68 61 6e 64  /.    dirp->hand
3fd0: 6c 65 20 3d 20 46 69 6e 64 46 69 72 73 74 46 69  le = FindFirstFi
3fe0: 6c 65 57 20 28 64 69 72 70 2d 3e 70 61 74 74 2c  leW (dirp->patt,
3ff0: 20 26 64 69 72 70 2d 3e 64 61 74 61 29 3b 0a 20   &dirp->data);. 
4000: 20 20 20 69 66 20 28 64 69 72 70 2d 3e 68 61 6e     if (dirp->han
4010: 64 6c 65 20 21 3d 20 49 4e 56 41 4c 49 44 5f 48  dle != INVALID_H
4020: 41 4e 44 4c 45 5f 56 41 4c 55 45 29 20 7b 0a 0a  ANDLE_VALUE) {..
4030: 20 20 20 20 20 20 20 20 2f 2a 20 61 20 64 69 72          /* a dir
4040: 65 63 74 6f 72 79 20 65 6e 74 72 79 20 69 73 20  ectory entry is 
4050: 6e 6f 77 20 77 61 69 74 69 6e 67 20 69 6e 20 6d  now waiting in m
4060: 65 6d 6f 72 79 20 2a 2f 0a 20 20 20 20 20 20 20  emory */.       
4070: 20 64 61 74 61 70 20 3d 20 26 64 69 72 70 2d 3e   datap = &dirp->
4080: 64 61 74 61 3b 0a 20 20 20 20 20 20 20 20 64 69  data;.        di
4090: 72 70 2d 3e 63 61 63 68 65 64 20 3d 20 31 3b 0a  rp->cached = 1;.
40a0: 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 0a 20  .    } else {.. 
40b0: 20 20 20 20 20 20 20 2f 2a 20 46 61 69 6c 65 64         /* Failed
40c0: 20 74 6f 20 72 65 2d 6f 70 65 6e 20 64 69 72 65   to re-open dire
40d0: 63 74 6f 72 79 3a 20 6e 6f 20 64 69 72 65 63 74  ctory: no direct
40e0: 6f 72 79 20 65 6e 74 72 79 20 69 6e 20 6d 65 6d  ory entry in mem
40f0: 6f 72 79 20 2a 2f 0a 20 20 20 20 20 20 20 20 64  ory */.        d
4100: 69 72 70 2d 3e 63 61 63 68 65 64 20 3d 20 30 3b  irp->cached = 0;
4110: 0a 20 20 20 20 20 20 20 20 64 61 74 61 70 20 3d  .        datap =
4120: 20 4e 55 4c 4c 3b 0a 0a 20 20 20 20 7d 0a 20 20   NULL;..    }.  
4130: 20 20 72 65 74 75 72 6e 20 64 61 74 61 70 3b 0a    return datap;.
4140: 7d 0a 0a 2f 2a 20 47 65 74 20 6e 65 78 74 20 64  }../* Get next d
4150: 69 72 65 63 74 6f 72 79 20 65 6e 74 72 79 20 28  irectory entry (
4160: 69 6e 74 65 72 6e 61 6c 29 20 2a 2f 0a 73 74 61  internal) */.sta
4170: 74 69 63 20 57 49 4e 33 32 5f 46 49 4e 44 5f 44  tic WIN32_FIND_D
4180: 41 54 41 57 2a 0a 64 69 72 65 6e 74 5f 6e 65 78  ATAW*.dirent_nex
4190: 74 28 0a 20 20 20 20 5f 57 44 49 52 20 2a 64 69  t(.    _WDIR *di
41a0: 72 70 29 0a 7b 0a 20 20 20 20 57 49 4e 33 32 5f  rp).{.    WIN32_
41b0: 46 49 4e 44 5f 44 41 54 41 57 20 2a 70 3b 0a 0a  FIND_DATAW *p;..
41c0: 20 20 20 20 2f 2a 20 47 65 74 20 6e 65 78 74 20      /* Get next 
41d0: 64 69 72 65 63 74 6f 72 79 20 65 6e 74 72 79 20  directory entry 
41e0: 2a 2f 0a 20 20 20 20 69 66 20 28 64 69 72 70 2d  */.    if (dirp-
41f0: 3e 63 61 63 68 65 64 20 21 3d 20 30 29 20 7b 0a  >cached != 0) {.
4200: 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 20 76 61  .        /* A va
4210: 6c 69 64 20 64 69 72 65 63 74 6f 72 79 20 65 6e  lid directory en
4220: 74 72 79 20 61 6c 72 65 61 64 79 20 69 6e 20 6d  try already in m
4230: 65 6d 6f 72 79 20 2a 2f 0a 20 20 20 20 20 20 20  emory */.       
4240: 20 70 20 3d 20 26 64 69 72 70 2d 3e 64 61 74 61   p = &dirp->data
4250: 3b 0a 20 20 20 20 20 20 20 20 64 69 72 70 2d 3e  ;.        dirp->
4260: 63 61 63 68 65 64 20 3d 20 30 3b 0a 0a 20 20 20  cached = 0;..   
4270: 20 7d 20 65 6c 73 65 20 69 66 20 28 64 69 72 70   } else if (dirp
4280: 2d 3e 68 61 6e 64 6c 65 20 21 3d 20 49 4e 56 41  ->handle != INVA
4290: 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45  LID_HANDLE_VALUE
42a0: 29 20 7b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20  ) {..        /* 
42b0: 47 65 74 20 74 68 65 20 6e 65 78 74 20 64 69 72  Get the next dir
42c0: 65 63 74 6f 72 79 20 65 6e 74 72 79 20 66 72 6f  ectory entry fro
42d0: 6d 20 73 74 72 65 61 6d 20 2a 2f 0a 20 20 20 20  m stream */.    
42e0: 20 20 20 20 69 66 20 28 46 69 6e 64 4e 65 78 74      if (FindNext
42f0: 46 69 6c 65 57 20 28 64 69 72 70 2d 3e 68 61 6e  FileW (dirp->han
4300: 64 6c 65 2c 20 26 64 69 72 70 2d 3e 64 61 74 61  dle, &dirp->data
4310: 29 20 21 3d 20 46 41 4c 53 45 29 20 7b 0a 20 20  ) != FALSE) {.  
4320: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 6f 74            /* Got
4330: 20 61 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 20   a file */.     
4340: 20 20 20 20 20 20 20 70 20 3d 20 26 64 69 72 70         p = &dirp
4350: 2d 3e 64 61 74 61 3b 0a 20 20 20 20 20 20 20 20  ->data;.        
4360: 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20  } else {.       
4370: 20 20 20 20 20 2f 2a 20 54 68 65 20 76 65 72 79       /* The very
4380: 20 6c 61 73 74 20 65 6e 74 72 79 20 68 61 73 20   last entry has 
4390: 62 65 65 6e 20 70 72 6f 63 65 73 73 65 64 20 6f  been processed o
43a0: 72 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  r an error occur
43b0: 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  ed */.          
43c0: 20 20 46 69 6e 64 43 6c 6f 73 65 20 28 64 69 72    FindClose (dir
43d0: 70 2d 3e 68 61 6e 64 6c 65 29 3b 0a 20 20 20 20  p->handle);.    
43e0: 20 20 20 20 20 20 20 20 64 69 72 70 2d 3e 68 61          dirp->ha
43f0: 6e 64 6c 65 20 3d 20 49 4e 56 41 4c 49 44 5f 48  ndle = INVALID_H
4400: 41 4e 44 4c 45 5f 56 41 4c 55 45 3b 0a 20 20 20  ANDLE_VALUE;.   
4410: 20 20 20 20 20 20 20 20 20 70 20 3d 20 4e 55 4c           p = NUL
4420: 4c 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  L;.        }..  
4430: 20 20 7d 20 65 6c 73 65 20 7b 0a 0a 20 20 20 20    } else {..    
4440: 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 64 69      /* End of di
4450: 72 65 63 74 6f 72 79 20 73 74 72 65 61 6d 20 72  rectory stream r
4460: 65 61 63 68 65 64 20 2a 2f 0a 20 20 20 20 20 20  eached */.      
4470: 20 20 70 20 3d 20 4e 55 4c 4c 3b 0a 0a 20 20 20    p = NULL;..   
4480: 20 7d 0a 0a 20 20 20 20 72 65 74 75 72 6e 20 70   }..    return p
4490: 3b 0a 7d 0a 0a 2f 2a 20 0a 20 2a 20 4f 70 65 6e  ;.}../* . * Open
44a0: 20 64 69 72 65 63 74 6f 72 79 20 73 74 72 65 61   directory strea
44b0: 6d 20 75 73 69 6e 67 20 70 6c 61 69 6e 20 6f 6c  m using plain ol
44c0: 64 20 43 2d 73 74 72 69 6e 67 2e 0a 20 2a 2f 0a  d C-string.. */.
44d0: 73 74 61 74 69 63 20 44 49 52 2a 0a 6f 70 65 6e  static DIR*.open
44e0: 64 69 72 28 0a 20 20 20 20 63 6f 6e 73 74 20 63  dir(.    const c
44f0: 68 61 72 20 2a 64 69 72 6e 61 6d 65 29 20 0a 7b  har *dirname) .{
4500: 0a 20 20 20 20 73 74 72 75 63 74 20 44 49 52 20  .    struct DIR 
4510: 2a 64 69 72 70 3b 0a 20 20 20 20 69 6e 74 20 65  *dirp;.    int e
4520: 72 72 6f 72 3b 0a 0a 20 20 20 20 2f 2a 20 4d 75  rror;..    /* Mu
4530: 73 74 20 68 61 76 65 20 64 69 72 65 63 74 6f 72  st have director
4540: 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 69 66  y name */.    if
4550: 20 28 64 69 72 6e 61 6d 65 20 3d 3d 20 4e 55 4c   (dirname == NUL
4560: 4c 20 20 7c 7c 20 20 64 69 72 6e 61 6d 65 5b 30  L  ||  dirname[0
4570: 5d 20 3d 3d 20 27 5c 30 27 29 20 7b 0a 20 20 20  ] == '\0') {.   
4580: 20 20 20 20 20 64 69 72 65 6e 74 5f 73 65 74 5f       dirent_set_
4590: 65 72 72 6e 6f 20 28 45 4e 4f 45 4e 54 29 3b 0a  errno (ENOENT);.
45a0: 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 4e          return N
45b0: 55 4c 4c 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  ULL;.    }..    
45c0: 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f  /* Allocate memo
45d0: 72 79 20 66 6f 72 20 44 49 52 20 73 74 72 75 63  ry for DIR struc
45e0: 74 75 72 65 20 2a 2f 0a 20 20 20 20 64 69 72 70  ture */.    dirp
45f0: 20 3d 20 28 44 49 52 2a 29 20 6d 61 6c 6c 6f 63   = (DIR*) malloc
4600: 20 28 73 69 7a 65 6f 66 20 28 73 74 72 75 63 74   (sizeof (struct
4610: 20 44 49 52 29 29 3b 0a 20 20 20 20 69 66 20 28   DIR));.    if (
4620: 64 69 72 70 29 20 7b 0a 20 20 20 20 20 20 20 20  dirp) {.        
4630: 77 63 68 61 72 5f 74 20 77 6e 61 6d 65 5b 50 41  wchar_t wname[PA
4640: 54 48 5f 4d 41 58 20 2b 20 31 5d 3b 0a 20 20 20  TH_MAX + 1];.   
4650: 20 20 20 20 20 73 69 7a 65 5f 74 20 6e 3b 0a 0a       size_t n;..
4660: 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65          /* Conve
4670: 72 74 20 64 69 72 65 63 74 6f 72 79 20 6e 61 6d  rt directory nam
4680: 65 20 74 6f 20 77 69 64 65 2d 63 68 61 72 61 63  e to wide-charac
4690: 74 65 72 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20  ter string */.  
46a0: 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 64 69        error = di
46b0: 72 65 6e 74 5f 6d 62 73 74 6f 77 63 73 5f 73 28  rent_mbstowcs_s(
46c0: 0a 20 20 20 20 20 20 20 20 20 20 20 20 26 6e 2c  .            &n,
46d0: 20 77 6e 61 6d 65 2c 20 50 41 54 48 5f 4d 41 58   wname, PATH_MAX
46e0: 20 2b 20 31 2c 20 64 69 72 6e 61 6d 65 2c 20 50   + 1, dirname, P
46f0: 41 54 48 5f 4d 41 58 29 3b 0a 20 20 20 20 20 20  ATH_MAX);.      
4700: 20 20 69 66 20 28 21 65 72 72 6f 72 29 20 7b 0a    if (!error) {.
4710: 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  .            /* 
4720: 4f 70 65 6e 20 64 69 72 65 63 74 6f 72 79 20 73  Open directory s
4730: 74 72 65 61 6d 20 75 73 69 6e 67 20 77 69 64 65  tream using wide
4740: 2d 63 68 61 72 61 63 74 65 72 20 6e 61 6d 65 20  -character name 
4750: 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 64  */.            d
4760: 69 72 70 2d 3e 77 64 69 72 70 20 3d 20 5f 77 6f  irp->wdirp = _wo
4770: 70 65 6e 64 69 72 20 28 77 6e 61 6d 65 29 3b 0a  pendir (wname);.
4780: 20 20 20 20 20 20 20 20 20 20 20 20 69 66 20 28              if (
4790: 64 69 72 70 2d 3e 77 64 69 72 70 29 20 7b 0a 20  dirp->wdirp) {. 
47a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
47b0: 2a 20 44 69 72 65 63 74 6f 72 79 20 73 74 72 65  * Directory stre
47c0: 61 6d 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 20  am opened */.   
47d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 65 72 72               err
47e0: 6f 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  or = 0;.        
47f0: 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20      } else {.   
4800: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4810: 46 61 69 6c 65 64 20 74 6f 20 6f 70 65 6e 20 64  Failed to open d
4820: 69 72 65 63 74 6f 72 79 20 73 74 72 65 61 6d 20  irectory stream 
4830: 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  */.             
4840: 20 20 20 65 72 72 6f 72 20 3d 20 31 3b 0a 20 20     error = 1;.  
4850: 20 20 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20            }..   
4860: 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20       } else {.  
4870: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 0a 20 20            /* .  
4880: 20 20 20 20 20 20 20 20 20 20 20 2a 20 43 61 6e             * Can
4890: 6e 6f 74 20 63 6f 6e 76 65 72 74 20 66 69 6c 65  not convert file
48a0: 20 6e 61 6d 65 20 74 6f 20 77 69 64 65 2d 63 68   name to wide-ch
48b0: 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 2e 20  aracter string. 
48c0: 20 54 68 69 73 0a 20 20 20 20 20 20 20 20 20 20   This.          
48d0: 20 20 20 2a 20 6f 63 63 75 72 73 20 69 66 20 74     * occurs if t
48e0: 68 65 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69  he string contai
48f0: 6e 73 20 69 6e 76 61 6c 69 64 20 6d 75 6c 74 69  ns invalid multi
4900: 2d 62 79 74 65 20 73 65 71 75 65 6e 63 65 73 20  -byte sequences 
4910: 6f 72 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  or.             
4920: 2a 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66  * the output buf
4930: 66 65 72 20 69 73 20 74 6f 6f 20 73 6d 61 6c 6c  fer is too small
4940: 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 20   to contain the 
4950: 72 65 73 75 6c 74 69 6e 67 0a 20 20 20 20 20 20  resulting.      
4960: 20 20 20 20 20 20 20 2a 20 73 74 72 69 6e 67 2e         * string.
4970: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f  .             */
4980: 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 72 72  .            err
4990: 6f 72 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  or = 1;.        
49a0: 7d 0a 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a  }..    } else {.
49b0: 20 20 20 20 20 20 20 20 2f 2a 20 43 61 6e 6e 6f          /* Canno
49c0: 74 20 61 6c 6c 6f 63 61 74 65 20 44 49 52 20 73  t allocate DIR s
49d0: 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 20 20  tructure */.    
49e0: 20 20 20 20 65 72 72 6f 72 20 3d 20 31 3b 0a 20      error = 1;. 
49f0: 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6c 65     }..    /* Cle
4a00: 61 6e 20 75 70 20 69 6e 20 63 61 73 65 20 6f 66  an up in case of
4a10: 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 69 66   error */.    if
4a20: 20 28 65 72 72 6f 72 20 20 26 26 20 20 64 69 72   (error  &&  dir
4a30: 70 29 20 7b 0a 20 20 20 20 20 20 20 20 66 72 65  p) {.        fre
4a40: 65 20 28 64 69 72 70 29 3b 0a 20 20 20 20 20 20  e (dirp);.      
4a50: 20 20 64 69 72 70 20 3d 20 4e 55 4c 4c 3b 0a 20    dirp = NULL;. 
4a60: 20 20 20 7d 0a 0a 20 20 20 20 72 65 74 75 72 6e     }..    return
4a70: 20 64 69 72 70 3b 0a 7d 0a 0a 2f 2a 0a 20 2a 20   dirp;.}../*. * 
4a80: 52 65 61 64 20 6e 65 78 74 20 64 69 72 65 63 74  Read next direct
4a90: 6f 72 79 20 65 6e 74 72 79 2e 0a 20 2a 0a 20 2a  ory entry.. *. *
4aa0: 20 57 68 65 6e 20 77 6f 72 6b 69 6e 67 20 77 69   When working wi
4ab0: 74 68 20 74 65 78 74 20 63 6f 6e 73 6f 6c 65 73  th text consoles
4ac0: 2c 20 70 6c 65 61 73 65 20 6e 6f 74 65 20 74 68  , please note th
4ad0: 61 74 20 66 69 6c 65 20 6e 61 6d 65 73 20 72 65  at file names re
4ae0: 74 75 72 6e 65 64 20 62 79 0a 20 2a 20 72 65 61  turned by. * rea
4af0: 64 64 69 72 28 29 20 61 72 65 20 72 65 70 72 65  ddir() are repre
4b00: 73 65 6e 74 65 64 20 69 6e 20 74 68 65 20 64 65  sented in the de
4b10: 66 61 75 6c 74 20 41 4e 53 49 20 63 6f 64 65 20  fault ANSI code 
4b20: 70 61 67 65 20 77 68 69 6c 65 20 61 6e 79 20 6f  page while any o
4b30: 75 74 70 75 74 20 74 6f 0a 20 2a 20 63 6f 6e 73  utput to. * cons
4b40: 6f 6c 65 20 69 73 20 74 79 70 69 63 61 6c 6c 79  ole is typically
4b50: 20 66 6f 72 6d 61 74 74 65 64 20 6f 6e 20 61 6e   formatted on an
4b60: 6f 74 68 65 72 20 63 6f 64 65 20 70 61 67 65 2e  other code page.
4b70: 20 20 54 68 75 73 2c 20 6e 6f 6e 2d 41 53 43 49    Thus, non-ASCI
4b80: 49 0a 20 2a 20 63 68 61 72 61 63 74 65 72 73 20  I. * characters 
4b90: 69 6e 20 66 69 6c 65 20 6e 61 6d 65 73 20 77 69  in file names wi
4ba0: 6c 6c 20 6e 6f 74 20 75 73 75 61 6c 6c 79 20 64  ll not usually d
4bb0: 69 73 70 6c 61 79 20 63 6f 72 72 65 63 74 6c 79  isplay correctly
4bc0: 20 6f 6e 20 63 6f 6e 73 6f 6c 65 2e 20 20 54 68   on console.  Th
4bd0: 65 0a 20 2a 20 70 72 6f 62 6c 65 6d 20 63 61 6e  e. * problem can
4be0: 20 62 65 20 66 69 78 65 64 20 69 6e 20 74 77 6f   be fixed in two
4bf0: 20 77 61 79 73 3a 20 28 31 29 20 63 68 61 6e 67   ways: (1) chang
4c00: 65 20 74 68 65 20 63 68 61 72 61 63 74 65 72 20  e the character 
4c10: 73 65 74 20 6f 66 20 63 6f 6e 73 6f 6c 65 0a 20  set of console. 
4c20: 2a 20 74 6f 20 31 32 35 32 20 75 73 69 6e 67 20  * to 1252 using 
4c30: 63 68 63 70 20 75 74 69 6c 69 74 79 20 61 6e 64  chcp utility and
4c40: 20 75 73 65 20 4c 75 63 69 64 61 20 43 6f 6e 73   use Lucida Cons
4c50: 6f 6c 65 20 66 6f 6e 74 2c 20 6f 72 20 28 32 29  ole font, or (2)
4c60: 20 75 73 65 0a 20 2a 20 5f 63 70 72 69 6e 74 66   use. * _cprintf
4c70: 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 6e 20 77   function when w
4c80: 72 69 74 69 6e 67 20 74 6f 20 63 6f 6e 73 6f 6c  riting to consol
4c90: 65 2e 20 20 54 68 65 20 5f 63 70 72 69 6e 66 28  e.  The _cprinf(
4ca0: 29 20 77 69 6c 6c 20 72 65 2d 65 6e 63 6f 64 65  ) will re-encode
4cb0: 0a 20 2a 20 41 4e 53 49 20 73 74 72 69 6e 67 73  . * ANSI strings
4cc0: 20 74 6f 20 74 68 65 20 63 6f 6e 73 6f 6c 65 20   to the console 
4cd0: 63 6f 64 65 20 70 61 67 65 20 73 6f 20 6d 61 6e  code page so man
4ce0: 79 20 6e 6f 6e 2d 41 53 43 49 49 20 63 68 61 72  y non-ASCII char
4cf0: 61 63 74 65 72 73 20 77 69 6c 6c 0a 20 2a 20 64  acters will. * d
4d00: 69 73 70 6c 61 79 20 63 6f 72 72 65 63 6c 79 2e  isplay correcly.
4d10: 0a 20 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75  . */.static stru
4d20: 63 74 20 64 69 72 65 6e 74 2a 0a 72 65 61 64 64  ct dirent*.readd
4d30: 69 72 28 0a 20 20 20 20 44 49 52 20 2a 64 69 72  ir(.    DIR *dir
4d40: 70 29 20 0a 7b 0a 20 20 20 20 57 49 4e 33 32 5f  p) .{.    WIN32_
4d50: 46 49 4e 44 5f 44 41 54 41 57 20 2a 64 61 74 61  FIND_DATAW *data
4d60: 70 3b 0a 20 20 20 20 73 74 72 75 63 74 20 64 69  p;.    struct di
4d70: 72 65 6e 74 20 2a 65 6e 74 70 3b 0a 0a 20 20 20  rent *entp;..   
4d80: 20 2f 2a 20 52 65 61 64 20 6e 65 78 74 20 64 69   /* Read next di
4d90: 72 65 63 74 6f 72 79 20 65 6e 74 72 79 20 2a 2f  rectory entry */
4da0: 0a 20 20 20 20 64 61 74 61 70 20 3d 20 64 69 72  .    datap = dir
4db0: 65 6e 74 5f 6e 65 78 74 20 28 64 69 72 70 2d 3e  ent_next (dirp->
4dc0: 77 64 69 72 70 29 3b 0a 20 20 20 20 69 66 20 28  wdirp);.    if (
4dd0: 64 61 74 61 70 29 20 7b 0a 20 20 20 20 20 20 20  datap) {.       
4de0: 20 73 69 7a 65 5f 74 20 6e 3b 0a 20 20 20 20 20   size_t n;.     
4df0: 20 20 20 69 6e 74 20 65 72 72 6f 72 3b 0a 0a 20     int error;.. 
4e00: 20 20 20 20 20 20 20 2f 2a 20 41 74 74 65 6d 70         /* Attemp
4e10: 74 20 74 6f 20 63 6f 6e 76 65 72 74 20 66 69 6c  t to convert fil
4e20: 65 20 6e 61 6d 65 20 74 6f 20 6d 75 6c 74 69 2d  e name to multi-
4e30: 62 79 74 65 20 73 74 72 69 6e 67 20 2a 2f 0a 20  byte string */. 
4e40: 20 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 64         error = d
4e50: 69 72 65 6e 74 5f 77 63 73 74 6f 6d 62 73 5f 73  irent_wcstombs_s
4e60: 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 26 6e  (.            &n
4e70: 2c 20 64 69 72 70 2d 3e 65 6e 74 2e 64 5f 6e 61  , dirp->ent.d_na
4e80: 6d 65 2c 20 4d 41 58 5f 50 41 54 48 20 2b 20 31  me, MAX_PATH + 1
4e90: 2c 20 64 61 74 61 70 2d 3e 63 46 69 6c 65 4e 61  , datap->cFileNa
4ea0: 6d 65 2c 20 4d 41 58 5f 50 41 54 48 29 3b 0a 0a  me, MAX_PATH);..
4eb0: 20 20 20 20 20 20 20 20 2f 2a 20 0a 20 20 20 20          /* .    
4ec0: 20 20 20 20 20 2a 20 49 66 20 74 68 65 20 66 69       * If the fi
4ed0: 6c 65 20 6e 61 6d 65 20 63 61 6e 6e 6f 74 20 62  le name cannot b
4ee0: 65 20 72 65 70 72 65 73 65 6e 74 65 64 20 62 79  e represented by
4ef0: 20 61 20 6d 75 6c 74 69 2d 62 79 74 65 20 73 74   a multi-byte st
4f00: 72 69 6e 67 2c 0a 20 20 20 20 20 20 20 20 20 2a  ring,.         *
4f10: 20 74 68 65 6e 20 61 74 74 65 6d 70 74 20 74 6f   then attempt to
4f20: 20 75 73 65 20 6f 6c 64 20 38 2b 33 20 66 69 6c   use old 8+3 fil
4f30: 65 20 6e 61 6d 65 2e 20 20 54 68 69 73 20 61 6c  e name.  This al
4f40: 6c 6f 77 73 20 74 72 61 64 69 74 69 6f 6e 61 6c  lows traditional
4f50: 0a 20 20 20 20 20 20 20 20 20 2a 20 55 6e 69 78  .         * Unix
4f60: 2d 63 6f 64 65 20 74 6f 20 61 63 63 65 73 73 20  -code to access 
4f70: 73 6f 6d 65 20 66 69 6c 65 20 6e 61 6d 65 73 20  some file names 
4f80: 64 65 73 70 69 74 65 20 6f 66 20 75 6e 69 63 6f  despite of unico
4f90: 64 65 0a 20 20 20 20 20 20 20 20 20 2a 20 63 68  de.         * ch
4fa0: 61 72 61 63 74 65 72 73 2c 20 61 6c 74 68 6f 75  aracters, althou
4fb0: 67 68 20 66 69 6c 65 20 6e 61 6d 65 73 20 6d 61  gh file names ma
4fc0: 79 20 73 65 65 6d 20 75 6e 66 61 6d 69 6c 69 61  y seem unfamilia
4fd0: 72 20 74 6f 20 74 68 65 20 75 73 65 72 2e 0a 20  r to the user.. 
4fe0: 20 20 20 20 20 20 20 20 2a 0a 20 20 20 20 20 20          *.      
4ff0: 20 20 20 2a 20 42 65 20 77 61 72 65 20 74 68 61     * Be ware tha
5000: 74 20 74 68 65 20 63 6f 64 65 20 62 65 6c 6f 77  t the code below
5010: 20 63 61 6e 6e 6f 74 20 63 6f 6d 65 20 75 70 20   cannot come up 
5020: 77 69 74 68 20 61 20 73 68 6f 72 74 20 66 69 6c  with a short fil
5030: 65 0a 20 20 20 20 20 20 20 20 20 2a 20 6e 61 6d  e.         * nam
5040: 65 20 75 6e 6c 65 73 73 20 74 68 65 20 66 69 6c  e unless the fil
5050: 65 20 73 79 73 74 65 6d 20 70 72 6f 76 69 64 65  e system provide
5060: 73 20 6f 6e 65 2e 20 20 41 74 20 6c 65 61 73 74  s one.  At least
5070: 0a 20 20 20 20 20 20 20 20 20 2a 20 56 69 72 74  .         * Virt
5080: 75 61 6c 42 6f 78 20 73 68 61 72 65 64 20 66 6f  ualBox shared fo
5090: 6c 64 65 72 73 20 66 61 69 6c 20 74 6f 20 64 6f  lders fail to do
50a0: 20 74 68 69 73 2e 0a 20 20 20 20 20 20 20 20 20   this..         
50b0: 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 20 28 65  */.        if (e
50c0: 72 72 6f 72 20 20 26 26 20 20 64 61 74 61 70 2d  rror  &&  datap-
50d0: 3e 63 41 6c 74 65 72 6e 61 74 65 46 69 6c 65 4e  >cAlternateFileN
50e0: 61 6d 65 5b 30 5d 20 21 3d 20 27 5c 30 27 29 20  ame[0] != '\0') 
50f0: 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 72  {.            er
5100: 72 6f 72 20 3d 20 64 69 72 65 6e 74 5f 77 63 73  ror = dirent_wcs
5110: 74 6f 6d 62 73 5f 73 28 0a 20 20 20 20 20 20 20  tombs_s(.       
5120: 20 20 20 20 20 20 20 20 20 26 6e 2c 20 64 69 72           &n, dir
5130: 70 2d 3e 65 6e 74 2e 64 5f 6e 61 6d 65 2c 20 4d  p->ent.d_name, M
5140: 41 58 5f 50 41 54 48 20 2b 20 31 2c 20 64 61 74  AX_PATH + 1, dat
5150: 61 70 2d 3e 63 41 6c 74 65 72 6e 61 74 65 46 69  ap->cAlternateFi
5160: 6c 65 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20  leName,.        
5170: 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 20 28          sizeof (
5180: 64 61 74 61 70 2d 3e 63 41 6c 74 65 72 6e 61 74  datap->cAlternat
5190: 65 46 69 6c 65 4e 61 6d 65 29 20 2f 20 0a 20 20  eFileName) / .  
51a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51b0: 20 20 73 69 7a 65 6f 66 20 28 64 61 74 61 70 2d    sizeof (datap-
51c0: 3e 63 41 6c 74 65 72 6e 61 74 65 46 69 6c 65 4e  >cAlternateFileN
51d0: 61 6d 65 5b 30 5d 29 29 3b 0a 20 20 20 20 20 20  ame[0]));.      
51e0: 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 69 66 20    }..        if 
51f0: 28 21 65 72 72 6f 72 29 20 7b 0a 20 20 20 20 20  (!error) {.     
5200: 20 20 20 20 20 20 20 44 57 4f 52 44 20 61 74 74         DWORD att
5210: 72 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 20 20  r;..            
5220: 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 64 69  /* Initialize di
5230: 72 65 63 74 6f 72 79 20 65 6e 74 72 79 20 66 6f  rectory entry fo
5240: 72 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 20 20  r return */.    
5250: 20 20 20 20 20 20 20 20 65 6e 74 70 20 3d 20 26          entp = &
5260: 64 69 72 70 2d 3e 65 6e 74 3b 0a 0a 20 20 20 20  dirp->ent;..    
5270: 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
5280: 68 20 6f 66 20 66 69 6c 65 20 6e 61 6d 65 20 65  h of file name e
5290: 78 63 6c 75 64 69 6e 67 20 7a 65 72 6f 20 74 65  xcluding zero te
52a0: 72 6d 69 6e 61 74 6f 72 20 2a 2f 0a 20 20 20 20  rminator */.    
52b0: 20 20 20 20 20 20 20 20 65 6e 74 70 2d 3e 64 5f          entp->d_
52c0: 6e 61 6d 6c 65 6e 20 3d 20 6e 20 2d 20 31 3b 0a  namlen = n - 1;.
52d0: 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  .            /* 
52e0: 46 69 6c 65 20 61 74 74 72 69 62 75 74 65 73 20  File attributes 
52f0: 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 61  */.            a
5300: 74 74 72 20 3d 20 64 61 74 61 70 2d 3e 64 77 46  ttr = datap->dwF
5310: 69 6c 65 41 74 74 72 69 62 75 74 65 73 3b 0a 20  ileAttributes;. 
5320: 20 20 20 20 20 20 20 20 20 20 20 69 66 20 28 28             if ((
5330: 61 74 74 72 20 26 20 46 49 4c 45 5f 41 54 54 52  attr & FILE_ATTR
5340: 49 42 55 54 45 5f 44 45 56 49 43 45 29 20 21 3d  IBUTE_DEVICE) !=
5350: 20 30 29 20 7b 0a 20 20 20 20 20 20 20 20 20 20   0) {.          
5360: 20 20 20 20 20 20 65 6e 74 70 2d 3e 64 5f 74 79        entp->d_ty
5370: 70 65 20 3d 20 44 54 5f 43 48 52 3b 0a 20 20 20  pe = DT_CHR;.   
5380: 20 20 20 20 20 20 20 20 20 7d 20 65 6c 73 65 20           } else 
5390: 69 66 20 28 28 61 74 74 72 20 26 20 46 49 4c 45  if ((attr & FILE
53a0: 5f 41 54 54 52 49 42 55 54 45 5f 44 49 52 45 43  _ATTRIBUTE_DIREC
53b0: 54 4f 52 59 29 20 21 3d 20 30 29 20 7b 0a 20 20  TORY) != 0) {.  
53c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 65 6e                en
53d0: 74 70 2d 3e 64 5f 74 79 70 65 20 3d 20 44 54 5f  tp->d_type = DT_
53e0: 44 49 52 3b 0a 20 20 20 20 20 20 20 20 20 20 20  DIR;.           
53f0: 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20   } else {.      
5400: 20 20 20 20 20 20 20 20 20 20 65 6e 74 70 2d 3e            entp->
5410: 64 5f 74 79 70 65 20 3d 20 44 54 5f 52 45 47 3b  d_type = DT_REG;
5420: 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 0a  .            }..
5430: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
5440: 65 73 65 74 20 64 75 6d 6d 79 20 66 69 65 6c 64  eset dummy field
5450: 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  s */.           
5460: 20 65 6e 74 70 2d 3e 64 5f 69 6e 6f 20 3d 20 30   entp->d_ino = 0
5470: 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 6e  ;.            en
5480: 74 70 2d 3e 64 5f 72 65 63 6c 65 6e 20 3d 20 73  tp->d_reclen = s
5490: 69 7a 65 6f 66 20 28 73 74 72 75 63 74 20 64 69  izeof (struct di
54a0: 72 65 6e 74 29 3b 0a 0a 20 20 20 20 20 20 20 20  rent);..        
54b0: 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20  } else {.       
54c0: 20 20 20 20 20 2f 2a 20 0a 20 20 20 20 20 20 20       /* .       
54d0: 20 20 20 20 20 20 2a 20 43 61 6e 6e 6f 74 20 63        * Cannot c
54e0: 6f 6e 76 65 72 74 20 66 69 6c 65 20 6e 61 6d 65  onvert file name
54f0: 20 74 6f 20 6d 75 6c 74 69 2d 62 79 74 65 20 73   to multi-byte s
5500: 74 72 69 6e 67 20 73 6f 20 63 6f 6e 73 74 72 75  tring so constru
5510: 63 74 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ct.             
5520: 2a 20 61 6e 20 65 72 72 6f 72 6e 6f 75 73 20 64  * an errornous d
5530: 69 72 65 63 74 6f 72 79 20 65 6e 74 72 79 20 61  irectory entry a
5540: 6e 64 20 72 65 74 75 72 6e 20 74 68 61 74 2e 20  nd return that. 
5550: 20 4e 6f 74 65 20 74 68 61 74 0a 20 20 20 20 20   Note that.     
5560: 20 20 20 20 20 20 20 20 2a 20 77 65 20 63 61 6e          * we can
5570: 6e 6f 74 20 72 65 74 75 72 6e 20 4e 55 4c 4c 20  not return NULL 
5580: 61 73 20 74 68 61 74 20 77 6f 75 6c 64 20 73 74  as that would st
5590: 6f 70 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e  op the processin
55a0: 67 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 2a  g.             *
55b0: 20 6f 66 20 64 69 72 65 63 74 6f 72 79 20 65 6e   of directory en
55c0: 74 72 69 65 73 20 63 6f 6d 70 6c 65 74 65 6c 79  tries completely
55d0: 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 2a  ..             *
55e0: 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 6e  /.            en
55f0: 74 70 20 3d 20 26 64 69 72 70 2d 3e 65 6e 74 3b  tp = &dirp->ent;
5600: 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 6e 74  .            ent
5610: 70 2d 3e 64 5f 6e 61 6d 65 5b 30 5d 20 3d 20 27  p->d_name[0] = '
5620: 3f 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ?';.            
5630: 65 6e 74 70 2d 3e 64 5f 6e 61 6d 65 5b 31 5d 20  entp->d_name[1] 
5640: 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20  = '\0';.        
5650: 20 20 20 20 65 6e 74 70 2d 3e 64 5f 6e 61 6d 6c      entp->d_naml
5660: 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  en = 1;.        
5670: 20 20 20 20 65 6e 74 70 2d 3e 64 5f 74 79 70 65      entp->d_type
5680: 20 3d 20 44 54 5f 55 4e 4b 4e 4f 57 4e 3b 0a 20   = DT_UNKNOWN;. 
5690: 20 20 20 20 20 20 20 20 20 20 20 65 6e 74 70 2d             entp-
56a0: 3e 64 5f 69 6e 6f 20 3d 20 30 3b 0a 20 20 20 20  >d_ino = 0;.    
56b0: 20 20 20 20 20 20 20 20 65 6e 74 70 2d 3e 64 5f          entp->d_
56c0: 72 65 63 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20  reclen = 0;.    
56d0: 20 20 20 20 7d 0a 0a 20 20 20 20 7d 20 65 6c 73      }..    } els
56e0: 65 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e  e {.        /* N
56f0: 6f 20 6d 6f 72 65 20 64 69 72 65 63 74 6f 72 79  o more directory
5700: 20 65 6e 74 72 69 65 73 20 2a 2f 0a 20 20 20 20   entries */.    
5710: 20 20 20 20 65 6e 74 70 20 3d 20 4e 55 4c 4c 3b      entp = NULL;
5720: 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 65 74 75  .    }..    retu
5730: 72 6e 20 65 6e 74 70 3b 0a 7d 0a 0a 2f 2a 0a 20  rn entp;.}../*. 
5740: 2a 20 43 6c 6f 73 65 20 64 69 72 65 63 74 6f 72  * Close director
5750: 79 20 73 74 72 65 61 6d 2e 0a 20 2a 2f 0a 73 74  y stream.. */.st
5760: 61 74 69 63 20 69 6e 74 0a 63 6c 6f 73 65 64 69  atic int.closedi
5770: 72 28 0a 20 20 20 20 44 49 52 20 2a 64 69 72 70  r(.    DIR *dirp
5780: 29 20 0a 7b 0a 20 20 20 20 69 6e 74 20 6f 6b 3b  ) .{.    int ok;
5790: 0a 20 20 20 20 69 66 20 28 64 69 72 70 29 20 7b  .    if (dirp) {
57a0: 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 6c 6f  ..        /* Clo
57b0: 73 65 20 77 69 64 65 2d 63 68 61 72 61 63 74 65  se wide-characte
57c0: 72 20 64 69 72 65 63 74 6f 72 79 20 73 74 72 65  r directory stre
57d0: 61 6d 20 2a 2f 0a 20 20 20 20 20 20 20 20 6f 6b  am */.        ok
57e0: 20 3d 20 5f 77 63 6c 6f 73 65 64 69 72 20 28 64   = _wclosedir (d
57f0: 69 72 70 2d 3e 77 64 69 72 70 29 3b 0a 20 20 20  irp->wdirp);.   
5800: 20 20 20 20 20 64 69 72 70 2d 3e 77 64 69 72 70       dirp->wdirp
5810: 20 3d 20 4e 55 4c 4c 3b 0a 0a 20 20 20 20 20 20   = NULL;..      
5820: 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 6d 75 6c    /* Release mul
5830: 74 69 2d 62 79 74 65 20 63 68 61 72 61 63 74 65  ti-byte characte
5840: 72 20 76 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 20  r version */.   
5850: 20 20 20 20 20 66 72 65 65 20 28 64 69 72 70 29       free (dirp)
5860: 3b 0a 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a  ;..    } else {.
5870: 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 76 61  .        /* Inva
5880: 6c 69 64 20 64 69 72 65 63 74 6f 72 79 20 73 74  lid directory st
5890: 72 65 61 6d 20 2a 2f 0a 20 20 20 20 20 20 20 20  ream */.        
58a0: 64 69 72 65 6e 74 5f 73 65 74 5f 65 72 72 6e 6f  dirent_set_errno
58b0: 20 28 45 42 41 44 46 29 3b 0a 20 20 20 20 20 20   (EBADF);.      
58c0: 20 20 6f 6b 20 3d 20 2f 2a 66 61 69 6c 75 72 65    ok = /*failure
58d0: 2a 2f 2d 31 3b 0a 0a 20 20 20 20 7d 0a 20 20 20  */-1;..    }.   
58e0: 20 72 65 74 75 72 6e 20 6f 6b 3b 0a 7d 0a 0a 2f   return ok;.}../
58f0: 2a 0a 20 2a 20 52 65 77 69 6e 64 20 64 69 72 65  *. * Rewind dire
5900: 63 74 6f 72 79 20 73 74 72 65 61 6d 20 74 6f 20  ctory stream to 
5910: 62 65 67 69 6e 6e 69 6e 67 2e 0a 20 2a 2f 0a 73  beginning.. */.s
5920: 74 61 74 69 63 20 76 6f 69 64 0a 72 65 77 69 6e  tatic void.rewin
5930: 64 64 69 72 28 0a 20 20 20 20 44 49 52 2a 20 64  ddir(.    DIR* d
5940: 69 72 70 29 20 0a 7b 0a 20 20 20 20 2f 2a 20 52  irp) .{.    /* R
5950: 65 77 69 6e 64 20 77 69 64 65 2d 63 68 61 72 61  ewind wide-chara
5960: 63 74 65 72 20 73 74 72 69 6e 67 20 64 69 72 65  cter string dire
5970: 63 74 6f 72 79 20 73 74 72 65 61 6d 20 2a 2f 0a  ctory stream */.
5980: 20 20 20 20 5f 77 72 65 77 69 6e 64 64 69 72 20      _wrewinddir 
5990: 28 64 69 72 70 2d 3e 77 64 69 72 70 29 3b 0a 7d  (dirp->wdirp);.}
59a0: 0a 0a 2f 2a 20 43 6f 6e 76 65 72 74 20 6d 75 6c  ../* Convert mul
59b0: 74 69 2d 62 79 74 65 20 73 74 72 69 6e 67 20 74  ti-byte string t
59c0: 6f 20 77 69 64 65 20 63 68 61 72 61 63 74 65 72  o wide character
59d0: 20 73 74 72 69 6e 67 20 2a 2f 0a 73 74 61 74 69   string */.stati
59e0: 63 20 69 6e 74 0a 64 69 72 65 6e 74 5f 6d 62 73  c int.dirent_mbs
59f0: 74 6f 77 63 73 5f 73 28 0a 20 20 20 20 73 69 7a  towcs_s(.    siz
5a00: 65 5f 74 20 2a 70 52 65 74 75 72 6e 56 61 6c 75  e_t *pReturnValu
5a10: 65 2c 0a 20 20 20 20 77 63 68 61 72 5f 74 20 2a  e,.    wchar_t *
5a20: 77 63 73 74 72 2c 0a 20 20 20 20 73 69 7a 65 5f  wcstr,.    size_
5a30: 74 20 73 69 7a 65 49 6e 57 6f 72 64 73 2c 0a 20  t sizeInWords,. 
5a40: 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6d     const char *m
5a50: 62 73 74 72 2c 0a 20 20 20 20 73 69 7a 65 5f 74  bstr,.    size_t
5a60: 20 63 6f 75 6e 74 29 0a 7b 0a 20 20 20 20 69 6e   count).{.    in
5a70: 74 20 65 72 72 6f 72 3b 0a 0a 23 69 66 20 64 65  t error;..#if de
5a80: 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 20  fined(_MSC_VER) 
5a90: 20 26 26 20 20 5f 4d 53 43 5f 56 45 52 20 3e 3d   &&  _MSC_VER >=
5aa0: 20 31 34 30 30 0a 0a 20 20 20 20 2f 2a 20 4d 69   1400..    /* Mi
5ab0: 63 72 6f 73 6f 66 74 20 56 69 73 75 61 6c 20 53  crosoft Visual S
5ac0: 74 75 64 69 6f 20 32 30 30 35 20 6f 72 20 6c 61  tudio 2005 or la
5ad0: 74 65 72 20 2a 2f 0a 20 20 20 20 65 72 72 6f 72  ter */.    error
5ae0: 20 3d 20 6d 62 73 74 6f 77 63 73 5f 73 20 28 70   = mbstowcs_s (p
5af0: 52 65 74 75 72 6e 56 61 6c 75 65 2c 20 77 63 73  ReturnValue, wcs
5b00: 74 72 2c 20 73 69 7a 65 49 6e 57 6f 72 64 73 2c  tr, sizeInWords,
5b10: 20 6d 62 73 74 72 2c 20 63 6f 75 6e 74 29 3b 0a   mbstr, count);.
5b20: 0a 23 65 6c 73 65 0a 0a 20 20 20 20 2f 2a 20 4f  .#else..    /* O
5b30: 6c 64 65 72 20 56 69 73 75 61 6c 20 53 74 75 64  lder Visual Stud
5b40: 69 6f 20 6f 72 20 6e 6f 6e 2d 4d 69 63 72 6f 73  io or non-Micros
5b50: 6f 66 74 20 63 6f 6d 70 69 6c 65 72 20 2a 2f 0a  oft compiler */.
5b60: 20 20 20 20 73 69 7a 65 5f 74 20 6e 3b 0a 0a 20      size_t n;.. 
5b70: 20 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74 6f     /* Convert to
5b80: 20 77 69 64 65 2d 63 68 61 72 61 63 74 65 72 20   wide-character 
5b90: 73 74 72 69 6e 67 20 2a 2f 0a 20 20 20 20 6e 20  string */.    n 
5ba0: 3d 20 6d 62 73 74 6f 77 63 73 20 28 77 63 73 74  = mbstowcs (wcst
5bb0: 72 2c 20 6d 62 73 74 72 2c 20 63 6f 75 6e 74 29  r, mbstr, count)
5bc0: 3b 0a 20 20 20 20 69 66 20 28 6e 20 3c 20 73 69  ;.    if (n < si
5bd0: 7a 65 49 6e 57 6f 72 64 73 29 20 7b 0a 0a 20 20  zeInWords) {..  
5be0: 20 20 20 20 20 20 2f 2a 20 5a 65 72 6f 2d 74 65        /* Zero-te
5bf0: 72 6d 69 6e 61 74 65 20 6f 75 74 70 75 74 20 62  rminate output b
5c00: 75 66 66 65 72 20 2a 2f 0a 20 20 20 20 20 20 20  uffer */.       
5c10: 20 69 66 20 28 77 63 73 74 72 29 20 7b 0a 20 20   if (wcstr) {.  
5c20: 20 20 20 20 20 20 20 20 20 20 77 63 73 74 72 5b            wcstr[
5c30: 6e 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  n] = 0;.        
5c40: 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 4c 65  }..        /* Le
5c50: 6e 67 74 68 20 6f 66 20 72 65 73 75 74 69 6e 67  ngth of resuting
5c60: 20 6d 75 6c 74 69 2d 62 79 74 65 20 73 74 72 69   multi-byte stri
5c70: 6e 67 20 57 49 54 48 20 7a 65 72 6f 20 74 65 72  ng WITH zero ter
5c80: 6d 69 6e 61 74 6f 72 20 2a 2f 0a 20 20 20 20 20  minator */.     
5c90: 20 20 20 69 66 20 28 70 52 65 74 75 72 6e 56 61     if (pReturnVa
5ca0: 6c 75 65 29 20 7b 0a 20 20 20 20 20 20 20 20 20  lue) {.         
5cb0: 20 20 20 2a 70 52 65 74 75 72 6e 56 61 6c 75 65     *pReturnValue
5cc0: 20 3d 20 6e 20 2b 20 31 3b 0a 20 20 20 20 20 20   = n + 1;.      
5cd0: 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20    }..        /* 
5ce0: 53 75 63 63 65 73 73 20 2a 2f 0a 20 20 20 20 20  Success */.     
5cf0: 20 20 20 65 72 72 6f 72 20 3d 20 30 3b 0a 0a 20     error = 0;.. 
5d00: 20 20 20 7d 20 65 6c 73 65 20 7b 0a 0a 20 20 20     } else {..   
5d10: 20 20 20 20 20 2f 2a 20 43 6f 75 6c 64 20 6e 6f       /* Could no
5d20: 74 20 63 6f 6e 76 65 72 74 20 73 74 72 69 6e 67  t convert string
5d30: 20 2a 2f 0a 20 20 20 20 20 20 20 20 65 72 72 6f   */.        erro
5d40: 72 20 3d 20 31 3b 0a 0a 20 20 20 20 7d 0a 0a 23  r = 1;..    }..#
5d50: 65 6e 64 69 66 0a 0a 20 20 20 20 72 65 74 75 72  endif..    retur
5d60: 6e 20 65 72 72 6f 72 3b 0a 7d 0a 0a 2f 2a 20 43  n error;.}../* C
5d70: 6f 6e 76 65 72 74 20 77 69 64 65 2d 63 68 61 72  onvert wide-char
5d80: 61 63 74 65 72 20 73 74 72 69 6e 67 20 74 6f 20  acter string to 
5d90: 6d 75 6c 74 69 2d 62 79 74 65 20 73 74 72 69 6e  multi-byte strin
5da0: 67 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 0a  g */.static int.
5db0: 64 69 72 65 6e 74 5f 77 63 73 74 6f 6d 62 73 5f  dirent_wcstombs_
5dc0: 73 28 0a 20 20 20 20 73 69 7a 65 5f 74 20 2a 70  s(.    size_t *p
5dd0: 52 65 74 75 72 6e 56 61 6c 75 65 2c 0a 20 20 20  ReturnValue,.   
5de0: 20 63 68 61 72 20 2a 6d 62 73 74 72 2c 0a 20 20   char *mbstr,.  
5df0: 20 20 73 69 7a 65 5f 74 20 73 69 7a 65 49 6e 42    size_t sizeInB
5e00: 79 74 65 73 2c 0a 20 20 20 20 63 6f 6e 73 74 20  ytes,.    const 
5e10: 77 63 68 61 72 5f 74 20 2a 77 63 73 74 72 2c 0a  wchar_t *wcstr,.
5e20: 20 20 20 20 73 69 7a 65 5f 74 20 63 6f 75 6e 74      size_t count
5e30: 29 0a 7b 0a 20 20 20 20 69 6e 74 20 65 72 72 6f  ).{.    int erro
5e40: 72 3b 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28  r;..#if defined(
5e50: 5f 4d 53 43 5f 56 45 52 29 20 20 26 26 20 20 5f  _MSC_VER)  &&  _
5e60: 4d 53 43 5f 56 45 52 20 3e 3d 20 31 34 30 30 0a  MSC_VER >= 1400.
5e70: 0a 20 20 20 20 2f 2a 20 4d 69 63 72 6f 73 6f 66  .    /* Microsof
5e80: 74 20 56 69 73 75 61 6c 20 53 74 75 64 69 6f 20  t Visual Studio 
5e90: 32 30 30 35 20 6f 72 20 6c 61 74 65 72 20 2a 2f  2005 or later */
5ea0: 0a 20 20 20 20 65 72 72 6f 72 20 3d 20 77 63 73  .    error = wcs
5eb0: 74 6f 6d 62 73 5f 73 20 28 70 52 65 74 75 72 6e  tombs_s (pReturn
5ec0: 56 61 6c 75 65 2c 20 6d 62 73 74 72 2c 20 73 69  Value, mbstr, si
5ed0: 7a 65 49 6e 42 79 74 65 73 2c 20 77 63 73 74 72  zeInBytes, wcstr
5ee0: 2c 20 63 6f 75 6e 74 29 3b 0a 0a 23 65 6c 73 65  , count);..#else
5ef0: 0a 0a 20 20 20 20 2f 2a 20 4f 6c 64 65 72 20 56  ..    /* Older V
5f00: 69 73 75 61 6c 20 53 74 75 64 69 6f 20 6f 72 20  isual Studio or 
5f10: 6e 6f 6e 2d 4d 69 63 72 6f 73 6f 66 74 20 63 6f  non-Microsoft co
5f20: 6d 70 69 6c 65 72 20 2a 2f 0a 20 20 20 20 73 69  mpiler */.    si
5f30: 7a 65 5f 74 20 6e 3b 0a 0a 20 20 20 20 2f 2a 20  ze_t n;..    /* 
5f40: 43 6f 6e 76 65 72 74 20 74 6f 20 6d 75 6c 74 69  Convert to multi
5f50: 2d 62 79 74 65 20 73 74 72 69 6e 67 20 2a 2f 0a  -byte string */.
5f60: 20 20 20 20 6e 20 3d 20 77 63 73 74 6f 6d 62 73      n = wcstombs
5f70: 20 28 6d 62 73 74 72 2c 20 77 63 73 74 72 2c 20   (mbstr, wcstr, 
5f80: 63 6f 75 6e 74 29 3b 0a 20 20 20 20 69 66 20 28  count);.    if (
5f90: 6e 20 3c 20 73 69 7a 65 49 6e 42 79 74 65 73 29  n < sizeInBytes)
5fa0: 20 7b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 5a   {..        /* Z
5fb0: 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 20 6f 75  ero-terminate ou
5fc0: 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 20  tput buffer */. 
5fd0: 20 20 20 20 20 20 20 69 66 20 28 6d 62 73 74 72         if (mbstr
5fe0: 29 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ) {.            
5ff0: 6d 62 73 74 72 5b 6e 5d 20 3d 20 27 5c 30 27 3b  mbstr[n] = '\0';
6000: 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  .        }..    
6010: 20 20 20 20 2f 2a 20 4c 65 6e 67 68 74 20 6f 66      /* Lenght of
6020: 20 72 65 73 75 6c 74 69 6e 67 20 6d 75 6c 74 69   resulting multi
6030: 2d 62 79 74 65 73 20 73 74 72 69 6e 67 20 57 49  -bytes string WI
6040: 54 48 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74  TH zero-terminat
6050: 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66  or */.        if
6060: 20 28 70 52 65 74 75 72 6e 56 61 6c 75 65 29 20   (pReturnValue) 
6070: 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 70  {.            *p
6080: 52 65 74 75 72 6e 56 61 6c 75 65 20 3d 20 6e 20  ReturnValue = n 
6090: 2b 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a  + 1;.        }..
60a0: 20 20 20 20 20 20 20 20 2f 2a 20 53 75 63 63 65          /* Succe
60b0: 73 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 65 72  ss */.        er
60c0: 72 6f 72 20 3d 20 30 3b 0a 0a 20 20 20 20 7d 20  ror = 0;..    } 
60d0: 65 6c 73 65 20 7b 0a 0a 20 20 20 20 20 20 20 20  else {..        
60e0: 2f 2a 20 43 61 6e 6e 6f 74 20 63 6f 6e 76 65 72  /* Cannot conver
60f0: 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 20 20  t string */.    
6100: 20 20 20 20 65 72 72 6f 72 20 3d 20 31 3b 0a 0a      error = 1;..
6110: 20 20 20 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 20      }..#endif.. 
6120: 20 20 20 72 65 74 75 72 6e 20 65 72 72 6f 72 3b     return error;
6130: 0a 7d 0a 0a 2f 2a 20 53 65 74 20 65 72 72 6e 6f  .}../* Set errno
6140: 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 73 74 61   variable */.sta
6150: 74 69 63 20 76 6f 69 64 0a 64 69 72 65 6e 74 5f  tic void.dirent_
6160: 73 65 74 5f 65 72 72 6e 6f 28 0a 20 20 20 20 69  set_errno(.    i
6170: 6e 74 20 65 72 72 6f 72 29 0a 7b 0a 23 69 66 20  nt error).{.#if 
6180: 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52  defined(_MSC_VER
6190: 29 20 26 26 20 5f 4d 53 43 5f 56 45 52 20 3e 3d  ) && _MSC_VER >=
61a0: 20 31 34 30 30 0a 0a 20 20 20 20 2f 2a 20 4d 69   1400..    /* Mi
61b0: 63 72 6f 73 6f 66 74 20 56 69 73 75 61 6c 20 53  crosoft Visual S
61c0: 74 75 64 69 6f 20 32 30 30 35 20 61 6e 64 20 6c  tudio 2005 and l
61d0: 61 74 65 72 20 2a 2f 0a 20 20 20 20 5f 73 65 74  ater */.    _set
61e0: 5f 65 72 72 6e 6f 20 28 65 72 72 6f 72 29 3b 0a  _errno (error);.
61f0: 0a 23 65 6c 73 65 0a 0a 20 20 20 20 2f 2a 20 4e  .#else..    /* N
6200: 6f 6e 2d 4d 69 63 72 6f 73 6f 66 74 20 63 6f 6d  on-Microsoft com
6210: 70 69 6c 65 72 20 6f 72 20 6f 6c 64 65 72 20 4d  piler or older M
6220: 69 63 72 6f 73 6f 66 74 20 63 6f 6d 70 69 6c 65  icrosoft compile
6230: 72 20 2a 2f 0a 20 20 20 20 65 72 72 6e 6f 20 3d  r */.    errno =
6240: 20 65 72 72 6f 72 3b 0a 0a 23 65 6e 64 69 66 0a   error;..#endif.
6250: 7d 0a 0a 0a 23 69 66 64 65 66 20 5f 5f 63 70 6c  }...#ifdef __cpl
6260: 75 73 70 6c 75 73 0a 7d 0a 23 65 6e 64 69 66 0a  usplus.}.#endif.
6270: 23 65 6e 64 69 66 20 2f 2a 44 49 52 45 4e 54 5f  #endif /*DIRENT_
6280: 48 2a 2f 0a 0a                                   H*/..