1 /*-------------------------------------------------------------------------
4 * opendir/readdir/closedir for win32/msvc
6 * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
13 *-------------------------------------------------------------------------
19 #include "postgres_fe.h"
28 struct dirent ret; /* Used to return to caller */
33 opendir(const char *dirname)
38 /* Make sure it is a directory */
39 attr = GetFileAttributes(dirname);
40 if (attr == INVALID_FILE_ATTRIBUTES)
45 if ((attr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY)
51 d = malloc(sizeof(DIR));
57 d->dirname = malloc(strlen(dirname) + 4);
64 strcpy(d->dirname, dirname);
65 if (d->dirname[strlen(d->dirname) - 1] != '/' &&
66 d->dirname[strlen(d->dirname) - 1] != '\\')
67 strcat(d->dirname, "\\"); /* Append backslash if not already
69 strcat(d->dirname, "*"); /* Search for entries named anything */
70 d->handle = INVALID_HANDLE_VALUE;
71 d->ret.d_ino = 0; /* no inodes on win32 */
72 d->ret.d_reclen = 0; /* not used on win32 */
82 if (d->handle == INVALID_HANDLE_VALUE)
84 d->handle = FindFirstFile(d->dirname, &fd);
85 if (d->handle == INVALID_HANDLE_VALUE)
93 if (!FindNextFile(d->handle, &fd))
95 if (GetLastError() == ERROR_NO_MORE_FILES)
97 /* No more files, force errno=0 (unlike mingw) */
101 _dosmaperr(GetLastError());
105 strcpy(d->ret.d_name, fd.cFileName); /* Both strings are MAX_PATH
107 d->ret.d_namlen = strlen(d->ret.d_name);
117 if (d->handle != INVALID_HANDLE_VALUE)
118 ret = !FindClose(d->handle);