Update MinGW so it handles fseeko() similar to Unix.
[PostgreSQL.git] / src / include / port / win32.h
blob256b71fbdc05c74758ab169e16596410ab44d80b
1 /* $PostgreSQL$ */
3 #if defined(_MSC_VER) || defined(__BORLANDC__)
4 #define WIN32_ONLY_COMPILER
5 #endif
7 #define _WIN32_WINNT 0x0500
8 /*
9 * Always build with SSPI support. Keep it as a #define in case
10 * we want a switch to disable it sometime in the future.
12 #ifndef __BORLANDC__
13 #define ENABLE_SSPI 1
14 #endif
16 /* undefine and redefine after #include */
17 #undef mkdir
19 #undef ERROR
20 #define _WINSOCKAPI_
21 #include <windows.h>
22 #include <winsock2.h>
23 #include <ws2tcpip.h>
24 #undef small
25 #include <process.h>
26 #include <signal.h>
27 #include <errno.h>
28 #include <direct.h>
29 #ifndef __BORLANDC__
30 #include <sys/utime.h> /* for non-unicode version */
31 #endif
32 #undef near
34 /* Must be here to avoid conflicting with prototype in windows.h */
35 #define mkdir(a,b) mkdir(a)
37 #define HAVE_FSYNC_WRITETHROUGH
38 #define HAVE_FSYNC_WRITETHROUGH_ONLY
39 #define ftruncate(a,b) chsize(a,b)
41 * Even though we don't support 'fsync' as a wal_sync_method,
42 * we do fsync() a few other places where _commit() is just fine.
44 #define fsync(fd) _commit(fd)
46 #define USES_WINSOCK
48 /* defines for dynamic linking on Win32 platform */
49 #if defined(WIN32) || defined(__CYGWIN__)
51 #if __GNUC__ && ! defined (__declspec)
52 #error You need egcs 1.1 or newer for compiling!
53 #endif
55 #ifdef BUILDING_DLL
56 #define PGDLLIMPORT __declspec (dllexport)
57 #else /* not BUILDING_DLL */
58 #define PGDLLIMPORT __declspec (dllimport)
59 #endif
60 #else /* not CYGWIN, not MSVC, not MingW */
62 #define PGDLLIMPORT
63 #endif
67 * IPC defines
69 #undef HAVE_UNION_SEMUN
70 #define HAVE_UNION_SEMUN 1
72 #define IPC_RMID 256
73 #define IPC_CREAT 512
74 #define IPC_EXCL 1024
75 #define IPC_PRIVATE 234564
76 #define IPC_NOWAIT 2048
77 #define IPC_STAT 4096
79 #define EACCESS 2048
80 #define EIDRM 4096
82 #define SETALL 8192
83 #define GETNCNT 16384
84 #define GETVAL 65536
85 #define SETVAL 131072
86 #define GETPID 262144
90 * Signal stuff
92 * For WIN32, there is no wait() call so there are no wait() macros
93 * to interpret the return value of system(). Instead, system()
94 * return values < 0x100 are used for exit() termination, and higher
95 * values are used to indicated non-exit() termination, which is
96 * similar to a unix-style signal exit (think SIGSEGV ==
97 * STATUS_ACCESS_VIOLATION). Return values are broken up into groups:
99 * http://msdn2.microsoft.com/en-gb/library/aa489609.aspx
101 * NT_SUCCESS 0 - 0x3FFFFFFF
102 * NT_INFORMATION 0x40000000 - 0x7FFFFFFF
103 * NT_WARNING 0x80000000 - 0xBFFFFFFF
104 * NT_ERROR 0xC0000000 - 0xFFFFFFFF
106 * Effectively, we don't care on the severity of the return value from
107 * system(), we just need to know if it was because of exit() or generated
108 * by the system, and it seems values >= 0x100 are system-generated.
109 * See this URL for a list of WIN32 STATUS_* values:
111 * Wine (URL used in our error messages) -
112 * http://source.winehq.org/source/include/ntstatus.h
113 * Descriptions - http://www.comp.nus.edu.sg/~wuyongzh/my_doc/ntstatus.txt
114 * MS SDK - http://www.nologs.com/ntstatus.html
116 * It seems the exception lists are in both ntstatus.h and winnt.h, but
117 * ntstatus.h has a more comprehensive list, and it only contains
118 * exception values, rather than winnt, which contains lots of other
119 * things:
121 * http://www.microsoft.com/msj/0197/exception/exception.aspx
123 * The ExceptionCode parameter is the number that the operating system
124 * assigned to the exception. You can see a list of various exception codes
125 * in WINNT.H by searching for #defines that start with "STATUS_". For
126 * example, the code for the all-too-familiar STATUS_ACCESS_VIOLATION is
127 * 0xC0000005. A more complete set of exception codes can be found in
128 * NTSTATUS.H from the Windows NT DDK.
130 * Some day we might want to print descriptions for the most common
131 * exceptions, rather than printing an include file name. We could use
132 * RtlNtStatusToDosError() and pass to FormatMessage(), which can print
133 * the text of error values, but MinGW does not support
134 * RtlNtStatusToDosError().
136 #define WIFEXITED(w) (((w) & 0XFFFFFF00) == 0)
137 #define WIFSIGNALED(w) (!WIFEXITED(w))
138 #define WEXITSTATUS(w) (w)
139 #define WTERMSIG(w) (w)
141 #define sigmask(sig) ( 1 << ((sig)-1) )
143 /* Signal function return values */
144 #undef SIG_DFL
145 #undef SIG_ERR
146 #undef SIG_IGN
147 #define SIG_DFL ((pqsigfunc)0)
148 #define SIG_ERR ((pqsigfunc)-1)
149 #define SIG_IGN ((pqsigfunc)1)
151 /* Some extra signals */
152 #define SIGHUP 1
153 #define SIGQUIT 3
154 #define SIGTRAP 5
155 #define SIGABRT 22 /* Set to match W32 value -- not UNIX value */
156 #define SIGKILL 9
157 #define SIGPIPE 13
158 #define SIGALRM 14
159 #define SIGSTOP 17
160 #define SIGTSTP 18
161 #define SIGCONT 19
162 #define SIGCHLD 20
163 #define SIGTTIN 21
164 #define SIGTTOU 22 /* Same as SIGABRT -- no problem, I hope */
165 #define SIGWINCH 28
166 #ifndef __BORLANDC__
167 #define SIGUSR1 30
168 #define SIGUSR2 31
169 #endif
172 * New versions of mingw have gettimeofday() and also declare
173 * struct timezone to support it.
175 #ifndef HAVE_GETTIMEOFDAY
176 struct timezone
178 int tz_minuteswest; /* Minutes west of GMT. */
179 int tz_dsttime; /* Nonzero if DST is ever in effect. */
181 #endif
183 /* for setitimer in backend/port/win32/timer.c */
184 #define ITIMER_REAL 0
185 struct itimerval
187 struct timeval it_interval;
188 struct timeval it_value;
191 int setitimer(int which, const struct itimerval * value, struct itimerval * ovalue);
194 * WIN32 does not provide 64-bit off_t, but does provide the functions operating
195 * with 64-bit offsets.
197 #define pgoff_t __int64
198 #ifdef WIN32_ONLY_COMPILER
199 #define fseeko(stream, offset, origin) _fseeki64(stream, offset, origin)
200 #define ftello(stream) _ftelli64(stream)
201 #else
202 #define fseeko(stream, offset, origin) fseeko64(stream, offset, origin)
203 #define ftello(stream) ftello64(stream)
204 #endif
207 * Supplement to <sys/types.h>.
209 * Perl already has typedefs for uid_t and gid_t.
211 #ifndef PLPERL_HAVE_UID_GID
212 typedef int uid_t;
213 typedef int gid_t;
214 #endif
215 typedef long key_t;
217 #ifdef WIN32_ONLY_COMPILER
218 typedef int pid_t;
219 #endif
222 * Supplement to <sys/stat.h>.
224 #define lstat(path, sb) stat((path), (sb))
227 * Supplement to <fcntl.h>.
228 * This is the same value as _O_NOINHERIT in the MS header file. This is
229 * to ensure that we don't collide with a future definition. It means
230 * we cannot use _O_NOINHERIT ourselves.
232 #define O_DSYNC 0x0080
235 * Supplement to <errno.h>.
237 #undef EAGAIN
238 #undef EINTR
239 #define EINTR WSAEINTR
240 #define EAGAIN WSAEWOULDBLOCK
241 #define EMSGSIZE WSAEMSGSIZE
242 #define EAFNOSUPPORT WSAEAFNOSUPPORT
243 #define EWOULDBLOCK WSAEWOULDBLOCK
244 #define ECONNRESET WSAECONNRESET
245 #define EINPROGRESS WSAEINPROGRESS
246 #define ENOBUFS WSAENOBUFS
247 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
248 #define ECONNREFUSED WSAECONNREFUSED
249 #define EBADFD WSAENOTSOCK
250 #define EOPNOTSUPP WSAEOPNOTSUPP
253 /* In backend/port/win32/signal.c */
254 extern PGDLLIMPORT volatile int pg_signal_queue;
255 extern PGDLLIMPORT int pg_signal_mask;
256 extern HANDLE pgwin32_signal_event;
257 extern HANDLE pgwin32_initial_signal_pipe;
259 #define UNBLOCKED_SIGNAL_QUEUE() (pg_signal_queue & ~pg_signal_mask)
262 void pgwin32_signal_initialize(void);
263 HANDLE pgwin32_create_signal_listener(pid_t pid);
264 void pgwin32_dispatch_queued_signals(void);
265 void pg_queue_signal(int signum);
267 /* In backend/port/win32/socket.c */
268 #ifndef FRONTEND
269 #define socket(af, type, protocol) pgwin32_socket(af, type, protocol)
270 #define accept(s, addr, addrlen) pgwin32_accept(s, addr, addrlen)
271 #define connect(s, name, namelen) pgwin32_connect(s, name, namelen)
272 #define select(n, r, w, e, timeout) pgwin32_select(n, r, w, e, timeout)
273 #define recv(s, buf, len, flags) pgwin32_recv(s, buf, len, flags)
274 #define send(s, buf, len, flags) pgwin32_send(s, buf, len, flags)
276 SOCKET pgwin32_socket(int af, int type, int protocol);
277 SOCKET pgwin32_accept(SOCKET s, struct sockaddr * addr, int *addrlen);
278 int pgwin32_connect(SOCKET s, const struct sockaddr * name, int namelen);
279 int pgwin32_select(int nfds, fd_set *readfs, fd_set *writefds, fd_set *exceptfds, const struct timeval * timeout);
280 int pgwin32_recv(SOCKET s, char *buf, int len, int flags);
281 int pgwin32_send(SOCKET s, char *buf, int len, int flags);
283 const char *pgwin32_socket_strerror(int err);
284 int pgwin32_waitforsinglesocket(SOCKET s, int what, int timeout);
286 /* in backend/port/win32/security.c */
287 extern int pgwin32_is_admin(void);
288 extern int pgwin32_is_service(void);
289 #endif
291 /* in port/win32error.c */
292 extern void _dosmaperr(unsigned long);
295 /* Things that exist in MingW headers, but need to be added to MSVC */
296 #ifdef WIN32_ONLY_COMPILER
297 typedef long ssize_t;
298 #ifndef __BORLANDC__
299 typedef unsigned short mode_t;
300 #endif
303 * Certain "standard edition" versions of MSVC throw a warning
304 * that later generates an error for "inline" statements, but
305 * __inline seems to work. e.g. Microsoft Visual C++ .NET
306 * Version 7.1.3088
308 #define inline __inline
309 #define __inline__ __inline
311 #ifndef __BORLANDC__
312 #define _S_IRWXU (_S_IREAD | _S_IWRITE | _S_IEXEC)
313 #define _S_IXUSR _S_IEXEC
314 #define _S_IWUSR _S_IWRITE
315 #define _S_IRUSR _S_IREAD
316 #define S_IRUSR _S_IRUSR
317 #define S_IWUSR _S_IWUSR
318 #define S_IXUSR _S_IXUSR
319 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
320 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
321 #endif
323 #define F_OK 0
324 #define W_OK 2
325 #define R_OK 4
327 #define isinf(x) ((_fpclass(x) == _FPCLASS_PINF) || (_fpclass(x) == _FPCLASS_NINF))
328 #define isnan(x) _isnan(x)
330 /* Pulled from Makefile.port in mingw */
331 #define DLSUFFIX ".dll"
333 #endif