]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
examples: fix compiling with MSVC
authorViktor Szakats <commit@vsz.me>
Fri, 24 May 2024 16:38:32 +0000 (18:38 +0200)
committerViktor Szakats <commit@vsz.me>
Sun, 26 May 2024 20:43:04 +0000 (22:43 +0200)
- `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

17 files changed:
docs/examples/10-at-a-time.c
docs/examples/externalsocket.c
docs/examples/http2-download.c
docs/examples/http2-pushinmemory.c
docs/examples/http2-serverpush.c
docs/examples/http2-upload.c
docs/examples/multi-app.c
docs/examples/multi-debugcallback.c
docs/examples/multi-double.c
docs/examples/multi-formadd.c
docs/examples/multi-legacy.c
docs/examples/multi-post.c
docs/examples/multi-single.c
docs/examples/persistent.c
docs/examples/sepheaders.c
docs/examples/url2file.c
docs/examples/websocket.c

index a622410fd327efedbb5a7d6c46281a8dc594bf32..1221126e3680ea45f11d9ba847feaee31e33d942 100644 (file)
@@ -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;
index 21e9a9c477d16292afa5fc98fb5fa6bc4fefc672..010654298d4358bded0d08a6a10e188f1d2758c1 100644 (file)
  * Pass in a custom socket for libcurl to use.
  * </DESC>
  */
+#ifdef _WIN32
+#ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
+#define _WINSOCK_DEPRECATED_NO_WARNINGS  /* for inet_addr() */
+#endif
+#endif
+
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <curl/curl.h>
 
 #ifdef _WIN32
-#include <winsock2.h>
 #define close closesocket
 #else
 #include <sys/types.h>        /*  socket types              */
 #include <sys/socket.h>       /*  socket definitions        */
 #include <netinet/in.h>
-#include <arpa/inet.h>        /*  inet (3) functions         */
+#include <arpa/inet.h>        /*  inet (3) functions        */
 #include <unistd.h>           /*  misc. Unix functions      */
 #endif
 
index c45a1c2d651a9d06bbb949be1f9eae6d4ea7b483..3889b0d8a4354c815a78640c9b3b93ba3fee288f 100644 (file)
 #include <errno.h>
 
 /* somewhat unix-specific */
+#ifndef _WIN32
 #include <sys/time.h>
 #include <unistd.h>
+#endif
 
 /* curl stuff */
 #include <curl/curl.h>
index 74c64e0a69e6fd95421d7643fcd3e9b858caa8b1..cc1422d5a04151ec924a706f5515d6a082145805 100644 (file)
 #include <string.h>
 
 /* somewhat unix-specific */
+#ifndef _WIN32
 #include <sys/time.h>
 #include <unistd.h>
+#endif
 
 /* curl stuff */
 #include <curl/curl.h>
index e830aa95d4c5d52584502a9de246b3e13c162a30..a03ff4efbb3ebe301978f8ebac2a7296d2ed03ef 100644 (file)
 #include <string.h>
 
 /* somewhat unix-specific */
+#ifndef _WIN32
 #include <sys/time.h>
 #include <unistd.h>
+#endif
 
 /* curl stuff */
 #include <curl/curl.h>
index 56d0c982504766898ac3558aa7dc903edfe93d31..4758a2fac39d32d2c358a7e502ff40eab29f4b38 100644 (file)
 #include <errno.h>
 
 /* somewhat unix-specific */
+#ifndef _MSC_VER
 #include <sys/time.h>
 #include <unistd.h>
+#endif
 
 /* curl stuff */
 #include <curl/curl.h>
 
 #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 */
index 63918f182e7224745aeb4df956565a9b1fc53647..95c8981ac59f1eb13b2b1aef70580f51cfbff15d 100644 (file)
 #include <string.h>
 
 /* somewhat unix-specific */
+#ifndef _WIN32
 #include <sys/time.h>
 #include <unistd.h>
+#endif
 
 /* curl stuff */
 #include <curl/curl.h>
@@ -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;
 
index 2feb214d50455183bd1ebd35ae833da1d31ff7f4..68f30e9b03d65addd1c07cad1fc8364376eafb49 100644 (file)
 #include <string.h>
 
 /* somewhat unix-specific */
+#ifndef _WIN32
 #include <sys/time.h>
 #include <unistd.h>
+#endif
 
 /* curl stuff */
 #include <curl/curl.h>
index 62295a92cf54656352262d4e0834bb39e5fbd10b..b59b665fd4bc39583f7fc9b41e82212293b09ec3 100644 (file)
 #include <string.h>
 
 /* somewhat unix-specific */
+#ifndef _WIN32
 #include <sys/time.h>
 #include <unistd.h>
+#endif
 
 /* curl stuff */
 #include <curl/curl.h>
index 801b61e6dd5815b745993c661bdc17ec1af5a9a5..1f2d6d6d27d246ab580b604d1dff142719b4d0ef 100644 (file)
@@ -33,7 +33,9 @@
 
 #include <stdio.h>
 #include <string.h>
+#ifndef _WIN32
 #include <sys/time.h>
+#endif
 
 #include <curl/curl.h>
 
index 67575418e520879b212b2f616e83cc5fe2896b54..4c1c760efcca4bc80b449e7f6cfa64e013ca5c01 100644 (file)
 #include <string.h>
 
 /* somewhat unix-specific */
+#ifndef _WIN32
 #include <sys/time.h>
 #include <unistd.h>
+#endif
 
 /* curl stuff */
 #include <curl/curl.h>
@@ -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;
 
index 112f3c0cd6f261da7e31319dcf239301eba5c192..886f3d2600f340031bfaabd42abd2a2d8b9eba1b 100644 (file)
@@ -28,7 +28,9 @@
 
 #include <stdio.h>
 #include <string.h>
+#ifndef _WIN32
 #include <sys/time.h>
+#endif
 
 #include <curl/curl.h>
 
index f5077850b30ae1322aa89fabf6dd763a543aafa4..79841bf89ad83c9e9cb50ac1f45f98660a20d537 100644 (file)
 #include <string.h>
 
 /* somewhat unix-specific */
+#ifndef _WIN32
 #include <sys/time.h>
 #include <unistd.h>
+#endif
 
 /* curl stuff */
 #include <curl/curl.h>
index 4be7d6f2af7f7569e0d2cb121a6783ec8fa2adf0..86488666cd188e6ded6405f30371134494adccbe 100644 (file)
  * </DESC>
  */
 #include <stdio.h>
+#ifndef _WIN32
 #include <unistd.h>
+#endif
+
 #include <curl/curl.h>
 
 int main(void)
index 0d090ed8159ef4750cceb8bf09d8fe49e6661a4c..8679b135195c604c7a367807ac753fe9a53d1965 100644 (file)
@@ -27,7 +27,9 @@
  */
 #include <stdio.h>
 #include <stdlib.h>
+#ifndef _WIN32
 #include <unistd.h>
+#endif
 
 #include <curl/curl.h>
 
index f95d57464b69c1ce4aefa16a2643b0939e506cbf..5bfe26fe067c679d79cc4e54c6e85d8554aef811 100644 (file)
@@ -27,7 +27,9 @@
  */
 #include <stdio.h>
 #include <stdlib.h>
+#ifndef _WIN32
 #include <unistd.h>
+#endif
 
 #include <curl/curl.h>
 
index 4487b50098e03bfdd61f20df88a55b05c397333e..996f2a024548646c44fcfef1c0da17a0274edb3e 100644 (file)
  */
 #include <stdio.h>
 #include <string.h>
+#ifdef _WIN32
+#include <windows.h>
+#define sleep(s) Sleep((DWORD)(s))
+#else
 #include <unistd.h>
+#endif
+
 #include <curl/curl.h>
 
 static int ping(CURL *curl, const char *send_payload)