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