From d96bea7155f0d97eebdccb2b442149ece0270709 Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Mon, 3 Aug 2026 11:19:46 +0200 Subject: [PATCH] example: drop two local macros, merge/simplify header guards Also: include standard headers first to not interfere with Windows redefinitions. Closes #2498 --- example/direct_tcpip.c | 12 +++++------- example/scp.c | 8 +++----- example/scp_nonblock.c | 12 ++++-------- example/scp_write.c | 20 +++++++++----------- example/scp_write_nonblock.c | 22 ++++++++++------------ example/sftp.c | 10 ++++------ example/sftp_RW_nonblock.c | 8 +++----- example/sftp_append.c | 4 ++-- example/sftp_mkdir.c | 4 ++-- example/sftp_mkdir_nonblock.c | 4 ++-- example/sftp_nonblock.c | 12 ++++-------- example/sftp_write.c | 4 ++-- example/sftp_write_nonblock.c | 6 +++--- example/sftp_write_sliding.c | 8 ++++---- example/sftpdir.c | 10 ++++------ example/sftpdir_nonblock.c | 4 ++-- example/ssh2.c | 12 +++++------- example/ssh2_agent.c | 6 +++--- example/ssh2_agent_forwarding.c | 6 +++--- example/ssh2_echo.c | 6 +++--- example/ssh2_exec.c | 8 ++++---- example/subsystem_netconf.c | 6 +++--- example/tcpip-forward.c | 12 +++++------- example/x11.c | 14 +++++++------- 24 files changed, 96 insertions(+), 122 deletions(-) diff --git a/example/direct_tcpip.c b/example/direct_tcpip.c index a550dd71..ddab47bd 100644 --- a/example/direct_tcpip.c +++ b/example/direct_tcpip.c @@ -6,13 +6,15 @@ #include "libssh2_setup.h" #include +#include +#include +#include + #ifdef _WIN32 #include /* for socklen_t */ #define recv(s, b, l, f) recv(s, b, (int)(l), f) #define send(s, b, l, f) send(s, b, (int)(l), f) -#endif - -#ifndef _WIN32 +#else #include #include #endif @@ -29,10 +31,6 @@ #include /* for timeval */ #endif -#include -#include -#include - #ifndef INADDR_NONE #define INADDR_NONE ((in_addr_t)~0) #endif diff --git a/example/scp.c b/example/scp.c index e1b18c28..478b1c2f 100644 --- a/example/scp.c +++ b/example/scp.c @@ -8,11 +8,11 @@ #include "libssh2_setup.h" #include +#include + #ifdef _WIN32 #define write(f, b, c) _write(f, b, (unsigned int)(c)) -#endif - -#ifndef _WIN32 +#else #include #include #endif @@ -23,8 +23,6 @@ #include #endif -#include - static const char *pubkey = "/home/username/.ssh/id_rsa.pub"; static const char *privkey = "/home/username/.ssh/id_rsa"; static const char *username = "username"; diff --git a/example/scp_nonblock.c b/example/scp_nonblock.c index baedf076..6c488999 100644 --- a/example/scp_nonblock.c +++ b/example/scp_nonblock.c @@ -13,11 +13,11 @@ #include "libssh2_setup.h" #include +#include + #ifdef _WIN32 #define write(f, b, c) _write(f, b, (unsigned int)(c)) -#endif - -#ifndef _WIN32 +#else #include #include #endif @@ -34,8 +34,6 @@ #include /* for timeval, gettimeofday() */ #endif -#include - static const char *pubkey = "/home/username/.ssh/id_rsa.pub"; static const char *privkey = "/home/username/.ssh/id_rsa"; static const char *username = "username"; @@ -47,15 +45,13 @@ static int gettimeofday(struct timeval *tp, void *tzp) { (void)tzp; if(tp) { -/* Offset between 1601-01-01 and 1970-01-01 in 100 nanosec units */ -#define WIN32_FT_OFFSET 116444736000000000 union { libssh2_uint64_t ns100; /* time since 1 Jan 1601 in 100ns units */ FILETIME ft; } now; GetSystemTimeAsFileTime(&now.ft); tp->tv_usec = (long)((now.ns100 / 10) % 1000000); - tp->tv_sec = (long)((now.ns100 - WIN32_FT_OFFSET) / 10000000); + tp->tv_sec = (long)((now.ns100 - 116444736000000000) / 10000000); } return 0; } diff --git a/example/scp_write.c b/example/scp_write.c index 335aa3a7..13f6953b 100644 --- a/example/scp_write.c +++ b/example/scp_write.c @@ -8,17 +8,6 @@ #include "libssh2_setup.h" #include -#ifndef _WIN32 -#include -#include -#endif -#ifdef HAVE_NETINET_IN_H -#include -#endif -#ifdef HAVE_ARPA_INET_H -#include -#endif - #include #ifdef _WIN32 @@ -27,6 +16,15 @@ #undef fstat #define fstat _fstat #define fileno _fileno +#else +#include +#include +#endif +#ifdef HAVE_NETINET_IN_H +#include +#endif +#ifdef HAVE_ARPA_INET_H +#include #endif static const char *pubkey = "/home/username/.ssh/id_rsa.pub"; diff --git a/example/scp_write_nonblock.c b/example/scp_write_nonblock.c index 26397a27..7212fc65 100644 --- a/example/scp_write_nonblock.c +++ b/example/scp_write_nonblock.c @@ -8,7 +8,16 @@ #include "libssh2_setup.h" #include -#ifndef _WIN32 +#include +#include /* for time() */ + +#ifdef _WIN32 +#undef stat +#define stat _stat +#undef fstat +#define fstat _fstat +#define fileno _fileno +#else #include #include #endif @@ -25,17 +34,6 @@ #include /* for timeval */ #endif -#include -#include /* for time() */ - -#ifdef _WIN32 -#undef stat -#define stat _stat -#undef fstat -#define fstat _fstat -#define fileno _fileno -#endif - static const char *pubkey = "/home/username/.ssh/id_rsa.pub"; static const char *privkey = "/home/username/.ssh/id_rsa"; static const char *username = "username"; diff --git a/example/sftp.c b/example/sftp.c index 59a674ed..a21aa607 100644 --- a/example/sftp.c +++ b/example/sftp.c @@ -14,12 +14,13 @@ #include #include +#include +#include + #ifdef _WIN32 #define strdup _strdup #define write(f, b, c) _write(f, b, (unsigned int)(c)) -#endif - -#ifndef _WIN32 +#else #include #include #endif @@ -30,9 +31,6 @@ #include #endif -#include -#include - static const char *pubkey = "/home/username/.ssh/id_rsa.pub"; static const char *privkey = "/home/username/.ssh/id_rsa"; static const char *username = "username"; diff --git a/example/sftp_RW_nonblock.c b/example/sftp_RW_nonblock.c index 7cf30dc6..d248cb7d 100644 --- a/example/sftp_RW_nonblock.c +++ b/example/sftp_RW_nonblock.c @@ -14,11 +14,11 @@ #include #include +#include + #ifdef _WIN32 #define write(f, b, c) _write(f, b, (unsigned int)(c)) -#endif - -#ifndef _WIN32 +#else #include #include #endif @@ -32,8 +32,6 @@ #include /* for timeval */ #endif -#include - static const char *pubkey = "/home/username/.ssh/id_rsa.pub"; static const char *privkey = "/home/username/.ssh/id_rsa"; static const char *username = "username"; diff --git a/example/sftp_append.c b/example/sftp_append.c index c5632b49..9c9bab3a 100644 --- a/example/sftp_append.c +++ b/example/sftp_append.c @@ -14,6 +14,8 @@ #include #include +#include + #ifndef _WIN32 #include #include @@ -25,8 +27,6 @@ #include #endif -#include - static const char *pubkey = "/home/username/.ssh/id_rsa.pub"; static const char *privkey = "/home/username/.ssh/id_rsa"; static const char *username = "username"; diff --git a/example/sftp_mkdir.c b/example/sftp_mkdir.c index 03dee178..f8857be1 100644 --- a/example/sftp_mkdir.c +++ b/example/sftp_mkdir.c @@ -14,6 +14,8 @@ #include #include +#include + #ifndef _WIN32 #include #include @@ -25,8 +27,6 @@ #include #endif -#include - static const char *pubkey = "/home/username/.ssh/id_rsa.pub"; static const char *privkey = "/home/username/.ssh/id_rsa"; static const char *username = "username"; diff --git a/example/sftp_mkdir_nonblock.c b/example/sftp_mkdir_nonblock.c index ceb4525b..028a9f3d 100644 --- a/example/sftp_mkdir_nonblock.c +++ b/example/sftp_mkdir_nonblock.c @@ -14,6 +14,8 @@ #include #include +#include + #ifndef _WIN32 #include #include @@ -25,8 +27,6 @@ #include #endif -#include - static const char *pubkey = "/home/username/.ssh/id_rsa.pub"; static const char *privkey = "/home/username/.ssh/id_rsa"; static const char *username = "username"; diff --git a/example/sftp_nonblock.c b/example/sftp_nonblock.c index 0a04def6..2b0e1c8f 100644 --- a/example/sftp_nonblock.c +++ b/example/sftp_nonblock.c @@ -14,11 +14,11 @@ #include #include +#include + #ifdef _WIN32 #define write(f, b, c) _write(f, b, (unsigned int)(c)) -#endif - -#ifndef _WIN32 +#else #include #include #endif @@ -35,8 +35,6 @@ #include /* for timeval, gettimeofday() */ #endif -#include - static const char *pubkey = "/home/username/.ssh/id_rsa.pub"; static const char *privkey = "/home/username/.ssh/id_rsa"; static const char *username = "username"; @@ -48,15 +46,13 @@ static int gettimeofday(struct timeval *tp, void *tzp) { (void)tzp; if(tp) { -/* Offset between 1601-01-01 and 1970-01-01 in 100 nanosec units */ -#define WIN32_FT_OFFSET 116444736000000000 union { libssh2_uint64_t ns100; /* time since 1 Jan 1601 in 100ns units */ FILETIME ft; } now; GetSystemTimeAsFileTime(&now.ft); tp->tv_usec = (long)((now.ns100 / 10) % 1000000); - tp->tv_sec = (long)((now.ns100 - WIN32_FT_OFFSET) / 10000000); + tp->tv_sec = (long)((now.ns100 - 116444736000000000) / 10000000); } return 0; } diff --git a/example/sftp_write.c b/example/sftp_write.c index ba84f08e..ea40f5ef 100644 --- a/example/sftp_write.c +++ b/example/sftp_write.c @@ -14,6 +14,8 @@ #include #include +#include + #ifndef _WIN32 #include #include @@ -25,8 +27,6 @@ #include #endif -#include - static const char *pubkey = "/home/username/.ssh/id_rsa.pub"; static const char *privkey = "/home/username/.ssh/id_rsa"; static const char *username = "username"; diff --git a/example/sftp_write_nonblock.c b/example/sftp_write_nonblock.c index c461242c..25db9d14 100644 --- a/example/sftp_write_nonblock.c +++ b/example/sftp_write_nonblock.c @@ -14,6 +14,9 @@ #include #include +#include +#include /* for time() */ + #ifndef _WIN32 #include #include @@ -31,9 +34,6 @@ #include /* for timeval */ #endif -#include -#include /* for time() */ - static const char *pubkey = "/home/username/.ssh/id_rsa.pub"; static const char *privkey = "/home/username/.ssh/id_rsa"; static const char *username = "username"; diff --git a/example/sftp_write_sliding.c b/example/sftp_write_sliding.c index 3a356832..525cea75 100644 --- a/example/sftp_write_sliding.c +++ b/example/sftp_write_sliding.c @@ -14,6 +14,10 @@ #include #include +#include +#include /* for time() */ +#include + #ifndef _WIN32 #include #include @@ -31,10 +35,6 @@ #include /* for timeval */ #endif -#include -#include /* for time() */ -#include - static const char *pubkey = "/home/username/.ssh/id_rsa.pub"; static const char *privkey = "/home/username/.ssh/id_rsa"; static const char *username = "username"; diff --git a/example/sftpdir.c b/example/sftpdir.c index bcef2701..83ec7798 100644 --- a/example/sftpdir.c +++ b/example/sftpdir.c @@ -14,11 +14,12 @@ #include #include +#include +#include + #ifdef _WIN32 #define strdup _strdup -#endif - -#ifndef _WIN32 +#else #include #include #endif @@ -29,9 +30,6 @@ #include #endif -#include -#include - #ifdef _MSC_VER #define LIBSSH2_FILESIZE_MASK "I64u" #else diff --git a/example/sftpdir_nonblock.c b/example/sftpdir_nonblock.c index 25801588..bdc0637e 100644 --- a/example/sftpdir_nonblock.c +++ b/example/sftpdir_nonblock.c @@ -14,6 +14,8 @@ #include #include +#include + #ifndef _WIN32 #include #include @@ -25,8 +27,6 @@ #include #endif -#include - #ifdef _MSC_VER #define LIBSSH2_FILESIZE_MASK "I64u" #else diff --git a/example/ssh2.c b/example/ssh2.c index 5503341b..59f4dbe2 100644 --- a/example/ssh2.c +++ b/example/ssh2.c @@ -18,11 +18,13 @@ #include "libssh2_setup.h" #include +#include +#include +#include + #ifdef _WIN32 #define strdup _strdup -#endif - -#ifndef _WIN32 +#else #include #include #endif @@ -33,10 +35,6 @@ #include #endif -#include -#include -#include - static const char *pubkey = ".ssh/id_rsa.pub"; static const char *privkey = ".ssh/id_rsa"; static const char *username = "username"; diff --git a/example/ssh2_agent.c b/example/ssh2_agent.c index b491d469..aad4e9d1 100644 --- a/example/ssh2_agent.c +++ b/example/ssh2_agent.c @@ -12,6 +12,9 @@ #include "libssh2_setup.h" #include +#include +#include + #ifndef _WIN32 #include #include @@ -23,9 +26,6 @@ #include #endif -#include -#include - static const char *username = "username"; int main(int argc, char *argv[]) diff --git a/example/ssh2_agent_forwarding.c b/example/ssh2_agent_forwarding.c index cadb3ba3..2653e2a1 100644 --- a/example/ssh2_agent_forwarding.c +++ b/example/ssh2_agent_forwarding.c @@ -16,6 +16,9 @@ #include "libssh2_setup.h" #include +#include +#include + #ifndef _WIN32 #include #include @@ -33,9 +36,6 @@ #include /* for timeval */ #endif -#include -#include - static const char *hostname = "127.0.0.1"; static const char *commandline = "uptime"; static const char *username; diff --git a/example/ssh2_echo.c b/example/ssh2_echo.c index ccd9c5a6..0b40cec7 100644 --- a/example/ssh2_echo.c +++ b/example/ssh2_echo.c @@ -17,6 +17,9 @@ #ifndef LIBSSH2_NO_DEPRECATED +#include +#include + #ifndef _WIN32 #include #include @@ -34,9 +37,6 @@ #include /* for timeval */ #endif -#include -#include - static const char *hostname = "127.0.0.1"; static const char *commandline = "cat"; static const char *username = "user"; diff --git a/example/ssh2_exec.c b/example/ssh2_exec.c index 4ca06dcf..8cd116f5 100644 --- a/example/ssh2_exec.c +++ b/example/ssh2_exec.c @@ -13,6 +13,10 @@ #include "libssh2_setup.h" #include +#include +#include +#include + #ifndef _WIN32 #include #include @@ -30,10 +34,6 @@ #include /* for timeval */ #endif -#include -#include -#include - static const char *hostname = "127.0.0.1"; static const char *commandline = "uptime"; static const char *pubkey = "/home/username/.ssh/id_rsa.pub"; diff --git a/example/subsystem_netconf.c b/example/subsystem_netconf.c index e9714aa1..f2fc40bd 100644 --- a/example/subsystem_netconf.c +++ b/example/subsystem_netconf.c @@ -6,6 +6,9 @@ #include "libssh2_setup.h" #include +#include +#include + #ifndef _WIN32 #include #include @@ -17,9 +20,6 @@ #include #endif -#include -#include - #ifndef INADDR_NONE #define INADDR_NONE ((in_addr_t)~0) #endif diff --git a/example/tcpip-forward.c b/example/tcpip-forward.c index 30645870..54feab10 100644 --- a/example/tcpip-forward.c +++ b/example/tcpip-forward.c @@ -6,13 +6,15 @@ #include "libssh2_setup.h" #include +#include +#include +#include + #ifdef _WIN32 #include /* for socklen_t */ #define recv(s, b, l, f) recv(s, b, (int)(l), f) #define send(s, b, l, f) send(s, b, (int)(l), f) -#endif - -#ifndef _WIN32 +#else #include #include #endif @@ -29,10 +31,6 @@ #include /* for timeval */ #endif -#include -#include -#include - #ifndef INADDR_NONE #define INADDR_NONE ((in_addr_t)~0) #endif diff --git a/example/x11.c b/example/x11.c index c3be63c3..31c16bfa 100644 --- a/example/x11.c +++ b/example/x11.c @@ -16,16 +16,19 @@ #if defined(HAVE_SYS_UN_H) && !defined(LIBSSH2_NO_DEPRECATED) +#include +#include + +#ifndef _WIN32 +#include +#include +#endif #ifdef HAVE_SYS_IOCTL_H #include #endif #ifdef HAVE_NETINET_IN_H #include #endif -#ifndef _WIN32 -#include -#include -#endif #ifdef HAVE_SYS_SELECT_H #include #endif @@ -39,9 +42,6 @@ #include #endif -#include -#include - #include #define PATH_UNIX_X "/tmp/.X11-unix/X%d"