summaryrefslogtreecommitdiffstats
path: root/yql/essentials/parser/pg_wrapper/postgresql/src/port/win32common.c
diff options
context:
space:
mode:
authorvvvv <[email protected]>2024-11-07 12:29:36 +0300
committervvvv <[email protected]>2024-11-07 13:49:47 +0300
commitd4c258e9431675bab6745c8638df6e3dfd4dca6b (patch)
treeb5efcfa11351152a4c872fccaea35749141c0b11 /yql/essentials/parser/pg_wrapper/postgresql/src/port/win32common.c
parent13a4f274caef5cfdaf0263b24e4d6bdd5521472b (diff)
Moved other yql/essentials libs YQL-19206
init commit_hash:7d4c435602078407bbf20dd3c32f9c90d2bbcbc0
Diffstat (limited to 'yql/essentials/parser/pg_wrapper/postgresql/src/port/win32common.c')
-rw-r--r--yql/essentials/parser/pg_wrapper/postgresql/src/port/win32common.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/yql/essentials/parser/pg_wrapper/postgresql/src/port/win32common.c b/yql/essentials/parser/pg_wrapper/postgresql/src/port/win32common.c
new file mode 100644
index 00000000000..2fd78f7f936
--- /dev/null
+++ b/yql/essentials/parser/pg_wrapper/postgresql/src/port/win32common.c
@@ -0,0 +1,68 @@
+/*-------------------------------------------------------------------------
+ *
+ * win32common.c
+ * Common routines shared among the win32*.c ports.
+ *
+ * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ * src/port/win32common.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#ifdef FRONTEND
+#include "postgres_fe.h"
+#else
+#include "postgres.h"
+#endif
+
+#ifdef WIN32
+
+/*
+ * pgwin32_get_file_type
+ *
+ * Convenience wrapper for GetFileType() with specific error handling for all the
+ * port implementations. Returns the file type associated with a HANDLE.
+ *
+ * On error, sets errno with FILE_TYPE_UNKNOWN as file type.
+ */
+DWORD
+pgwin32_get_file_type(HANDLE hFile)
+{
+ DWORD fileType = FILE_TYPE_UNKNOWN;
+ DWORD lastError;
+
+ errno = 0;
+
+ /*
+ * When stdin, stdout, and stderr aren't associated with a stream the
+ * special value -2 is returned:
+ * https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle
+ */
+ if (hFile == INVALID_HANDLE_VALUE || hFile == (HANDLE) -2)
+ {
+ errno = EINVAL;
+ return FILE_TYPE_UNKNOWN;
+ }
+
+ fileType = GetFileType(hFile);
+ lastError = GetLastError();
+
+ /*
+ * Invoke GetLastError in order to distinguish between a "valid" return of
+ * FILE_TYPE_UNKNOWN and its return due to a calling error. In case of
+ * success, GetLastError() returns NO_ERROR.
+ */
+ if (fileType == FILE_TYPE_UNKNOWN && lastError != NO_ERROR)
+ {
+ _dosmaperr(lastError);
+ return FILE_TYPE_UNKNOWN;
+ }
+
+ return fileType;
+}
+
+#endif /* WIN32 */