From: Viktor Szakats Date: Fri, 24 May 2024 16:38:32 +0000 (+0200) Subject: examples: fix compiling with MSVC X-Git-Tag: curl-8_9_0~386 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d4b85890555388bec212b75f47a5c1a48705b156;p=thirdparty%2Fcurl.git examples: fix compiling with MSVC - `websocket.c`: use `Sleep()` on Windows. `sleep()` and `unistd.h` are not available in MSVC. - `http2-upload.c`: use local `gettimeofday()` implementation when compiled with MSVC. (Alternate solution is to disable the trace function for MSVC.) Public domain code copied and adapted from libssh2: https://github.com/libssh2/libssh2/blob/e973493f992313b3be73f51d3f7ca6d52e288558/src/misc.c#L719-L743 - silence compiler warning for deprecated `inet_addr()`. Also drop duplicate winsock2 include. ``` curl\docs\examples\externalsocket.c(125,32): error C2220: the following warning is treated as an error [curl\bld\docs\examples\curl-example-externalsocket.vcxproj] curl\docs\examples\externalsocket.c(125,32): warning C4996: 'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings [curl\bld\docs\examples\curl-example-e ``` Ref: https://github.com/curl/curl/actions/runs/9227337318/job/25389073450#step:4:95 - silence an MSVC compiler warning. This is in conflict with `checksrc` rules, so silence the rule in favour of the warning-free C syntax. ``` curl\docs\examples\multi-legacy.c(152,1): error C2220: the following warning is treated as an error [curl\bld\docs\examples\curl-example-multi-legacy.vcxproj] curl\docs\examples\multi-legacy.c(152,1): warning C4706: assignment within conditional expression [curl\bld\docs\examples\curl-example-multi-legacy.vcxproj] ``` Ref: https://github.com/curl/curl/actions/runs/9227337318/job/25389073450#step:4:226 - do not use `sys/time.h` and `unistd.h` in Windows builds. Some of these includes look unnecessary. Subject to another PR. Cherry-picked from #13766 Closes #13771 --- diff --git a/docs/examples/10-at-a-time.c b/docs/examples/10-at-a-time.c index a622410fd3..1221126e36 100644 --- a/docs/examples/10-at-a-time.c +++ b/docs/examples/10-at-a-time.c @@ -127,7 +127,8 @@ int main(void) int still_alive = 1; curl_multi_perform(cm, &still_alive); - while((msg = curl_multi_info_read(cm, &msgs_left))) { + /* !checksrc! disable EQUALSNULL 1 */ + while((msg = curl_multi_info_read(cm, &msgs_left)) != NULL) { if(msg->msg == CURLMSG_DONE) { char *url; CURL *e = msg->easy_handle; diff --git a/docs/examples/externalsocket.c b/docs/examples/externalsocket.c index 21e9a9c477..010654298d 100644 --- a/docs/examples/externalsocket.c +++ b/docs/examples/externalsocket.c @@ -25,19 +25,24 @@ * Pass in a custom socket for libcurl to use. * */ +#ifdef _WIN32 +#ifndef _WINSOCK_DEPRECATED_NO_WARNINGS +#define _WINSOCK_DEPRECATED_NO_WARNINGS /* for inet_addr() */ +#endif +#endif + #include #include #include #include #ifdef _WIN32 -#include #define close closesocket #else #include /* socket types */ #include /* socket definitions */ #include -#include /* inet (3) functions */ +#include /* inet (3) functions */ #include /* misc. Unix functions */ #endif diff --git a/docs/examples/http2-download.c b/docs/examples/http2-download.c index c45a1c2d65..3889b0d8a4 100644 --- a/docs/examples/http2-download.c +++ b/docs/examples/http2-download.c @@ -31,8 +31,10 @@ #include /* somewhat unix-specific */ +#ifndef _WIN32 #include #include +#endif /* curl stuff */ #include diff --git a/docs/examples/http2-pushinmemory.c b/docs/examples/http2-pushinmemory.c index 74c64e0a69..cc1422d5a0 100644 --- a/docs/examples/http2-pushinmemory.c +++ b/docs/examples/http2-pushinmemory.c @@ -30,8 +30,10 @@ #include /* somewhat unix-specific */ +#ifndef _WIN32 #include #include +#endif /* curl stuff */ #include diff --git a/docs/examples/http2-serverpush.c b/docs/examples/http2-serverpush.c index e830aa95d4..a03ff4efbb 100644 --- a/docs/examples/http2-serverpush.c +++ b/docs/examples/http2-serverpush.c @@ -30,8 +30,10 @@ #include /* somewhat unix-specific */ +#ifndef _WIN32 #include #include +#endif /* curl stuff */ #include diff --git a/docs/examples/http2-upload.c b/docs/examples/http2-upload.c index 56d0c98250..4758a2fac3 100644 --- a/docs/examples/http2-upload.c +++ b/docs/examples/http2-upload.c @@ -33,8 +33,10 @@ #include /* somewhat unix-specific */ +#ifndef _MSC_VER #include #include +#endif /* curl stuff */ #include @@ -49,6 +51,26 @@ #define NUM_HANDLES 1000 +#ifdef _MSC_VER +#define gettimeofday(a, b) my_gettimeofday((a), (b)) +int my_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 { + CURL_TYPEOF_CURL_OFF_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); + } + return 0; +} +#endif + struct input { FILE *in; size_t bytes_read; /* count up */ diff --git a/docs/examples/multi-app.c b/docs/examples/multi-app.c index 63918f182e..95c8981ac5 100644 --- a/docs/examples/multi-app.c +++ b/docs/examples/multi-app.c @@ -31,8 +31,10 @@ #include /* somewhat unix-specific */ +#ifndef _WIN32 #include #include +#endif /* curl stuff */ #include @@ -84,7 +86,8 @@ int main(void) break; } /* See how the transfers went */ - while((msg = curl_multi_info_read(multi_handle, &msgs_left))) { + /* !checksrc! disable EQUALSNULL 1 */ + while((msg = curl_multi_info_read(multi_handle, &msgs_left)) != NULL) { if(msg->msg == CURLMSG_DONE) { int idx; diff --git a/docs/examples/multi-debugcallback.c b/docs/examples/multi-debugcallback.c index 2feb214d50..68f30e9b03 100644 --- a/docs/examples/multi-debugcallback.c +++ b/docs/examples/multi-debugcallback.c @@ -30,8 +30,10 @@ #include /* somewhat unix-specific */ +#ifndef _WIN32 #include #include +#endif /* curl stuff */ #include diff --git a/docs/examples/multi-double.c b/docs/examples/multi-double.c index 62295a92cf..b59b665fd4 100644 --- a/docs/examples/multi-double.c +++ b/docs/examples/multi-double.c @@ -29,8 +29,10 @@ #include /* somewhat unix-specific */ +#ifndef _WIN32 #include #include +#endif /* curl stuff */ #include diff --git a/docs/examples/multi-formadd.c b/docs/examples/multi-formadd.c index 801b61e6dd..1f2d6d6d27 100644 --- a/docs/examples/multi-formadd.c +++ b/docs/examples/multi-formadd.c @@ -33,7 +33,9 @@ #include #include +#ifndef _WIN32 #include +#endif #include diff --git a/docs/examples/multi-legacy.c b/docs/examples/multi-legacy.c index 67575418e5..4c1c760efc 100644 --- a/docs/examples/multi-legacy.c +++ b/docs/examples/multi-legacy.c @@ -31,8 +31,10 @@ #include /* somewhat unix-specific */ +#ifndef _WIN32 #include #include +#endif /* curl stuff */ #include @@ -147,7 +149,8 @@ int main(void) } /* See how the transfers went */ - while((msg = curl_multi_info_read(multi_handle, &msgs_left))) { + /* !checksrc! disable EQUALSNULL 1 */ + while((msg = curl_multi_info_read(multi_handle, &msgs_left)) != NULL) { if(msg->msg == CURLMSG_DONE) { int idx; diff --git a/docs/examples/multi-post.c b/docs/examples/multi-post.c index 112f3c0cd6..886f3d2600 100644 --- a/docs/examples/multi-post.c +++ b/docs/examples/multi-post.c @@ -28,7 +28,9 @@ #include #include +#ifndef _WIN32 #include +#endif #include diff --git a/docs/examples/multi-single.c b/docs/examples/multi-single.c index f5077850b3..79841bf89a 100644 --- a/docs/examples/multi-single.c +++ b/docs/examples/multi-single.c @@ -30,8 +30,10 @@ #include /* somewhat unix-specific */ +#ifndef _WIN32 #include #include +#endif /* curl stuff */ #include diff --git a/docs/examples/persistent.c b/docs/examples/persistent.c index 4be7d6f2af..86488666cd 100644 --- a/docs/examples/persistent.c +++ b/docs/examples/persistent.c @@ -26,7 +26,10 @@ * */ #include +#ifndef _WIN32 #include +#endif + #include int main(void) diff --git a/docs/examples/sepheaders.c b/docs/examples/sepheaders.c index 0d090ed815..8679b13519 100644 --- a/docs/examples/sepheaders.c +++ b/docs/examples/sepheaders.c @@ -27,7 +27,9 @@ */ #include #include +#ifndef _WIN32 #include +#endif #include diff --git a/docs/examples/url2file.c b/docs/examples/url2file.c index f95d57464b..5bfe26fe06 100644 --- a/docs/examples/url2file.c +++ b/docs/examples/url2file.c @@ -27,7 +27,9 @@ */ #include #include +#ifndef _WIN32 #include +#endif #include diff --git a/docs/examples/websocket.c b/docs/examples/websocket.c index 4487b50098..996f2a0245 100644 --- a/docs/examples/websocket.c +++ b/docs/examples/websocket.c @@ -27,7 +27,13 @@ */ #include #include +#ifdef _WIN32 +#include +#define sleep(s) Sleep((DWORD)(s)) +#else #include +#endif + #include static int ping(CURL *curl, const char *send_payload)