summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Harris <pharris@opentext.com>2021-02-01 17:43:52 -0500
committerPeter Harris <pharris@opentext.com>2021-06-04 14:31:13 +0000
commit4b0d9d3868aad8d5f4266821e9eda586e6c2bfa7 (patch)
treed31a073fa5d036d5ec404b539fac44cbc5be18d3
parentcd0fba98a2d0867d505ff1a7ca8d7a7c757acfa2 (diff)
Fix build on Windows
Notable changes: Protect include of unistd.h (and other POSIX headers). Use SOCKET (which is larger than int) and closesocket (because close is not compatible) for sockets. Use <stdint.h>'s intptr_t instead of the non-portable ssize_t. Signed-off-by: Peter Harris <pharris@opentext.com>
-rw-r--r--src/xcb.h4
-rw-r--r--src/xcb_auth.c11
-rw-r--r--src/xcb_conn.c11
-rw-r--r--src/xcb_in.c4
-rw-r--r--src/xcb_out.c4
-rw-r--r--src/xcb_util.c6
6 files changed, 34 insertions, 6 deletions
diff --git a/src/xcb.h b/src/xcb.h
index 09e123e..3f39bb4 100644
--- a/src/xcb.h
+++ b/src/xcb.h
@@ -51,7 +51,11 @@ extern "C" {
* @file xcb.h
*/
+#ifdef __GNUC__
#define XCB_PACKED __attribute__((__packed__))
+#else
+#define XCB_PACKED
+#endif
/**
* @defgroup XCB_Core_API XCB Core API
diff --git a/src/xcb_auth.c b/src/xcb_auth.c
index 6937afb..8ebe9a4 100644
--- a/src/xcb_auth.c
+++ b/src/xcb_auth.c
@@ -31,8 +31,6 @@
#include <assert.h>
#include <X11/Xauth.h>
-#include <sys/param.h>
-#include <unistd.h>
#include <stdlib.h>
#include <time.h>
@@ -49,6 +47,8 @@
#endif
#include "xcb_windefs.h"
#else
+#include <sys/param.h>
+#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
@@ -271,10 +271,17 @@ static int compute_auth(xcb_auth_info_t *info, Xauth *authptr, struct sockaddr *
to the value returned by either getpeername() or getsockname()
(according to POSIX, applications should not assume a particular
length for `sockaddr_un.sun_path') */
+#ifdef _WIN32
+static struct sockaddr *get_peer_sock_name(int(_stdcall *socket_func)(SOCKET,
+ struct sockaddr *,
+ socklen_t *),
+ int fd)
+#else
static struct sockaddr *get_peer_sock_name(int (*socket_func)(int,
struct sockaddr *,
socklen_t *),
int fd)
+#endif
{
socklen_t socknamelen = sizeof(struct sockaddr) + INITIAL_SOCKNAME_SLACK;
socklen_t actual_socknamelen = socknamelen;
diff --git a/src/xcb_conn.c b/src/xcb_conn.c
index 8dab658..158f676 100644
--- a/src/xcb_conn.c
+++ b/src/xcb_conn.c
@@ -32,7 +32,6 @@
#include <assert.h>
#include <string.h>
#include <stdio.h>
-#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
@@ -48,7 +47,9 @@
#ifdef _WIN32
#include "xcb_windefs.h"
+#include <io.h>
#else
+#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif /* _WIN32 */
@@ -345,7 +346,11 @@ xcb_connection_t *xcb_connect_to_fd(int fd, xcb_auth_info_t *auth_info)
c = calloc(1, sizeof(xcb_connection_t));
if(!c) {
+#ifdef _WIN32
+ closesocket(fd);
+#else
close(fd);
+#endif
return _xcb_conn_ret_error(XCB_CONN_CLOSED_MEM_INSUFFICIENT) ;
}
@@ -378,7 +383,11 @@ void xcb_disconnect(xcb_connection_t *c)
/* disallow further sends and receives */
shutdown(c->fd, SHUT_RDWR);
+#ifdef _WIN32
+ closesocket(c->fd);
+#else
close(c->fd);
+#endif
pthread_mutex_destroy(&c->iolock);
_xcb_in_destroy(&c->in);
diff --git a/src/xcb_in.c b/src/xcb_in.c
index 796b4e9..7d02e9b 100644
--- a/src/xcb_in.c
+++ b/src/xcb_in.c
@@ -32,7 +32,6 @@
#include <assert.h>
#include <string.h>
#include <stdlib.h>
-#include <unistd.h>
#include <stdio.h>
#include <errno.h>
@@ -40,6 +39,7 @@
#include <poll.h>
#endif
#ifndef _WIN32
+#include <unistd.h>
#include <sys/select.h>
#include <sys/socket.h>
#endif
@@ -365,7 +365,7 @@ static void free_reply_list(struct reply_list *head)
}
}
-static int read_block(const int fd, void *buf, const ssize_t len)
+static int read_block(const int fd, void *buf, const intptr_t len)
{
int done = 0;
while(done < len)
diff --git a/src/xcb_out.c b/src/xcb_out.c
index c9593e5..df94867 100644
--- a/src/xcb_out.c
+++ b/src/xcb_out.c
@@ -31,7 +31,11 @@
#include <assert.h>
#include <stdlib.h>
+#ifdef _WIN32
+#include <io.h>
+#else
#include <unistd.h>
+#endif
#include <string.h>
#include "xcb.h"
diff --git a/src/xcb_util.c b/src/xcb_util.c
index a16270c..0296ce0 100644
--- a/src/xcb_util.c
+++ b/src/xcb_util.c
@@ -36,12 +36,12 @@
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
-#include <unistd.h>
#include <string.h>
#ifdef _WIN32
#include "xcb_windefs.h"
#else
+#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/un.h>
@@ -415,7 +415,11 @@ static int _xcb_open_tcp(const char *host, char *protocol, const unsigned short
if(_xcb_do_connect(fd, (struct sockaddr*)&_s, sizeof(_s)) >= 0)
break;
+#ifdef _WIN32
+ closesocket(fd);
+#else
close(fd);
+#endif
fd = -1;
++_c;
}