From cdb437c54540d2d98edd7e16416733f0973a4c06 Mon Sep 17 00:00:00 2001 From: Tim Crawford Date: Tue, 28 Nov 2017 20:45:51 -0500 Subject: [PATCH 1/2] Fix compilation with VS2017 The primary issue is that in MSVC 14.00 (VS2015) Microsoft added snprintf as a function to the standard library and prevents users from defining it to something else (typically, this was _snprintf). So, only define it when using _MSC_VER < 1900. Other changes are: - Fix macro definition of dup2 - Add macro for getpid - Add function definition for win32_dup - Add missing EXTERNs Signed-off-by: Tim Crawford --- include/iscsi.h | 4 ++-- lib/init.c | 1 + lib/socket.c | 2 +- win32/win32_compat.h | 15 +++++++++++---- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/include/iscsi.h b/include/iscsi.h index 9b2b85e..798703c 100644 --- a/include/iscsi.h +++ b/include/iscsi.h @@ -166,7 +166,7 @@ enum iscsi_immediate_data { ISCSI_IMMEDIATE_DATA_NO = 0, ISCSI_IMMEDIATE_DATA_YES = 1 }; -int iscsi_set_immediate_data(struct iscsi_context *iscsi, enum iscsi_immediate_data immediate_data); +EXTERN int iscsi_set_immediate_data(struct iscsi_context *iscsi, enum iscsi_immediate_data immediate_data); /* * This function is used to set the desired mode for initial_r2t @@ -179,7 +179,7 @@ enum iscsi_initial_r2t { ISCSI_INITIAL_R2T_NO = 0, ISCSI_INITIAL_R2T_YES = 1 }; -int +EXTERN int iscsi_set_initial_r2t(struct iscsi_context *iscsi, enum iscsi_initial_r2t initial_r2t); diff --git a/lib/init.c b/lib/init.c index 645b05f..88bc7eb 100644 --- a/lib/init.c +++ b/lib/init.c @@ -22,6 +22,7 @@ #define _GNU_SOURCE #if defined(WIN32) +#include "win32/win32_compat.h" #else #include #include diff --git a/lib/socket.c b/lib/socket.c index a7c2fa0..22e6e57 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -1102,7 +1102,7 @@ void iscsi_set_bind_interfaces(struct iscsi_context *iscsi, char * interfaces _U #endif } -#ifdef WIN32 +#if defined(_MSC_VER) && _MSC_VER < 1900 static iscsi_transport iscsi_transport_tcp = { iscsi_tcp_connect, iscsi_tcp_queue_pdu, diff --git a/win32/win32_compat.h b/win32/win32_compat.h index 62330f6..8ec388a 100644 --- a/win32/win32_compat.h +++ b/win32/win32_compat.h @@ -22,9 +22,10 @@ THE SOFTWARE. */ /*Adaptions by memphiz@xbmc.org*/ -#ifdef WIN32 #ifndef win32_COMPAT_H_ #define win32_COMPAT_H_ + +#ifdef WIN32 #define NO_IPv6 1 #include @@ -65,16 +66,22 @@ typedef int socklen_t; #define writev win32_writev #define strncasecmp _strnicmp #define strdup _strdup -#define dup2(x, y, z) win32_dup2(x, y) +#define dup2(x, y) win32_dup2(x, y) #define poll(x, y, z) win32_poll(x, y, z) #define inet_pton(x,y,z) win32_inet_pton(x,y,z) #define sleep(x) Sleep(x * 1000) +#define getpid GetCurrentProcessId + +#if defined(_MSC_VER) && _MSC_VER < 1900 #define snprintf(a, b, c, ...) _snprintf_s(a, b, b, c, ## __VA_ARGS__) +#endif + int win32_inet_pton(int af, const char * src, void * dst); int win32_poll(struct pollfd *fds, unsigned int nfsd, int timeout); int win32_gettimeofday(struct timeval *tv, struct timezone *tz); ssize_t win32_writev(int fd, const struct iovec *iov, int iovcnt); ssize_t win32_readv(int fd, const struct iovec *iov, int iovcnt); +int win32_dup2(int oldfd, int newfd); struct iovec { void *iov_base; @@ -83,5 +90,5 @@ struct iovec { #define inline -#endif//win32_COMPAT_H_ -#endif//WIN32 +#endif // WIN32 +#endif // win32_COMPAT_H_ From aba0f7da1ae9d6f1d5fd628f3cdb1df5a3073b8a Mon Sep 17 00:00:00 2001 From: Tim Crawford Date: Tue, 28 Nov 2017 20:48:48 -0500 Subject: [PATCH 2/2] Replace WIN32 with _WIN32 Using WIN32 depends on the build environment defining the variable. _WIN32 is a predefined MSVC macro and is always available. Signed-off-by: Tim Crawford --- examples/iscsiclient.c | 4 ++-- include/iscsi-private.h | 4 ++-- include/iscsi.h | 2 +- include/md5.h | 2 +- include/scsi-lowlevel.h | 2 +- lib/connect.c | 2 +- lib/crc32c.c | 2 +- lib/init.c | 2 +- lib/iscsi-command.c | 2 +- lib/logging.c | 2 +- lib/login.c | 2 +- lib/nop.c | 2 +- lib/pdu.c | 2 +- lib/scsi-lowlevel.c | 2 +- lib/socket.c | 6 +++--- lib/sync.c | 2 +- utils/iscsi-ls.c | 4 ++-- win32/win32_compat.c | 2 +- win32/win32_compat.h | 4 ++-- 19 files changed, 25 insertions(+), 25 deletions(-) diff --git a/examples/iscsiclient.c b/examples/iscsiclient.c index 95dbe87..476a925 100644 --- a/examples/iscsiclient.c +++ b/examples/iscsiclient.c @@ -34,7 +34,7 @@ /* This is the host/port we connect to.*/ #define TARGET "127.0.0.1:3260" -#if defined(WIN32) +#if defined(_WIN32) #include #include "win32/win32_compat.h" #pragma comment(lib, "ws2_32.lib") @@ -625,7 +625,7 @@ int main(int argc _U_, char *argv[] _U_) struct client_state clnt; printf("iscsi client\n"); -#if defined(WIN32) +#if defined(_WIN32) if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) { printf("Failed to start Winsock2\n"); exit(10); diff --git a/include/iscsi-private.h b/include/iscsi-private.h index a85b4bd..aedbbe0 100644 --- a/include/iscsi-private.h +++ b/include/iscsi-private.h @@ -20,7 +20,7 @@ #include #include -#if defined(WIN32) +#if defined(_WIN32) #include #define ssize_t SSIZE_T #endif @@ -324,7 +324,7 @@ int iscsi_process_reject(struct iscsi_context *iscsi, struct iscsi_in_pdu *in); int iscsi_send_target_nop_out(struct iscsi_context *iscsi, uint32_t ttt, uint32_t lun); -#if defined(WIN32) +#if defined(_WIN32) void iscsi_set_error(struct iscsi_context *iscsi, const char *error_string, ...); #else diff --git a/include/iscsi.h b/include/iscsi.h index 798703c..63fa94b 100644 --- a/include/iscsi.h +++ b/include/iscsi.h @@ -20,7 +20,7 @@ #include #include -#if defined(WIN32) +#if defined(_WIN32) #define EXTERN __declspec( dllexport ) #else #define EXTERN diff --git a/include/md5.h b/include/md5.h index 52ba252..c5cdd0e 100644 --- a/include/md5.h +++ b/include/md5.h @@ -23,7 +23,7 @@ #ifndef MD5_H #define MD5_H -#if defined(WIN32) +#if defined(_WIN32) #else #include #endif diff --git a/include/scsi-lowlevel.h b/include/scsi-lowlevel.h index 2b1e407..f2a8908 100644 --- a/include/scsi-lowlevel.h +++ b/include/scsi-lowlevel.h @@ -17,7 +17,7 @@ #ifndef __scsi_lowlevel_h__ #define __scsi_lowlevel_h__ -#if defined(WIN32) +#if defined(_WIN32) #define EXTERN __declspec( dllexport ) #else #define EXTERN diff --git a/lib/connect.c b/lib/connect.c index 989b924..4043f7c 100644 --- a/lib/connect.c +++ b/lib/connect.c @@ -15,7 +15,7 @@ You should have received a copy of the GNU Lesser General Public License along with this program; if not, see . */ -#if defined(WIN32) +#if defined(_WIN32) #include "win32/win32_compat.h" #else #include diff --git a/lib/crc32c.c b/lib/crc32c.c index 4455171..3c9d6a5 100644 --- a/lib/crc32c.c +++ b/lib/crc32c.c @@ -15,7 +15,7 @@ You should have received a copy of the GNU Lesser General Public License along with this program; if not, see . */ -#if defined(WIN32) +#if defined(_WIN32) #else #include #endif diff --git a/lib/init.c b/lib/init.c index 88bc7eb..34a95a5 100644 --- a/lib/init.c +++ b/lib/init.c @@ -21,7 +21,7 @@ #define _GNU_SOURCE -#if defined(WIN32) +#if defined(_WIN32) #include "win32/win32_compat.h" #else #include diff --git a/lib/iscsi-command.c b/lib/iscsi-command.c index f9ca8e0..5be7ee2 100644 --- a/lib/iscsi-command.c +++ b/lib/iscsi-command.c @@ -27,7 +27,7 @@ #include #endif -#if defined(WIN32) +#if defined(_WIN32) #include #endif diff --git a/lib/logging.c b/lib/logging.c index bc31f24..be518fc 100644 --- a/lib/logging.c +++ b/lib/logging.c @@ -31,7 +31,7 @@ #include #endif -#if defined(WIN32) +#if defined(_WIN32) #include "win32/win32_compat.h" #endif diff --git a/lib/login.c b/lib/login.c index 36de085..e2bb112 100644 --- a/lib/login.c +++ b/lib/login.c @@ -32,7 +32,7 @@ #include #endif -#if defined(WIN32) +#if defined(_WIN32) #include #include "win32/win32_compat.h" #endif diff --git a/lib/nop.c b/lib/nop.c index 6d9f450..b2104cd 100644 --- a/lib/nop.c +++ b/lib/nop.c @@ -16,7 +16,7 @@ along with this program; if not, see . */ -#if defined(WIN32) +#if defined(_WIN32) #else #include #endif diff --git a/lib/pdu.c b/lib/pdu.c index 6fd4b45..0bcbe11 100644 --- a/lib/pdu.c +++ b/lib/pdu.c @@ -34,7 +34,7 @@ #define PRIx32 "x" #endif -#if defined(WIN32) +#if defined(_WIN32) #include #include #include "win32/win32_compat.h" diff --git a/lib/scsi-lowlevel.c b/lib/scsi-lowlevel.c index 0558e5b..d68d990 100644 --- a/lib/scsi-lowlevel.c +++ b/lib/scsi-lowlevel.c @@ -38,7 +38,7 @@ #include "aros/aros_compat.h" #endif -#if defined(WIN32) +#if defined(_WIN32) #include #include "win32/win32_compat.h" #else diff --git a/lib/socket.c b/lib/socket.c index 22e6e57..4aa0d9a 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -43,7 +43,7 @@ #include #endif -#if defined(WIN32) +#if defined(_WIN32) #include #include #include "win32/win32_compat.h" @@ -139,7 +139,7 @@ void iscsi_decrement_iface_rr() { static int set_nonblocking(int fd) { -#if defined(WIN32) +#if defined(_WIN32) unsigned long opt = 1; return ioctlsocket(fd, FIONBIO, &opt); #else @@ -272,7 +272,7 @@ static int iscsi_tcp_connect(struct iscsi_context *iscsi, union socket_address * } if (connect(iscsi->fd, &sa->sa, socksize) != 0 -#if defined(WIN32) +#if defined(_WIN32) && WSAGetLastError() != WSAEWOULDBLOCK #endif && errno != EINPROGRESS) { diff --git a/lib/sync.c b/lib/sync.c index 1c2084e..ce59c29 100644 --- a/lib/sync.c +++ b/lib/sync.c @@ -27,7 +27,7 @@ #include "aros/aros_compat.h" #endif -#if defined(WIN32) +#if defined(_WIN32) #include #include "win32/win32_compat.h" #endif diff --git a/utils/iscsi-ls.c b/utils/iscsi-ls.c index 2c4dbc6..95ab1a6 100644 --- a/utils/iscsi-ls.c +++ b/utils/iscsi-ls.c @@ -19,7 +19,7 @@ #include "config.h" #endif -#if defined(WIN32) +#if defined(_WIN32) #include #include "win32_compat.h" #pragma comment(lib, "ws2_32.lib") @@ -354,7 +354,7 @@ int main(int argc, char *argv[]) int i; static int show_help = 0, show_usage = 0, debug = 0; -#ifdef WIN32 +#ifdef _WIN32 if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) { printf("Failed to start Winsock2\n"); exit(10); diff --git a/win32/win32_compat.c b/win32/win32_compat.c index 8c797be..d6bece5 100644 --- a/win32/win32_compat.c +++ b/win32/win32_compat.c @@ -21,7 +21,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#ifndef WIN32 +#ifndef _WIN32 static int dummy ATTRIBUTE((unused)); diff --git a/win32/win32_compat.h b/win32/win32_compat.h index 8ec388a..f30269a 100644 --- a/win32/win32_compat.h +++ b/win32/win32_compat.h @@ -25,7 +25,7 @@ THE SOFTWARE. #ifndef win32_COMPAT_H_ #define win32_COMPAT_H_ -#ifdef WIN32 +#ifdef _WIN32 #define NO_IPv6 1 #include @@ -90,5 +90,5 @@ struct iovec { #define inline -#endif // WIN32 +#endif // _WIN32 #endif // win32_COMPAT_H_