From 5458ba05b5f320d518854dcd1854d4f5c935bc5a Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Thu, 10 Feb 2011 23:47:46 -0800 Subject: [PATCH] Cleaned up daemon.c --- guacd/src/daemon.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/guacd/src/daemon.c b/guacd/src/daemon.c index bdc0a0da..c7340619 100644 --- a/guacd/src/daemon.c +++ b/guacd/src/daemon.c @@ -25,24 +25,33 @@ #ifdef __MINGW32__ #include -#define CLOSE_SOCKET(socket) closesocket(socket) #else #include #include -#define CLOSE_SOCKET(socket) close(socket) #endif -#ifdef __HAVE_PTHREAD_H__ +#ifdef HAVE_LIBPTHREAD #include -#elif defined(__MINGW32) +#elif defined(__MINGW32__) #include #include #endif #include -char error[65536]; +#include +#include +/* Windows / MINGW32 handles closing sockets differently */ +#ifdef __MINGW32__ +#define CLOSE_SOCKET(socket) closesocket(socket) +#else +#define CLOSE_SOCKET(socket) close(socket) +#endif + + +/* Cross-platform strerror()/errno clone */ +char error[65536]; char* lasterror() { #ifdef __MINGW32__ snprintf(error, sizeof(error)-1, "ERROR #%i", GetLastError()); @@ -52,8 +61,6 @@ char* lasterror() { #endif } -#include -#include typedef struct client_thread_data { @@ -113,6 +120,11 @@ int main(int argc, char* argv[]) { /* Daemon Process */ pid_t daemon_pid; +#ifdef __MINGW32__ + /* Structure for holding winsock version info */ + WSADATA wsadata; +#endif + /* Parse arguments */ while ((opt = getopt(argc, argv, "l:")) != -1) { if (opt == 'l') { @@ -135,7 +147,7 @@ int main(int argc, char* argv[]) { server_addr.sin_port = htons(listen_port); #ifdef __MINGW32__ - WSADATA wsadata; + /* If compiling for Windows, init winsock. */ if (WSAStartup(MAKEWORD(1,1), &wsadata) == SOCKET_ERROR) { fprintf(stderr, "Error creating socket."); exit(EXIT_FAILURE); @@ -185,7 +197,7 @@ int main(int argc, char* argv[]) { /* Daemon loop */ for (;;) { -#ifdef pthread_t +#ifdef HAVE_LIBPTHREAD pthread_t thread; #endif client_thread_data* data; @@ -207,7 +219,7 @@ int main(int argc, char* argv[]) { data = malloc(sizeof(client_thread_data)); data->fd = connected_socket_fd; -#ifdef pthread_t +#ifdef HAVE_LIBPTHREAD if (pthread_create(&thread, NULL, start_client_thread, (void*) data)) { GUAC_LOG_ERROR("Could not create client thread: %s", lasterror()); return 3;