199a2dd95SBruce Richardson /* SPDX-License-Identifier: MIT
299a2dd95SBruce Richardson * Dirent interface for Microsoft Visual Studio
399a2dd95SBruce Richardson * Version 1.21
499a2dd95SBruce Richardson * Copyright (C) 2006-2012 Toni Ronkko
599a2dd95SBruce Richardson * https://github.com/tronkko/dirent
699a2dd95SBruce Richardson */
799a2dd95SBruce Richardson
899a2dd95SBruce Richardson #ifndef DIRENT_H
999a2dd95SBruce Richardson #define DIRENT_H
1099a2dd95SBruce Richardson
1199a2dd95SBruce Richardson /*
1299a2dd95SBruce Richardson * Include windows.h without Windows Sockets 1.1 to prevent conflicts with
1399a2dd95SBruce Richardson * Windows Sockets 2.0.
1499a2dd95SBruce Richardson */
1599a2dd95SBruce Richardson #ifndef WIN32_LEAN_AND_MEAN
1699a2dd95SBruce Richardson # define WIN32_LEAN_AND_MEAN
1799a2dd95SBruce Richardson #endif
1899a2dd95SBruce Richardson
1999a2dd95SBruce Richardson #include <windows.h>
2099a2dd95SBruce Richardson
2199a2dd95SBruce Richardson #include <stdio.h>
2299a2dd95SBruce Richardson #include <stdarg.h>
2399a2dd95SBruce Richardson #include <wchar.h>
2499a2dd95SBruce Richardson #include <string.h>
2599a2dd95SBruce Richardson #include <stdlib.h>
2699a2dd95SBruce Richardson #include <malloc.h>
2799a2dd95SBruce Richardson #include <sys/types.h>
2899a2dd95SBruce Richardson #include <sys/stat.h>
2999a2dd95SBruce Richardson #include <errno.h>
3099a2dd95SBruce Richardson
3199a2dd95SBruce Richardson /* Maximum length of file name */
3299a2dd95SBruce Richardson #if !defined(PATH_MAX)
3399a2dd95SBruce Richardson # define PATH_MAX MAX_PATH
3499a2dd95SBruce Richardson #endif
3599a2dd95SBruce Richardson
3699a2dd95SBruce Richardson /* File type flags for d_type */
3799a2dd95SBruce Richardson #define DT_UNKNOWN 0
3899a2dd95SBruce Richardson #define DT_REG S_IFREG
3999a2dd95SBruce Richardson #define DT_DIR S_IFDIR
4099a2dd95SBruce Richardson #define DT_CHR S_IFCHR
4199a2dd95SBruce Richardson
4299a2dd95SBruce Richardson /*
4399a2dd95SBruce Richardson * File type macros. Note that block devices, sockets and links cannot be
4499a2dd95SBruce Richardson * distinguished on Windows and the macros S_ISBLK, S_ISSOCK and S_ISLNK are
4599a2dd95SBruce Richardson * only defined for compatibility. These macros should always return false
4699a2dd95SBruce Richardson * on Windows.
4799a2dd95SBruce Richardson */
4899a2dd95SBruce Richardson #if !defined(S_ISDIR)
4999a2dd95SBruce Richardson # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
5099a2dd95SBruce Richardson #endif
5199a2dd95SBruce Richardson #if !defined(S_ISREG)
5299a2dd95SBruce Richardson # define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
5399a2dd95SBruce Richardson #endif
5499a2dd95SBruce Richardson
5599a2dd95SBruce Richardson /* Wide-character version */
5699a2dd95SBruce Richardson struct _wdirent {
5799a2dd95SBruce Richardson /* Always zero */
5899a2dd95SBruce Richardson long d_ino;
5999a2dd95SBruce Richardson
6099a2dd95SBruce Richardson /* Structure size */
6199a2dd95SBruce Richardson unsigned short d_reclen;
6299a2dd95SBruce Richardson
6399a2dd95SBruce Richardson /* Length of name without \0 */
6499a2dd95SBruce Richardson size_t d_namlen;
6599a2dd95SBruce Richardson
6699a2dd95SBruce Richardson /* File type */
6799a2dd95SBruce Richardson int d_type;
6899a2dd95SBruce Richardson
6999a2dd95SBruce Richardson /* File name */
7099a2dd95SBruce Richardson wchar_t d_name[PATH_MAX];
7199a2dd95SBruce Richardson };
7299a2dd95SBruce Richardson typedef struct _wdirent _wdirent;
7399a2dd95SBruce Richardson
7499a2dd95SBruce Richardson struct _WDIR {
7599a2dd95SBruce Richardson /* Current directory entry */
7699a2dd95SBruce Richardson struct _wdirent ent;
7799a2dd95SBruce Richardson
7899a2dd95SBruce Richardson /* Private file data */
7999a2dd95SBruce Richardson WIN32_FIND_DATAW data;
8099a2dd95SBruce Richardson
8199a2dd95SBruce Richardson /* True if data is valid */
8299a2dd95SBruce Richardson int cached;
8399a2dd95SBruce Richardson
8499a2dd95SBruce Richardson /* Win32 search handle */
8599a2dd95SBruce Richardson HANDLE handle;
8699a2dd95SBruce Richardson
8799a2dd95SBruce Richardson /* Initial directory name */
8899a2dd95SBruce Richardson wchar_t *patt;
8999a2dd95SBruce Richardson };
9099a2dd95SBruce Richardson typedef struct _WDIR _WDIR;
9199a2dd95SBruce Richardson
9299a2dd95SBruce Richardson static _WDIR *_wopendir(const wchar_t *dirname);
9399a2dd95SBruce Richardson static int _wclosedir(_WDIR *dirp);
9499a2dd95SBruce Richardson
9599a2dd95SBruce Richardson /* For compatibility with Symbian */
9699a2dd95SBruce Richardson #define wdirent _wdirent
9799a2dd95SBruce Richardson #define WDIR _WDIR
9899a2dd95SBruce Richardson #define wopendir _wopendir
9999a2dd95SBruce Richardson #define wclosedir _wclosedir
10099a2dd95SBruce Richardson
10199a2dd95SBruce Richardson /* Multi-byte character versions */
10299a2dd95SBruce Richardson struct dirent {
10399a2dd95SBruce Richardson /* Always zero */
10499a2dd95SBruce Richardson long d_ino;
10599a2dd95SBruce Richardson
10699a2dd95SBruce Richardson /* Structure size */
10799a2dd95SBruce Richardson unsigned short d_reclen;
10899a2dd95SBruce Richardson
10999a2dd95SBruce Richardson /* Length of name without \0 */
11099a2dd95SBruce Richardson size_t d_namlen;
11199a2dd95SBruce Richardson
11299a2dd95SBruce Richardson /* File type */
11399a2dd95SBruce Richardson int d_type;
11499a2dd95SBruce Richardson
11599a2dd95SBruce Richardson /* File name */
11699a2dd95SBruce Richardson char d_name[PATH_MAX];
11799a2dd95SBruce Richardson };
11899a2dd95SBruce Richardson typedef struct dirent dirent;
11999a2dd95SBruce Richardson
12099a2dd95SBruce Richardson struct DIR {
12199a2dd95SBruce Richardson struct dirent ent;
12299a2dd95SBruce Richardson struct _WDIR *wdirp;
12399a2dd95SBruce Richardson };
12499a2dd95SBruce Richardson typedef struct DIR DIR;
12599a2dd95SBruce Richardson
12699a2dd95SBruce Richardson static DIR *opendir(const char *dirname);
12799a2dd95SBruce Richardson static struct dirent *readdir(DIR *dirp);
12899a2dd95SBruce Richardson static int closedir(DIR *dirp);
12999a2dd95SBruce Richardson
13099a2dd95SBruce Richardson /* Internal utility functions */
13199a2dd95SBruce Richardson static WIN32_FIND_DATAW *dirent_first(_WDIR *dirp);
13299a2dd95SBruce Richardson static WIN32_FIND_DATAW *dirent_next(_WDIR *dirp);
13399a2dd95SBruce Richardson
13499a2dd95SBruce Richardson static int dirent_mbstowcs_s(
13599a2dd95SBruce Richardson size_t *pReturnValue,
13699a2dd95SBruce Richardson wchar_t *wcstr,
13799a2dd95SBruce Richardson size_t sizeInWords,
13899a2dd95SBruce Richardson const char *mbstr,
13999a2dd95SBruce Richardson size_t count);
14099a2dd95SBruce Richardson
14199a2dd95SBruce Richardson static int dirent_wcstombs_s(
14299a2dd95SBruce Richardson size_t *pReturnValue,
14399a2dd95SBruce Richardson char *mbstr,
14499a2dd95SBruce Richardson size_t sizeInBytes,
14599a2dd95SBruce Richardson const wchar_t *wcstr,
14699a2dd95SBruce Richardson size_t count);
14799a2dd95SBruce Richardson
14899a2dd95SBruce Richardson static void dirent_set_errno(int error);
14999a2dd95SBruce Richardson
15099a2dd95SBruce Richardson /*
15199a2dd95SBruce Richardson * Open directory stream DIRNAME for read and return a pointer to the
15299a2dd95SBruce Richardson * internal working area that is used to retrieve individual directory
15399a2dd95SBruce Richardson * entries.
15499a2dd95SBruce Richardson */
15599a2dd95SBruce Richardson static _WDIR*
_wopendir(const wchar_t * dirname)15699a2dd95SBruce Richardson _wopendir(const wchar_t *dirname)
15799a2dd95SBruce Richardson {
15899a2dd95SBruce Richardson _WDIR *dirp = NULL;
15999a2dd95SBruce Richardson int error;
16099a2dd95SBruce Richardson
16199a2dd95SBruce Richardson /* Must have directory name */
16299a2dd95SBruce Richardson if (dirname == NULL || dirname[0] == '\0') {
16399a2dd95SBruce Richardson dirent_set_errno(ENOENT);
16499a2dd95SBruce Richardson return NULL;
16599a2dd95SBruce Richardson }
16699a2dd95SBruce Richardson
16799a2dd95SBruce Richardson /* Allocate new _WDIR structure */
16899a2dd95SBruce Richardson dirp = (_WDIR *)malloc(sizeof(struct _WDIR));
16999a2dd95SBruce Richardson if (dirp != NULL) {
17099a2dd95SBruce Richardson DWORD n;
17199a2dd95SBruce Richardson
17299a2dd95SBruce Richardson /* Reset _WDIR structure */
17399a2dd95SBruce Richardson dirp->handle = INVALID_HANDLE_VALUE;
17499a2dd95SBruce Richardson dirp->patt = NULL;
17599a2dd95SBruce Richardson dirp->cached = 0;
17699a2dd95SBruce Richardson
17799a2dd95SBruce Richardson /* Compute the length of full path plus zero terminator
17899a2dd95SBruce Richardson *
17999a2dd95SBruce Richardson * Note that on WinRT there's no way to convert relative paths
18099a2dd95SBruce Richardson * into absolute paths, so just assume its an absolute path.
18199a2dd95SBruce Richardson */
18299a2dd95SBruce Richardson #if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
18399a2dd95SBruce Richardson n = wcslen(dirname);
18499a2dd95SBruce Richardson #else
18599a2dd95SBruce Richardson n = GetFullPathNameW(dirname, 0, NULL, NULL);
18699a2dd95SBruce Richardson #endif
18799a2dd95SBruce Richardson
18899a2dd95SBruce Richardson /* Allocate room for absolute directory name and search
18999a2dd95SBruce Richardson * pattern
19099a2dd95SBruce Richardson */
19199a2dd95SBruce Richardson dirp->patt = (wchar_t *)malloc(sizeof(wchar_t) * n + 16);
19299a2dd95SBruce Richardson if (dirp->patt) {
19399a2dd95SBruce Richardson /* Convert relative directory name to an
19499a2dd95SBruce Richardson * absolute one. This allows rewinddir() to
19599a2dd95SBruce Richardson * function correctly even when current working
19699a2dd95SBruce Richardson * directory is changed between opendir()
19799a2dd95SBruce Richardson * and rewinddir().
19899a2dd95SBruce Richardson *
19999a2dd95SBruce Richardson * Note that on WinRT there's no way to convert
20099a2dd95SBruce Richardson * relative paths into absolute paths, so just
20199a2dd95SBruce Richardson * assume its an absolute path.
20299a2dd95SBruce Richardson */
20399a2dd95SBruce Richardson #if defined(WINAPI_FAMILY) && \
20499a2dd95SBruce Richardson (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
20599a2dd95SBruce Richardson wcsncpy_s(dirp->patt, n + 1, dirname, n);
20699a2dd95SBruce Richardson #else
20799a2dd95SBruce Richardson n = GetFullPathNameW(dirname, n, dirp->patt, NULL);
20899a2dd95SBruce Richardson #endif
20999a2dd95SBruce Richardson if (n > 0) {
21099a2dd95SBruce Richardson wchar_t *p;
21199a2dd95SBruce Richardson
21299a2dd95SBruce Richardson /* Append search pattern \* to the directory
21399a2dd95SBruce Richardson * name
21499a2dd95SBruce Richardson */
21599a2dd95SBruce Richardson p = dirp->patt + n;
21699a2dd95SBruce Richardson if (dirp->patt < p) {
21799a2dd95SBruce Richardson switch (p[-1]) {
21899a2dd95SBruce Richardson case '\\':
21999a2dd95SBruce Richardson case '/':
22099a2dd95SBruce Richardson case ':':
22199a2dd95SBruce Richardson /* Directory ends in path separator,
22299a2dd95SBruce Richardson * e.g.c:\temp\
22399a2dd95SBruce Richardson */
22499a2dd95SBruce Richardson /*NOP*/;
22599a2dd95SBruce Richardson break;
22699a2dd95SBruce Richardson
22799a2dd95SBruce Richardson default:
22899a2dd95SBruce Richardson /* Directory name doesn't end in path
22999a2dd95SBruce Richardson * separator
23099a2dd95SBruce Richardson */
23199a2dd95SBruce Richardson *p++ = '\\';
23299a2dd95SBruce Richardson }
23399a2dd95SBruce Richardson }
23499a2dd95SBruce Richardson *p++ = '*';
23599a2dd95SBruce Richardson *p = '\0';
23699a2dd95SBruce Richardson
23799a2dd95SBruce Richardson /* Open directory stream and retrieve the first
23899a2dd95SBruce Richardson * entry
23999a2dd95SBruce Richardson */
24099a2dd95SBruce Richardson if (dirent_first(dirp)) {
24199a2dd95SBruce Richardson /* Directory stream opened successfully */
24299a2dd95SBruce Richardson error = 0;
24399a2dd95SBruce Richardson } else {
24499a2dd95SBruce Richardson /* Cannot retrieve first entry */
24599a2dd95SBruce Richardson error = 1;
24699a2dd95SBruce Richardson dirent_set_errno(ENOENT);
24799a2dd95SBruce Richardson }
24899a2dd95SBruce Richardson
24999a2dd95SBruce Richardson } else {
25099a2dd95SBruce Richardson /* Cannot retrieve full path name */
25199a2dd95SBruce Richardson dirent_set_errno(ENOENT);
25299a2dd95SBruce Richardson error = 1;
25399a2dd95SBruce Richardson }
25499a2dd95SBruce Richardson
25599a2dd95SBruce Richardson } else {
25699a2dd95SBruce Richardson /* Cannot allocate memory for search pattern */
25799a2dd95SBruce Richardson error = 1;
25899a2dd95SBruce Richardson }
25999a2dd95SBruce Richardson
26099a2dd95SBruce Richardson } else {
26199a2dd95SBruce Richardson /* Cannot allocate _WDIR structure */
26299a2dd95SBruce Richardson error = 1;
26399a2dd95SBruce Richardson }
26499a2dd95SBruce Richardson
26599a2dd95SBruce Richardson /* Clean up in case of error */
26699a2dd95SBruce Richardson if (error && dirp) {
26799a2dd95SBruce Richardson _wclosedir(dirp);
26899a2dd95SBruce Richardson dirp = NULL;
26999a2dd95SBruce Richardson }
27099a2dd95SBruce Richardson
27199a2dd95SBruce Richardson return dirp;
27299a2dd95SBruce Richardson }
27399a2dd95SBruce Richardson
27499a2dd95SBruce Richardson /*
27599a2dd95SBruce Richardson * Close directory stream opened by opendir() function.
27699a2dd95SBruce Richardson * This invalidates the DIR structure as well as any directory
27799a2dd95SBruce Richardson * entry read previously by _wreaddir().
27899a2dd95SBruce Richardson */
27999a2dd95SBruce Richardson static int
_wclosedir(_WDIR * dirp)28099a2dd95SBruce Richardson _wclosedir(_WDIR *dirp)
28199a2dd95SBruce Richardson {
28299a2dd95SBruce Richardson int ok;
28399a2dd95SBruce Richardson if (dirp) {
28499a2dd95SBruce Richardson
28599a2dd95SBruce Richardson /* Release search handle */
28699a2dd95SBruce Richardson if (dirp->handle != INVALID_HANDLE_VALUE) {
28799a2dd95SBruce Richardson FindClose(dirp->handle);
28899a2dd95SBruce Richardson dirp->handle = INVALID_HANDLE_VALUE;
28999a2dd95SBruce Richardson }
29099a2dd95SBruce Richardson
29199a2dd95SBruce Richardson /* Release search pattern */
29299a2dd95SBruce Richardson if (dirp->patt) {
29399a2dd95SBruce Richardson free(dirp->patt);
29499a2dd95SBruce Richardson dirp->patt = NULL;
29599a2dd95SBruce Richardson }
29699a2dd95SBruce Richardson
29799a2dd95SBruce Richardson /* Release directory structure */
29899a2dd95SBruce Richardson free(dirp);
29999a2dd95SBruce Richardson ok = /*success*/0;
30099a2dd95SBruce Richardson
30199a2dd95SBruce Richardson } else {
30299a2dd95SBruce Richardson /* Invalid directory stream */
30399a2dd95SBruce Richardson dirent_set_errno(EBADF);
30499a2dd95SBruce Richardson ok = /*failure*/-1;
30599a2dd95SBruce Richardson }
30699a2dd95SBruce Richardson return ok;
30799a2dd95SBruce Richardson }
30899a2dd95SBruce Richardson
30999a2dd95SBruce Richardson /* Get first directory entry (internal) */
31099a2dd95SBruce Richardson static WIN32_FIND_DATAW*
dirent_first(_WDIR * dirp)31199a2dd95SBruce Richardson dirent_first(_WDIR *dirp)
31299a2dd95SBruce Richardson {
31399a2dd95SBruce Richardson WIN32_FIND_DATAW *datap;
31499a2dd95SBruce Richardson
31599a2dd95SBruce Richardson /* Open directory and retrieve the first entry */
31699a2dd95SBruce Richardson dirp->handle = FindFirstFileExW(
31799a2dd95SBruce Richardson dirp->patt, FindExInfoStandard, &dirp->data,
31899a2dd95SBruce Richardson FindExSearchNameMatch, NULL, 0);
31999a2dd95SBruce Richardson if (dirp->handle != INVALID_HANDLE_VALUE) {
32099a2dd95SBruce Richardson
32199a2dd95SBruce Richardson /* a directory entry is now waiting in memory */
32299a2dd95SBruce Richardson datap = &dirp->data;
32399a2dd95SBruce Richardson dirp->cached = 1;
32499a2dd95SBruce Richardson
32599a2dd95SBruce Richardson } else {
32699a2dd95SBruce Richardson
32799a2dd95SBruce Richardson /* Failed to re-open directory: no directory entry in memory */
32899a2dd95SBruce Richardson dirp->cached = 0;
32999a2dd95SBruce Richardson datap = NULL;
33099a2dd95SBruce Richardson
33199a2dd95SBruce Richardson }
33299a2dd95SBruce Richardson return datap;
33399a2dd95SBruce Richardson }
33499a2dd95SBruce Richardson
33599a2dd95SBruce Richardson /* Get next directory entry (internal) */
33699a2dd95SBruce Richardson static WIN32_FIND_DATAW*
dirent_next(_WDIR * dirp)33799a2dd95SBruce Richardson dirent_next(_WDIR *dirp)
33899a2dd95SBruce Richardson {
33999a2dd95SBruce Richardson WIN32_FIND_DATAW *p;
34099a2dd95SBruce Richardson
34199a2dd95SBruce Richardson /* Get next directory entry */
34299a2dd95SBruce Richardson if (dirp->cached != 0) {
34399a2dd95SBruce Richardson
34499a2dd95SBruce Richardson /* A valid directory entry already in memory */
34599a2dd95SBruce Richardson p = &dirp->data;
34699a2dd95SBruce Richardson dirp->cached = 0;
34799a2dd95SBruce Richardson
34899a2dd95SBruce Richardson } else if (dirp->handle != INVALID_HANDLE_VALUE) {
34999a2dd95SBruce Richardson
35099a2dd95SBruce Richardson /* Get the next directory entry from stream */
35199a2dd95SBruce Richardson if (FindNextFileW(dirp->handle, &dirp->data) != FALSE) {
35299a2dd95SBruce Richardson /* Got a file */
35399a2dd95SBruce Richardson p = &dirp->data;
35499a2dd95SBruce Richardson } else {
35599a2dd95SBruce Richardson /* The very last entry has been processed
35699a2dd95SBruce Richardson *or an error occurred
35799a2dd95SBruce Richardson */
35899a2dd95SBruce Richardson FindClose(dirp->handle);
35999a2dd95SBruce Richardson dirp->handle = INVALID_HANDLE_VALUE;
36099a2dd95SBruce Richardson p = NULL;
36199a2dd95SBruce Richardson }
36299a2dd95SBruce Richardson
36399a2dd95SBruce Richardson } else {
36499a2dd95SBruce Richardson
36599a2dd95SBruce Richardson /* End of directory stream reached */
36699a2dd95SBruce Richardson p = NULL;
36799a2dd95SBruce Richardson
36899a2dd95SBruce Richardson }
36999a2dd95SBruce Richardson
37099a2dd95SBruce Richardson return p;
37199a2dd95SBruce Richardson }
37299a2dd95SBruce Richardson
37399a2dd95SBruce Richardson /*
37499a2dd95SBruce Richardson * Open directory stream using plain old C-string.
37599a2dd95SBruce Richardson */
37699a2dd95SBruce Richardson static DIR*
opendir(const char * dirname)37799a2dd95SBruce Richardson opendir(const char *dirname)
37899a2dd95SBruce Richardson {
37999a2dd95SBruce Richardson struct DIR *dirp;
38099a2dd95SBruce Richardson int error;
38199a2dd95SBruce Richardson
38299a2dd95SBruce Richardson /* Must have directory name */
38399a2dd95SBruce Richardson if (dirname == NULL || dirname[0] == '\0') {
38499a2dd95SBruce Richardson dirent_set_errno(ENOENT);
38599a2dd95SBruce Richardson return NULL;
38699a2dd95SBruce Richardson }
38799a2dd95SBruce Richardson
38899a2dd95SBruce Richardson /* Allocate memory for DIR structure */
38999a2dd95SBruce Richardson dirp = (DIR *)malloc(sizeof(struct DIR));
39099a2dd95SBruce Richardson if (dirp) {
39199a2dd95SBruce Richardson wchar_t wname[PATH_MAX];
39299a2dd95SBruce Richardson size_t n;
39399a2dd95SBruce Richardson
39499a2dd95SBruce Richardson /* Convert directory name to wide-character string */
39599a2dd95SBruce Richardson error = dirent_mbstowcs_s(&n, wname, PATH_MAX,
39699a2dd95SBruce Richardson dirname, PATH_MAX);
39799a2dd95SBruce Richardson if (!error) {
39899a2dd95SBruce Richardson
39999a2dd95SBruce Richardson /* Open directory stream using wide-character name */
40099a2dd95SBruce Richardson dirp->wdirp = _wopendir(wname);
40199a2dd95SBruce Richardson if (dirp->wdirp) {
40299a2dd95SBruce Richardson /* Directory stream opened */
40399a2dd95SBruce Richardson error = 0;
40499a2dd95SBruce Richardson } else {
40599a2dd95SBruce Richardson /* Failed to open directory stream */
40699a2dd95SBruce Richardson error = 1;
40799a2dd95SBruce Richardson }
40899a2dd95SBruce Richardson
40999a2dd95SBruce Richardson } else {
41099a2dd95SBruce Richardson /*
41199a2dd95SBruce Richardson * Cannot convert file name to wide-character string.
41299a2dd95SBruce Richardson * This occurs if the string contains invalid multi-byte
41399a2dd95SBruce Richardson * sequences or the output buffer is too small to
41499a2dd95SBruce Richardson * contain the resulting string.
41599a2dd95SBruce Richardson */
41699a2dd95SBruce Richardson error = 1;
41799a2dd95SBruce Richardson }
41899a2dd95SBruce Richardson
41999a2dd95SBruce Richardson } else {
42099a2dd95SBruce Richardson /* Cannot allocate DIR structure */
42199a2dd95SBruce Richardson error = 1;
42299a2dd95SBruce Richardson }
42399a2dd95SBruce Richardson
42499a2dd95SBruce Richardson /* Clean up in case of error */
42599a2dd95SBruce Richardson if (error && dirp) {
42699a2dd95SBruce Richardson free(dirp);
42799a2dd95SBruce Richardson dirp = NULL;
42899a2dd95SBruce Richardson }
42999a2dd95SBruce Richardson
43099a2dd95SBruce Richardson return dirp;
43199a2dd95SBruce Richardson }
43299a2dd95SBruce Richardson
43399a2dd95SBruce Richardson /*
43499a2dd95SBruce Richardson * Read next directory entry.
43599a2dd95SBruce Richardson *
43699a2dd95SBruce Richardson * When working with text consoles, please note that file names
43799a2dd95SBruce Richardson * returned by readdir() are represented in the default ANSI code
43899a2dd95SBruce Richardson * page while any output toconsole is typically formatted on another
43999a2dd95SBruce Richardson * code page. Thus, non-ASCII characters in file names will not usually
44099a2dd95SBruce Richardson * display correctly on console. The problem can be fixed in two ways:
44199a2dd95SBruce Richardson * (1) change the character set of console to 1252 using chcp utility
44299a2dd95SBruce Richardson * and use Lucida Console font, or (2) use _cprintf function when
443*7be78d02SJosh Soref * writing to console. The _cprintf() will re-encode ANSI strings to the
44499a2dd95SBruce Richardson * console code page so many non-ASCII characters will display correctly.
44599a2dd95SBruce Richardson */
44699a2dd95SBruce Richardson static struct dirent*
readdir(DIR * dirp)44799a2dd95SBruce Richardson readdir(DIR *dirp)
44899a2dd95SBruce Richardson {
44999a2dd95SBruce Richardson WIN32_FIND_DATAW *datap;
45099a2dd95SBruce Richardson struct dirent *entp;
45199a2dd95SBruce Richardson
45299a2dd95SBruce Richardson /* Read next directory entry */
45399a2dd95SBruce Richardson datap = dirent_next(dirp->wdirp);
45499a2dd95SBruce Richardson if (datap) {
45599a2dd95SBruce Richardson size_t n;
45699a2dd95SBruce Richardson int error;
45799a2dd95SBruce Richardson
45899a2dd95SBruce Richardson /* Attempt to convert file name to multi-byte string */
45999a2dd95SBruce Richardson error = dirent_wcstombs_s(&n, dirp->ent.d_name,
46099a2dd95SBruce Richardson PATH_MAX, datap->cFileName, PATH_MAX);
46199a2dd95SBruce Richardson
46299a2dd95SBruce Richardson /*
46399a2dd95SBruce Richardson * If the file name cannot be represented by a multi-byte
46499a2dd95SBruce Richardson * string, then attempt to use old 8+3 file name.
46599a2dd95SBruce Richardson * This allows traditional Unix-code to access some file
46699a2dd95SBruce Richardson * names despite of unicode characters, although file names
46799a2dd95SBruce Richardson * may seem unfamiliar to the user.
46899a2dd95SBruce Richardson *
46999a2dd95SBruce Richardson * Be ware that the code below cannot come up with a short
47099a2dd95SBruce Richardson * file name unless the file system provides one. At least
47199a2dd95SBruce Richardson * VirtualBox shared folders fail to do this.
47299a2dd95SBruce Richardson */
47399a2dd95SBruce Richardson if (error && datap->cAlternateFileName[0] != '\0') {
47499a2dd95SBruce Richardson error = dirent_wcstombs_s(
47599a2dd95SBruce Richardson &n, dirp->ent.d_name, PATH_MAX,
47699a2dd95SBruce Richardson datap->cAlternateFileName, PATH_MAX);
47799a2dd95SBruce Richardson }
47899a2dd95SBruce Richardson
47999a2dd95SBruce Richardson if (!error) {
48099a2dd95SBruce Richardson DWORD attr;
48199a2dd95SBruce Richardson
48299a2dd95SBruce Richardson /* Initialize directory entry for return */
48399a2dd95SBruce Richardson entp = &dirp->ent;
48499a2dd95SBruce Richardson
48599a2dd95SBruce Richardson /* Length of file name excluding zero terminator */
48699a2dd95SBruce Richardson entp->d_namlen = n - 1;
48799a2dd95SBruce Richardson
48899a2dd95SBruce Richardson /* File attributes */
48999a2dd95SBruce Richardson attr = datap->dwFileAttributes;
49099a2dd95SBruce Richardson if ((attr & FILE_ATTRIBUTE_DEVICE) != 0)
49199a2dd95SBruce Richardson entp->d_type = DT_CHR;
49299a2dd95SBruce Richardson else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0)
49399a2dd95SBruce Richardson entp->d_type = DT_DIR;
49499a2dd95SBruce Richardson else
49599a2dd95SBruce Richardson entp->d_type = DT_REG;
49699a2dd95SBruce Richardson
49799a2dd95SBruce Richardson /* Reset dummy fields */
49899a2dd95SBruce Richardson entp->d_ino = 0;
49999a2dd95SBruce Richardson entp->d_reclen = sizeof(struct dirent);
50099a2dd95SBruce Richardson
50199a2dd95SBruce Richardson } else {
50299a2dd95SBruce Richardson /*
50399a2dd95SBruce Richardson * Cannot convert file name to multi-byte string so
50499a2dd95SBruce Richardson * construct an erroneous directory entry and return
50599a2dd95SBruce Richardson * that. Note that we cannot return NULL as that would
50699a2dd95SBruce Richardson * stop the processing of directory entries completely.
50799a2dd95SBruce Richardson */
50899a2dd95SBruce Richardson entp = &dirp->ent;
50999a2dd95SBruce Richardson entp->d_name[0] = '?';
51099a2dd95SBruce Richardson entp->d_name[1] = '\0';
51199a2dd95SBruce Richardson entp->d_namlen = 1;
51299a2dd95SBruce Richardson entp->d_type = DT_UNKNOWN;
51399a2dd95SBruce Richardson entp->d_ino = 0;
51499a2dd95SBruce Richardson entp->d_reclen = 0;
51599a2dd95SBruce Richardson }
51699a2dd95SBruce Richardson
51799a2dd95SBruce Richardson } else {
51899a2dd95SBruce Richardson /* No more directory entries */
51999a2dd95SBruce Richardson entp = NULL;
52099a2dd95SBruce Richardson }
52199a2dd95SBruce Richardson
52299a2dd95SBruce Richardson return entp;
52399a2dd95SBruce Richardson }
52499a2dd95SBruce Richardson
52599a2dd95SBruce Richardson /*
52699a2dd95SBruce Richardson * Close directory stream.
52799a2dd95SBruce Richardson */
52899a2dd95SBruce Richardson static int
closedir(DIR * dirp)52999a2dd95SBruce Richardson closedir(DIR *dirp)
53099a2dd95SBruce Richardson {
53199a2dd95SBruce Richardson int ok;
53299a2dd95SBruce Richardson if (dirp) {
53399a2dd95SBruce Richardson
53499a2dd95SBruce Richardson /* Close wide-character directory stream */
53599a2dd95SBruce Richardson ok = _wclosedir(dirp->wdirp);
53699a2dd95SBruce Richardson dirp->wdirp = NULL;
53799a2dd95SBruce Richardson
53899a2dd95SBruce Richardson /* Release multi-byte character version */
53999a2dd95SBruce Richardson free(dirp);
54099a2dd95SBruce Richardson
54199a2dd95SBruce Richardson } else {
54299a2dd95SBruce Richardson
54399a2dd95SBruce Richardson /* Invalid directory stream */
54499a2dd95SBruce Richardson dirent_set_errno(EBADF);
54599a2dd95SBruce Richardson ok = /*failure*/-1;
54699a2dd95SBruce Richardson
54799a2dd95SBruce Richardson }
54899a2dd95SBruce Richardson return ok;
54999a2dd95SBruce Richardson }
55099a2dd95SBruce Richardson
55199a2dd95SBruce Richardson /* Convert multi-byte string to wide character string */
55299a2dd95SBruce Richardson static int
dirent_mbstowcs_s(size_t * pReturnValue,wchar_t * wcstr,size_t sizeInWords,const char * mbstr,size_t count)55399a2dd95SBruce Richardson dirent_mbstowcs_s(
55499a2dd95SBruce Richardson size_t *pReturnValue,
55599a2dd95SBruce Richardson wchar_t *wcstr,
55699a2dd95SBruce Richardson size_t sizeInWords,
55799a2dd95SBruce Richardson const char *mbstr,
55899a2dd95SBruce Richardson size_t count)
55999a2dd95SBruce Richardson {
56099a2dd95SBruce Richardson int error;
56199a2dd95SBruce Richardson
56299a2dd95SBruce Richardson #if defined(_MSC_VER) && _MSC_VER >= 1400
56399a2dd95SBruce Richardson /* Microsoft Visual Studio 2005 or later */
56499a2dd95SBruce Richardson error = mbstowcs_s(pReturnValue, wcstr,
56599a2dd95SBruce Richardson sizeInWords, mbstr, count);
56699a2dd95SBruce Richardson #else
56799a2dd95SBruce Richardson
56899a2dd95SBruce Richardson /* Older Visual Studio or non-Microsoft compiler */
56999a2dd95SBruce Richardson size_t n;
57099a2dd95SBruce Richardson
57199a2dd95SBruce Richardson /* Convert to wide-character string (or count characters) */
57299a2dd95SBruce Richardson n = mbstowcs(wcstr, mbstr, sizeInWords);
57399a2dd95SBruce Richardson if (!wcstr || n < count) {
57499a2dd95SBruce Richardson
57599a2dd95SBruce Richardson /* Zero-terminate output buffer */
57699a2dd95SBruce Richardson if (wcstr && sizeInWords) {
57799a2dd95SBruce Richardson if (n >= sizeInWords)
57899a2dd95SBruce Richardson n = sizeInWords - 1;
57999a2dd95SBruce Richardson wcstr[n] = 0;
58099a2dd95SBruce Richardson }
58199a2dd95SBruce Richardson
582*7be78d02SJosh Soref /* Length of resulting multi-byte string WITH zero
58399a2dd95SBruce Richardson *terminator
58499a2dd95SBruce Richardson */
58599a2dd95SBruce Richardson if (pReturnValue)
58699a2dd95SBruce Richardson *pReturnValue = n + 1;
58799a2dd95SBruce Richardson
58899a2dd95SBruce Richardson /* Success */
58999a2dd95SBruce Richardson error = 0;
59099a2dd95SBruce Richardson
59199a2dd95SBruce Richardson } else {
59299a2dd95SBruce Richardson
59399a2dd95SBruce Richardson /* Could not convert string */
59499a2dd95SBruce Richardson error = 1;
59599a2dd95SBruce Richardson
59699a2dd95SBruce Richardson }
59799a2dd95SBruce Richardson #endif
59899a2dd95SBruce Richardson
59999a2dd95SBruce Richardson return error;
60099a2dd95SBruce Richardson }
60199a2dd95SBruce Richardson
60299a2dd95SBruce Richardson /* Convert wide-character string to multi-byte string */
60399a2dd95SBruce Richardson static int
dirent_wcstombs_s(size_t * pReturnValue,char * mbstr,size_t sizeInBytes,const wchar_t * wcstr,size_t count)60499a2dd95SBruce Richardson dirent_wcstombs_s(
60599a2dd95SBruce Richardson size_t *pReturnValue,
60699a2dd95SBruce Richardson char *mbstr,
60799a2dd95SBruce Richardson size_t sizeInBytes, /* max size of mbstr */
60899a2dd95SBruce Richardson const wchar_t *wcstr,
60999a2dd95SBruce Richardson size_t count)
61099a2dd95SBruce Richardson {
61199a2dd95SBruce Richardson int error;
61299a2dd95SBruce Richardson
61399a2dd95SBruce Richardson #if defined(_MSC_VER) && _MSC_VER >= 1400
61499a2dd95SBruce Richardson /* Microsoft Visual Studio 2005 or later */
61599a2dd95SBruce Richardson error = wcstombs_s(pReturnValue, mbstr, sizeInBytes, wcstr, count);
61699a2dd95SBruce Richardson #else
61799a2dd95SBruce Richardson /* Older Visual Studio or non-Microsoft compiler */
61899a2dd95SBruce Richardson size_t n;
61999a2dd95SBruce Richardson
62099a2dd95SBruce Richardson /* Convert to multi-byte string
62199a2dd95SBruce Richardson * (or count the number of bytes needed)
62299a2dd95SBruce Richardson */
62399a2dd95SBruce Richardson n = wcstombs(mbstr, wcstr, sizeInBytes);
62499a2dd95SBruce Richardson if (!mbstr || n < count) {
62599a2dd95SBruce Richardson /* Zero-terminate output buffer */
62699a2dd95SBruce Richardson if (mbstr && sizeInBytes) {
62799a2dd95SBruce Richardson if (n >= sizeInBytes)
62899a2dd95SBruce Richardson n = sizeInBytes - 1;
62999a2dd95SBruce Richardson mbstr[n] = '\0';
63099a2dd95SBruce Richardson }
63199a2dd95SBruce Richardson /* Length of resulting multi-bytes string WITH
63299a2dd95SBruce Richardson *zero-terminator
63399a2dd95SBruce Richardson */
63499a2dd95SBruce Richardson if (pReturnValue)
63599a2dd95SBruce Richardson *pReturnValue = n + 1;
63699a2dd95SBruce Richardson /* Success */
63799a2dd95SBruce Richardson error = 0;
63899a2dd95SBruce Richardson } else {
63999a2dd95SBruce Richardson /* Cannot convert string */
64099a2dd95SBruce Richardson error = 1;
64199a2dd95SBruce Richardson }
64299a2dd95SBruce Richardson #endif
64399a2dd95SBruce Richardson
64499a2dd95SBruce Richardson return error;
64599a2dd95SBruce Richardson }
64699a2dd95SBruce Richardson
64799a2dd95SBruce Richardson /* Set errno variable */
64899a2dd95SBruce Richardson static void
dirent_set_errno(int error)64999a2dd95SBruce Richardson dirent_set_errno(int error)
65099a2dd95SBruce Richardson {
65199a2dd95SBruce Richardson #if defined(_MSC_VER) && _MSC_VER >= 1400
65299a2dd95SBruce Richardson /* Microsoft Visual Studio 2005 and later */
65399a2dd95SBruce Richardson _set_errno(error);
65499a2dd95SBruce Richardson #else
65599a2dd95SBruce Richardson
65699a2dd95SBruce Richardson /* Non-Microsoft compiler or older Microsoft compiler */
65799a2dd95SBruce Richardson errno = error;
65899a2dd95SBruce Richardson #endif
65999a2dd95SBruce Richardson }
66099a2dd95SBruce Richardson
66199a2dd95SBruce Richardson #endif /*DIRENT_H*/
662