<commit_before>#ifndef foowincompatfoo
#define foowincompatfoo

// This file and its companion wincompat.c provide some Posix interfaces to
// Windows APIs so the rest of the code can keep using them.


// require at least Windows Vista
#undef WINVER
#undef _WIN32_WINNT
#define WINVER 0x0600
#define _WIN32_WINNT WINVER

#include <winsock2.h>
#include <ws2tcpip.h>
#include <mswsock.h>
#include <sys/types.h>


// wrappers around WSAStartup/WSACleanup to avoid clutter
void winsock_init(void);
void winsock_exit(void);


// the equivalent of strerror(errno) for Windows sockets
char *errnostrsocket(void);


// Winsock doesn't have recvmsg/sendmsg but offers the same functionality
// with WSARecvMsg/WSASendMsg, so we implement the former in terms of the
// latter.

struct iovec {                   /* Scatter/gather array items */
   void  *iov_base;              /* Starting address */
   size_t iov_len;               /* Number of bytes to transfer */
};

struct msghdr {
   void         *msg_name;       /* optional address */
   socklen_t     msg_namelen;    /* size of address */
   struct iovec *msg_iov;        /* scatter/gather array */
   size_t        msg_iovlen;     /* # elements in msg_iov */
   void         *msg_control;    /* ancillary data, see below */
   size_t        msg_controllen; /* ancillary data buffer len */
   int           msg_flags;      /* flags on received message */
};

// MSDN says this struct is called wsacmsghdr but MingW uses _WSACMSGHDR.
// TODO: Verify what it is on actual Windows.
// cf. http://msdn.microsoft.com/en-us/library/ms741645(v=vs.85).aspx
// -->
// MingW32 x86 4.8.1 uses wsacmsghdr, MingW x86_x64 uses _WSACMSGHDR and Visual Studio 2015 RC (ws2def.h) defines:
// #if(_WIN32_WINNT >= 0x0600)
// #define _WSACMSGHDR cmsghdr
// #endif //(_WIN32_WINNT>=0x0600)
// typedef struct _WSACMSGHDR {
//     SIZE_T      cmsg_len;
//     INT         cmsg_level;
//     INT         cmsg_type;
//     /* followed by UCHAR cmsg_data[] */
// } WSACMSGHDR, *PWSACMSGHDR, FAR *LPWSACMSGHDR;
#ifdef __MINGW32__
  #ifdef __MINGW64_VERSION_MAJOR
    #define cmsghdr _WSACMSGHDR     // as in 'struct cmsghdr'
  #else
    #define cmsghdr wsacmsghdr      // as in 'struct cmsghdr'
  #endif
#elif (_WIN32_WINNT < 0x0600)
  #define cmsghdr _WSACMSGHDR
#endif

// VS2015 ws2def.h already defines: #define CMSG_FIRSTHDR WSA_CMSG_FIRSTHDR
#ifdef CMSG_FIRSTHDR
#undef CMSG_FIRSTHDR
#endif
static inline struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *m) {
    WSAMSG wm;
    wm.Control.len = m->msg_controllen;
    wm.Control.buf = (char*)m->msg_control;
    return WSA_CMSG_FIRSTHDR(&wm);
}

// VS2015 ws2def.h already defines: #define CMSG_NXTHDR WSA_CMSG_NXTHDR
#ifdef CMSG_NXTHDR
#undef CMSG_NXTHDR
#endif
static inline struct cmsghdr *CMSG_NXTHDR(struct msghdr *m, struct cmsghdr *c) {
    WSAMSG wm;
    wm.Control.len = m->msg_controllen;
    wm.Control.buf = (char*)m->msg_control;
    return WSA_CMSG_NXTHDR(&wm, c);
}

#ifndef CMSG_SPACE
 #define CMSG_SPACE(len) WSA_CMSG_SPACE(len)
#endif
#ifndef CMSG_LEN
 #define CMSG_LEN(len) WSA_CMSG_LEN(len)
#endif

// we're going to be naughty and redefine CMSG_DATA as an alias even though it
// is also a constant defined in wincrypt.h which we don't care about.
#undef CMSG_DATA
#define CMSG_DATA(c) WSA_CMSG_DATA(c)

#ifdef _MSC_VER
   // VS2012 and up has no ssize_t defined, before it was defined as unsigned int
   #ifndef _SSIZE_T
   #define _SSIZE_T
   typedef signed int        ssize_t;
   #endif
#endif

ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);

// ESHUTDOWN does not seem to exist on Windows, even though WSAESHUTDOWN does.
// MingW doesn't define it and MSDN doesn't list it, so we alias it to EBADF.
// cf. http://msdn.microsoft.com/en-us/library/5814770t.aspx
#ifndef ESHUTDOWN
#define ESHUTDOWN EBADF
#endif


// Windows doesn't have ioctl but offers ioctlsocket for some socket-related
// functions. Unfortunately, argument types differ, so we implement a
// (restricted) wrapper.
int ioctl(int d, unsigned long request, int *p);


// Windows lacks poll, but WSAPoll is good enough for us.
#define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout)

// Windows lacks pipe. It has an equivalent CreatePipe but we really need
// something to give to WSAPoll, so we fake it with a local TCP socket. (ugh)<commit_msg>fix msvc _WIN64 ssize_t definition<commit_after>#ifndef foowincompatfoo
#define foowincompatfoo

// This file and its companion wincompat.c provide some Posix interfaces to
// Windows APIs so the rest of the code can keep using them.


// require at least Windows Vista
#undef WINVER
#undef _WIN32_WINNT
#define WINVER 0x0600
#define _WIN32_WINNT WINVER

#include <winsock2.h>
#include <ws2tcpip.h>
#include <mswsock.h>
#include <sys/types.h>


// wrappers around WSAStartup/WSACleanup to avoid clutter
void winsock_init(void);
void winsock_exit(void);


// the equivalent of strerror(errno) for Windows sockets
char *errnostrsocket(void);


// Winsock doesn't have recvmsg/sendmsg but offers the same functionality
// with WSARecvMsg/WSASendMsg, so we implement the former in terms of the
// latter.

struct iovec {                   /* Scatter/gather array items */
   void  *iov_base;              /* Starting address */
   size_t iov_len;               /* Number of bytes to transfer */
};

struct msghdr {
   void         *msg_name;       /* optional address */
   socklen_t     msg_namelen;    /* size of address */
   struct iovec *msg_iov;        /* scatter/gather array */
   size_t        msg_iovlen;     /* # elements in msg_iov */
   void         *msg_control;    /* ancillary data, see below */
   size_t        msg_controllen; /* ancillary data buffer len */
   int           msg_flags;      /* flags on received message */
};

// MSDN says this struct is called wsacmsghdr but MingW uses _WSACMSGHDR.
// TODO: Verify what it is on actual Windows.
// cf. http://msdn.microsoft.com/en-us/library/ms741645(v=vs.85).aspx
// -->
// MingW32 x86 4.8.1 uses wsacmsghdr, MingW x86_x64 uses _WSACMSGHDR and Visual Studio 2015 RC (ws2def.h) defines:
// #if(_WIN32_WINNT >= 0x0600)
// #define _WSACMSGHDR cmsghdr
// #endif //(_WIN32_WINNT>=0x0600)
// typedef struct _WSACMSGHDR {
//     SIZE_T      cmsg_len;
//     INT         cmsg_level;
//     INT         cmsg_type;
//     /* followed by UCHAR cmsg_data[] */
// } WSACMSGHDR, *PWSACMSGHDR, FAR *LPWSACMSGHDR;
#ifdef __MINGW32__
  #ifdef __MINGW64_VERSION_MAJOR
    #define cmsghdr _WSACMSGHDR     // as in 'struct cmsghdr'
  #else
    #define cmsghdr wsacmsghdr      // as in 'struct cmsghdr'
  #endif
#elif (_WIN32_WINNT < 0x0600)
  #define cmsghdr _WSACMSGHDR
#endif

// VS2015 ws2def.h already defines: #define CMSG_FIRSTHDR WSA_CMSG_FIRSTHDR
#ifdef CMSG_FIRSTHDR
#undef CMSG_FIRSTHDR
#endif
static inline struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *m) {
    WSAMSG wm;
    wm.Control.len = m->msg_controllen;
    wm.Control.buf = (char*)m->msg_control;
    return WSA_CMSG_FIRSTHDR(&wm);
}

// VS2015 ws2def.h already defines: #define CMSG_NXTHDR WSA_CMSG_NXTHDR
#ifdef CMSG_NXTHDR
#undef CMSG_NXTHDR
#endif
static inline struct cmsghdr *CMSG_NXTHDR(struct msghdr *m, struct cmsghdr *c) {
    WSAMSG wm;
    wm.Control.len = m->msg_controllen;
    wm.Control.buf = (char*)m->msg_control;
    return WSA_CMSG_NXTHDR(&wm, c);
}

#ifndef CMSG_SPACE
 #define CMSG_SPACE(len) WSA_CMSG_SPACE(len)
#endif
#ifndef CMSG_LEN
 #define CMSG_LEN(len) WSA_CMSG_LEN(len)
#endif

// we're going to be naughty and redefine CMSG_DATA as an alias even though it
// is also a constant defined in wincrypt.h which we don't care about.
#undef CMSG_DATA
#define CMSG_DATA(c) WSA_CMSG_DATA(c)

#ifdef _MSC_VER
   // VS2012 and up has no ssize_t defined, before it was defined as unsigned int
  #ifndef _SSIZE_T_DEFINED
    #define _SSIZE_T_DEFINED
    #undef ssize_t
    #ifdef _WIN64
      typedef signed __int64  ssize_t;
    #else
      typedef signed int      ssize_t;
    #endif
  #endif
#endif

ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);

// ESHUTDOWN does not seem to exist on Windows, even though WSAESHUTDOWN does.
// MingW doesn't define it and MSDN doesn't list it, so we alias it to EBADF.
// cf. http://msdn.microsoft.com/en-us/library/5814770t.aspx
#ifndef ESHUTDOWN
#define ESHUTDOWN EBADF
#endif


// Windows doesn't have ioctl but offers ioctlsocket for some socket-related
// functions. Unfortunately, argument types differ, so we implement a
// (restricted) wrapper.
int ioctl(int d, unsigned long request, int *p);


// Windows lacks poll, but WSAPoll is good enough for us.
#define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout)

// Windows lacks pipe. It has an equivalent CreatePipe but we really need
// something to give to WSAPoll, so we fake it with a local TCP socket. (ugh)
{'fraction_non_alphanumeric': 0.09424025100851637, 'fraction_numerical': 0.01882563872702824, 'mean_word_length': 3.3157640232108316, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6863342', 'n_tokens_mistral': 3367, 'n_tokens_neox': 3142, 'n_words': 983}