PostgreSQL Source Code  git master
win32fseek.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * win32fseek.c
4  * Replacements for fseeko() and ftello().
5  *
6  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
7  *
8  * IDENTIFICATION
9  * src/port/win32fseek.c
10  *
11  *-------------------------------------------------------------------------
12  */
13 
14 #ifdef FRONTEND
15 #include "postgres_fe.h"
16 #else
17 #include "postgres.h"
18 #endif
19 
20 #ifdef _MSC_VER
21 
22 /*
23  * _pgfseeko64
24  *
25  * Calling fseek() on a handle to a non-seeking device such as a pipe or
26  * a communications device is not supported, and fseek() may not return
27  * an error. This wrapper relies on the file type to check which cases
28  * are supported.
29  */
30 int
31 _pgfseeko64(FILE *stream, pgoff_t offset, int origin)
32 {
33  DWORD fileType;
34  HANDLE hFile = (HANDLE) _get_osfhandle(_fileno(stream));
35 
36  fileType = pgwin32_get_file_type(hFile);
37  if (errno != 0)
38  return -1;
39 
40  if (fileType == FILE_TYPE_DISK)
41  return _fseeki64(stream, offset, origin);
42  else if (fileType == FILE_TYPE_CHAR || fileType == FILE_TYPE_PIPE)
43  errno = ESPIPE;
44  else
45  errno = EINVAL;
46 
47  return -1;
48 }
49 
50 /*
51  * _pgftello64
52  *
53  * Same as _pgfseeko64().
54  */
55 pgoff_t
56 _pgftello64(FILE *stream)
57 {
58  DWORD fileType;
59  HANDLE hFile = (HANDLE) _get_osfhandle(_fileno(stream));
60 
61  fileType = pgwin32_get_file_type(hFile);
62  if (errno != 0)
63  return -1;
64 
65  if (fileType == FILE_TYPE_DISK)
66  return _ftelli64(stream);
67  else if (fileType == FILE_TYPE_CHAR || fileType == FILE_TYPE_PIPE)
68  errno = ESPIPE;
69  else
70  errno = EINVAL;
71 
72  return -1;
73 }
74 
75 #endif /* _MSC_VER */
DWORD pgwin32_get_file_type(HANDLE hFile)
Definition: win32common.c:31
#define pgoff_t
Definition: win32_port.h:207