1 #include "../../git-compat-util.h"
4 struct dirent dd_dir; /* includes d_type */
5 HANDLE dd_handle; /* FindFirstFile handle */
6 int dd_stat; /* 0-based index */
9 static inline void finddata2dirent(struct dirent *ent, WIN32_FIND_DATAW *fdata)
11 /* convert UTF-16 name to UTF-8 */
12 xwcstoutf(ent->d_name, fdata->cFileName, sizeof(ent->d_name));
14 /* Set file type, based on WIN32_FIND_DATA */
15 if (fdata->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
21 DIR *opendir(const char *name)
23 wchar_t pattern[MAX_PATH + 2]; /* + 2 for '/' '*' */
24 WIN32_FIND_DATAW fdata;
29 /* convert name to UTF-16 and check length < MAX_PATH */
30 if ((len = xutftowcs_path(pattern, name)) < 0)
33 /* append optional '/' and wildcard '*' */
34 if (len && !is_dir_sep(pattern[len - 1]))
39 /* open find handle */
40 h = FindFirstFileW(pattern, &fdata);
41 if (h == INVALID_HANDLE_VALUE) {
42 DWORD err = GetLastError();
43 errno = (err == ERROR_DIRECTORY) ? ENOTDIR : err_win_to_posix(err);
47 /* initialize DIR structure and copy first dir entry */
48 dir = xmalloc(sizeof(DIR));
51 finddata2dirent(&dir->dd_dir, &fdata);
55 struct dirent *readdir(DIR *dir)
58 errno = EBADF; /* No set_errno for mingw */
62 /* if first entry, dirent has already been set up by opendir */
64 /* get next entry and convert from WIN32_FIND_DATA to dirent */
65 WIN32_FIND_DATAW fdata;
66 if (FindNextFileW(dir->dd_handle, &fdata)) {
67 finddata2dirent(&dir->dd_dir, &fdata);
69 DWORD lasterr = GetLastError();
70 /* POSIX says you shouldn't set errno when readdir can't
71 find any more files; so, if another error we leave it set. */
72 if (lasterr != ERROR_NO_MORE_FILES)
73 errno = err_win_to_posix(lasterr);
82 int closedir(DIR *dir)
89 FindClose(dir->dd_handle);