aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/libpq/src/port/win32common.c
blob: 2fd78f7f9366400b6194d415ead1f497b886fe0d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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 */