From ccbe79c03b8c64432bbf1ce82dc0e123cc02bd6d Mon Sep 17 00:00:00 2001 From: Kirill Isakov Date: Sat, 2 Apr 2022 00:26:51 +0600 Subject: [PATCH] Allow building sptps_{test,keypair} with MSVC --- src/meson.build | 8 +++++--- src/sptps_keypair.c | 2 -- src/sptps_test.c | 21 ++++++--------------- 3 files changed, 11 insertions(+), 20 deletions(-) diff --git a/src/meson.build b/src/meson.build index af2410d0..d2b76cbe 100644 --- a/src/meson.build +++ b/src/meson.build @@ -216,8 +216,10 @@ foreach type : check_types endif endforeach +src_getopt = [] if not cdata.has('HAVE_GETOPT_H') or not cc.has_function('getopt_long', prefix: have_prefix, args: cc_defs) - src_lib_common += ['getopt.c', 'getopt1.c'] + src_getopt = ['getopt.c', 'getopt1.c'] + src_lib_common += src_getopt endif if not opt_miniupnpc.disabled() @@ -403,7 +405,7 @@ exe_tincd = executable( exe_sptps_test = executable( 'sptps_test', - sources: 'sptps_test.c', + sources: [src_getopt, 'sptps_test.c'], dependencies: deps_lib_common, link_with: lib_common, implicit_include_directories: false, @@ -413,7 +415,7 @@ exe_sptps_test = executable( exe_sptps_keypair = executable( 'sptps_keypair', - sources: 'sptps_keypair.c', + sources: [src_getopt, 'sptps_keypair.c'], dependencies: deps_lib_common, link_with: lib_common, implicit_include_directories: false, diff --git a/src/sptps_keypair.c b/src/sptps_keypair.c index d762724f..7e47d06f 100644 --- a/src/sptps_keypair.c +++ b/src/sptps_keypair.c @@ -19,8 +19,6 @@ #include "system.h" -#include - #include "crypto.h" #include "ecdsagen.h" #include "logger.h" diff --git a/src/sptps_test.c b/src/sptps_test.c index baca66b3..7867b722 100644 --- a/src/sptps_test.c +++ b/src/sptps_test.c @@ -23,12 +23,6 @@ #include #endif -#include - -#ifdef HAVE_WINDOWS -#include -#endif - #include "crypto.h" #include "ecdsa.h" #include "meta.h" @@ -70,7 +64,7 @@ int addressfamily = AF_UNSPEC; static bool send_data(void *handle, uint8_t type, const void *data, size_t len) { (void)type; - char hex[len * 2 + 1]; + char *hex = alloca(len * 2 + 1); bin2hex(data, hex, len); if(verbose) { @@ -169,7 +163,7 @@ int stdin_sock_fd = -1; // separate thread between the stdin and the sptps loop way below. This thread // reads stdin and sends its content to the main thread through a TCP socket, // which can be properly select()'ed. -static void *stdin_reader_thread(void *arg) { +static DWORD WINAPI stdin_reader_thread(LPVOID arg) { struct sockaddr_in sa; socklen_t sa_size = sizeof(sa); @@ -226,7 +220,7 @@ static void *stdin_reader_thread(void *arg) { closesocket(stdin_sock_fd); stdin_sock_fd = -1; - return NULL; + return 0; } static int start_input_reader(void) { @@ -275,11 +269,8 @@ static int start_input_reader(void) { fprintf(stderr, "stdin thread is listening on :%d\n", ntohs(connect_sa.sin_port)); } - pthread_t th; - int err = pthread_create(&th, NULL, stdin_reader_thread, NULL); - - if(err) { - fprintf(stderr, "Could not start reader thread: %s\n", strerror(err)); + if(!CreateThread(NULL, 0, stdin_reader_thread, NULL, 0, NULL)) { + fprintf(stderr, "Could not start reader thread: %d\n", GetLastError()); goto server_err; } @@ -669,7 +660,7 @@ int main(int argc, char *argv[]) { } if(verbose) { - char hex[len * 2 + 1]; + char *hex = alloca(len * 2 + 1); bin2hex(buf, hex, len); fprintf(stderr, "Received %ld bytes of data:\n%s\n", (long)len, hex); } -- 2.20.1