summaryrefslogtreecommitdiffstats
path: root/src/port/win32fseek.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:17:33 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:17:33 +0000
commit5e45211a64149b3c659b90ff2de6fa982a5a93ed (patch)
tree739caf8c461053357daa9f162bef34516c7bf452 /src/port/win32fseek.c
parentInitial commit. (diff)
downloadpostgresql-15-5e45211a64149b3c659b90ff2de6fa982a5a93ed.tar.xz
postgresql-15-5e45211a64149b3c659b90ff2de6fa982a5a93ed.zip
Adding upstream version 15.5.upstream/15.5
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/port/win32fseek.c')
-rw-r--r--src/port/win32fseek.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/port/win32fseek.c b/src/port/win32fseek.c
new file mode 100644
index 0000000..985313c
--- /dev/null
+++ b/src/port/win32fseek.c
@@ -0,0 +1,75 @@
+/*-------------------------------------------------------------------------
+ *
+ * win32fseek.c
+ * Replacements for fseeko() and ftello().
+ *
+ * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ * src/port/win32fseek.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#ifdef FRONTEND
+#include "postgres_fe.h"
+#else
+#include "postgres.h"
+#endif
+
+#if defined(WIN32) && defined(_MSC_VER)
+
+/*
+ * _pgfseeko64
+ *
+ * Calling fseek() on a handle to a non-seeking device such as a pipe or
+ * a communications device is not supported, and fseek() may not return
+ * an error. This wrapper relies on the file type to check which cases
+ * are supported.
+ */
+int
+_pgfseeko64(FILE *stream, pgoff_t offset, int origin)
+{
+ DWORD fileType;
+ HANDLE hFile = (HANDLE) _get_osfhandle(_fileno(stream));
+
+ fileType = pgwin32_get_file_type(hFile);
+ if (errno != 0)
+ return -1;
+
+ if (fileType == FILE_TYPE_DISK)
+ return _fseeki64(stream, offset, origin);
+ else if (fileType == FILE_TYPE_CHAR || fileType == FILE_TYPE_PIPE)
+ errno = ESPIPE;
+ else
+ errno = EINVAL;
+
+ return -1;
+}
+
+/*
+ * _pgftello64
+ *
+ * Same as _pgfseeko64().
+ */
+pgoff_t
+_pgftello64(FILE *stream)
+{
+ DWORD fileType;
+ HANDLE hFile = (HANDLE) _get_osfhandle(_fileno(stream));
+
+ fileType = pgwin32_get_file_type(hFile);
+ if (errno != 0)
+ return -1;
+
+ if (fileType == FILE_TYPE_DISK)
+ return _ftelli64(stream);
+ else if (fileType == FILE_TYPE_CHAR || fileType == FILE_TYPE_PIPE)
+ errno = ESPIPE;
+ else
+ errno = EINVAL;
+
+ return -1;
+}
+
+#endif /* defined(WIN32) && defined(_MSC_VER) */