build with mingw
[fw/stlink] / mingw / mingw.h
1 #ifdef __MINGW32__
2
3 #include <io.h>
4
5 #include <winsock.h>
6
7 #define ENOTCONN        WSAENOTCONN
8 #define EWOULDBLOCK     WSAEWOULDBLOCK
9 #define ENOBUFS         WSAENOBUFS
10 #define ECONNRESET      WSAECONNRESET
11 #define ESHUTDOWN       WSAESHUTDOWN
12 #define EAFNOSUPPORT    WSAEAFNOSUPPORT
13 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
14 #define EINPROGRESS     WSAEINPROGRESS
15 #define EISCONN         WSAEISCONN
16
17 /* winsock doesn't feature poll(), so there is a version implemented
18  * in terms of select() in mingw.c. The following definitions
19  * are copied from linux man pages. A poll() macro is defined to
20  * call the version in mingw.c.
21  */
22 #define POLLIN      0x0001    /* There is data to read */
23 #define POLLPRI     0x0002    /* There is urgent data to read */
24 #define POLLOUT     0x0004    /* Writing now will not block */
25 #define POLLERR     0x0008    /* Error condition */
26 #define POLLHUP     0x0010    /* Hung up */
27 #define POLLNVAL    0x0020    /* Invalid request: fd not open */
28 struct pollfd {
29     SOCKET fd;        /* file descriptor */
30     short events;     /* requested events */
31     short revents;    /* returned events */
32 };
33 #define poll(x, y, z)        win32_poll(x, y, z)
34
35 /* These wrappers do nothing special except set the global errno variable if
36  * an error occurs (winsock doesn't do this by default). They set errno
37  * to unix-like values (i.e. WSAEWOULDBLOCK is mapped to EAGAIN), so code
38  * outside of this file "shouldn't" have to worry about winsock specific error
39  * handling.
40  */
41 #define socket(x, y, z)      win32_socket(x, y, z)
42 #define connect(x, y, z)     win32_connect(x, y, z)
43 #define accept(x, y, z)      win32_accept(x, y, z)
44 #define shutdown(x, y)       win32_shutdown(x, y)
45
46 /* Winsock uses int instead of the usual socklen_t */
47 typedef int socklen_t;
48
49 int     win32_poll(struct pollfd *, unsigned int, int);
50 SOCKET  win32_socket(int, int, int);
51 int     win32_connect(SOCKET, struct sockaddr*, socklen_t);
52 SOCKET  win32_accept(SOCKET, struct sockaddr*, socklen_t *);
53 int     win32_shutdown(SOCKET, int);
54
55 #define strtok_r(x, y, z)      win32_strtok_r(x, y, z)
56 #define strsep(x,y) win32_strsep(x,y)
57
58 char *win32_strtok_r(char *s, const char *delim, char **lasts);
59 char *win32_strsep(char **stringp, const char *delim);
60
61 #endif