]> git.meshlink.io Git - catta/blobdiff - src/compat/windows/wincompat.c
moved back windows compat headers and fixed catta headers to stop clutter dependent...
[catta] / src / compat / windows / wincompat.c
index 4433b5b54648be93ff1fec50c7baa2674a74d11f..93a557e1e897a35c5663af87f0d988cae62abc1a 100644 (file)
@@ -4,6 +4,8 @@
 #include <assert.h>
 #include <stdint.h>
 
+#include <catta/log.h>
+
 // helper: convert WSAGetLastError() to an errno constant
 static int wsa_errno(void)
 {
@@ -29,6 +31,31 @@ static int wsa_errno(void)
     }
 }
 
+void winsock_init(void)
+{
+    WSADATA wsa;
+    int error;
+
+    if((error = WSAStartup(MAKEWORD(2,2), &wsa)) != 0)
+        catta_log_error("WSAStartup() failed: %d", error);
+}
+
+void winsock_exit(void)
+{
+    if(WSACleanup() == SOCKET_ERROR)
+        catta_log_warn("WSACleanup() failed: %d", WSAGetLastError());
+}
+
+char *errnostrsocket(void)
+{
+    static char buf[256];
+
+    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
+                  NULL, WSAGetLastError(), 0, buf, sizeof(buf), NULL);
+
+    return buf;
+}
+
 ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags)
 {
     LPFN_WSARECVMSG WSARecvMsg = NULL;
@@ -192,7 +219,7 @@ ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags)
 
 int ioctl(int d, unsigned long request, int *p)
 {
-    u_long arg = 0;
+    u_long arg = *p;
 
     if(ioctlsocket(d, request, &arg) == SOCKET_ERROR) {
         errno = wsa_errno();
@@ -210,11 +237,11 @@ int ioctl(int d, unsigned long request, int *p)
 
 int pipe(int pipefd[2])
 {
-    int lsock = INVALID_SOCKET;
+    int lsock = (int)INVALID_SOCKET;
     struct sockaddr_in laddr;
     socklen_t laddrlen = sizeof(laddr);
 
-    pipefd[0] = pipefd[1] = INVALID_SOCKET;
+    pipefd[0] = pipefd[1] = (int)INVALID_SOCKET;
 
     // bind a listening socket to a TCP port on localhost
     laddr.sin_family = AF_INET;
@@ -267,7 +294,8 @@ int uname(struct utsname *buf)
     strncpy(buf->version, "unknown", sizeof(buf->sysname)-1);   // we don't need it
 
     // computer (node) name
-    if(GetComputerName(buf->nodename, sizeof(buf->nodename)-1) == 0) {
+    DWORD nodename_size = sizeof(buf->nodename)-1;
+    if(GetComputerName(buf->nodename, &nodename_size) == 0) {
         errno = EFAULT;
         return -1;
     }