1 /*-------------------------------------------------------------------------
4 * opendir/readdir/closedir for win32/msvc
6 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/port/dirent.c,v 1.2 2006/06/26 12:58:17 momjian Exp $
13 *-------------------------------------------------------------------------
22 struct dirent ret; /* Used to return to caller */
26 DIR* opendir(const char *dirname)
31 /* Make sure it is a directory */
32 attr = GetFileAttributes(dirname);
33 if (attr == INVALID_FILE_ATTRIBUTES)
38 if ((attr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY)
44 d = malloc(sizeof(DIR));
50 d->dirname = malloc(strlen(dirname)+4);
57 strcpy(d->dirname, dirname);
58 if (d->dirname[strlen(d->dirname)-1] != '/' &&
59 d->dirname[strlen(d->dirname)-1] != '\\')
60 strcat(d->dirname,"\\"); /* Append backslash if not already there */
61 strcat(d->dirname,"*"); /* Search for entries named anything */
62 d->handle = INVALID_HANDLE_VALUE;
63 d->ret.d_ino = 0; /* no inodes on win32 */
64 d->ret.d_reclen = 0; /* not used on win32 */
69 struct dirent* readdir(DIR * d)
73 if (d->handle == INVALID_HANDLE_VALUE)
75 d->handle = FindFirstFile(d->dirname, &fd);
76 if (d->handle == INVALID_HANDLE_VALUE)
84 if (!FindNextFile(d->handle, &fd))
86 if (GetLastError() == ERROR_NO_MORE_FILES)
88 /* No more files, force errno=0 (unlike mingw) */
92 _dosmaperr(GetLastError());
96 strcpy(d->ret.d_name, fd.cFileName); /* Both strings are MAX_PATH long */
97 d->ret.d_namlen = strlen(d->ret.d_name);
103 if (d->handle != INVALID_HANDLE_VALUE)
104 FindClose(d->handle);