Improve notation of BuiltinTrancheNames
[pgsql.git] / src / port / dirent.c
blob1f47ea65756c2f4b499ec9413d0ad8f5e4c1033a
1 /*-------------------------------------------------------------------------
3 * dirent.c
4 * opendir/readdir/closedir for win32/msvc
6 * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * IDENTIFICATION
11 * src/port/dirent.c
13 *-------------------------------------------------------------------------
16 #ifndef FRONTEND
17 #include "postgres.h"
18 #else
19 #include "postgres_fe.h"
20 #endif
22 #include <dirent.h>
25 struct DIR
27 char *dirname;
28 struct dirent ret; /* Used to return to caller */
29 HANDLE handle;
32 DIR *
33 opendir(const char *dirname)
35 DWORD attr;
36 DIR *d;
38 /* Make sure it is a directory */
39 attr = GetFileAttributes(dirname);
40 if (attr == INVALID_FILE_ATTRIBUTES)
42 errno = ENOENT;
43 return NULL;
45 if ((attr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY)
47 errno = ENOTDIR;
48 return NULL;
51 d = malloc(sizeof(DIR));
52 if (!d)
54 errno = ENOMEM;
55 return NULL;
57 d->dirname = malloc(strlen(dirname) + 4);
58 if (!d->dirname)
60 errno = ENOMEM;
61 free(d);
62 return NULL;
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 there */
68 strcat(d->dirname, "*"); /* Search for entries named anything */
69 d->handle = INVALID_HANDLE_VALUE;
70 d->ret.d_ino = 0; /* no inodes on win32 */
71 d->ret.d_reclen = 0; /* not used on win32 */
72 d->ret.d_type = DT_UNKNOWN;
74 return d;
77 struct dirent *
78 readdir(DIR *d)
80 WIN32_FIND_DATA fd;
82 if (d->handle == INVALID_HANDLE_VALUE)
84 d->handle = FindFirstFile(d->dirname, &fd);
85 if (d->handle == INVALID_HANDLE_VALUE)
87 /* If there are no files, force errno=0 (unlike mingw) */
88 if (GetLastError() == ERROR_FILE_NOT_FOUND)
89 errno = 0;
90 else
91 _dosmaperr(GetLastError());
92 return NULL;
95 else
97 if (!FindNextFile(d->handle, &fd))
99 /* If there are no more files, force errno=0 (like mingw) */
100 if (GetLastError() == ERROR_NO_MORE_FILES)
101 errno = 0;
102 else
103 _dosmaperr(GetLastError());
104 return NULL;
107 strcpy(d->ret.d_name, fd.cFileName); /* Both strings are MAX_PATH long */
108 d->ret.d_namlen = strlen(d->ret.d_name);
111 * For reparse points dwReserved0 field will contain the ReparseTag. We
112 * check this first, because reparse points are also reported as
113 * directories.
115 if ((fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0 &&
116 (fd.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT))
117 d->ret.d_type = DT_LNK;
118 else if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
119 d->ret.d_type = DT_DIR;
120 else
121 d->ret.d_type = DT_REG;
123 return &d->ret;
127 closedir(DIR *d)
129 int ret = 0;
131 if (d->handle != INVALID_HANDLE_VALUE)
132 ret = !FindClose(d->handle);
133 free(d->dirname);
134 free(d);
136 return ret;