1 /*-------------------------------------------------------------------------
4 * opendir/readdir/closedir for win32/msvc
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
13 *-------------------------------------------------------------------------
23 struct dirent ret
; /* Used to return to caller */
28 opendir(const char *dirname
)
33 /* Make sure it is a directory */
34 attr
= GetFileAttributes(dirname
);
35 if (attr
== INVALID_FILE_ATTRIBUTES
)
40 if ((attr
& FILE_ATTRIBUTE_DIRECTORY
) != FILE_ATTRIBUTE_DIRECTORY
)
46 d
= malloc(sizeof(DIR));
52 d
->dirname
= malloc(strlen(dirname
) + 4);
59 strcpy(d
->dirname
, dirname
);
60 if (d
->dirname
[strlen(d
->dirname
) - 1] != '/' &&
61 d
->dirname
[strlen(d
->dirname
) - 1] != '\\')
62 strcat(d
->dirname
, "\\"); /* Append backslash if not already
64 strcat(d
->dirname
, "*"); /* Search for entries named anything */
65 d
->handle
= INVALID_HANDLE_VALUE
;
66 d
->ret
.d_ino
= 0; /* no inodes on win32 */
67 d
->ret
.d_reclen
= 0; /* not used on win32 */
77 if (d
->handle
== INVALID_HANDLE_VALUE
)
79 d
->handle
= FindFirstFile(d
->dirname
, &fd
);
80 if (d
->handle
== INVALID_HANDLE_VALUE
)
88 if (!FindNextFile(d
->handle
, &fd
))
90 if (GetLastError() == ERROR_NO_MORE_FILES
)
92 /* No more files, force errno=0 (unlike mingw) */
96 _dosmaperr(GetLastError());
100 strcpy(d
->ret
.d_name
, fd
.cFileName
); /* Both strings are MAX_PATH
102 d
->ret
.d_namlen
= strlen(d
->ret
.d_name
);
109 if (d
->handle
!= INVALID_HANDLE_VALUE
)
110 FindClose(d
->handle
);