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:
  e973493f99/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
This commit is contained in:
Viktor Szakats 2024-05-24 18:38:32 +02:00
parent 21eb2b55a6
commit d4b8589055
No known key found for this signature in database
GPG Key ID: B5ABD165E2AEF201
17 changed files with 68 additions and 5 deletions

View 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;

View File

@ -25,13 +25,18 @@
* 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 */

View File

@ -31,8 +31,10 @@
#include <errno.h>
/* somewhat unix-specific */
#ifndef _WIN32
#include <sys/time.h>
#include <unistd.h>
#endif
/* curl stuff */
#include <curl/curl.h>

View File

@ -30,8 +30,10 @@
#include <string.h>
/* somewhat unix-specific */
#ifndef _WIN32
#include <sys/time.h>
#include <unistd.h>
#endif
/* curl stuff */
#include <curl/curl.h>

View File

@ -30,8 +30,10 @@
#include <string.h>
/* somewhat unix-specific */
#ifndef _WIN32
#include <sys/time.h>
#include <unistd.h>
#endif
/* curl stuff */
#include <curl/curl.h>

View File

@ -33,8 +33,10 @@
#include <errno.h>
/* somewhat unix-specific */
#ifndef _MSC_VER
#include <sys/time.h>
#include <unistd.h>
#endif
/* curl stuff */
#include <curl/curl.h>
@ -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 */

View File

@ -31,8 +31,10 @@
#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;

View File

@ -30,8 +30,10 @@
#include <string.h>
/* somewhat unix-specific */
#ifndef _WIN32
#include <sys/time.h>
#include <unistd.h>
#endif
/* curl stuff */
#include <curl/curl.h>

View File

@ -29,8 +29,10 @@
#include <string.h>
/* somewhat unix-specific */
#ifndef _WIN32
#include <sys/time.h>
#include <unistd.h>
#endif
/* curl stuff */
#include <curl/curl.h>

View File

@ -33,7 +33,9 @@
#include <stdio.h>
#include <string.h>
#ifndef _WIN32
#include <sys/time.h>
#endif
#include <curl/curl.h>

View File

@ -31,8 +31,10 @@
#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;

View File

@ -28,7 +28,9 @@
#include <stdio.h>
#include <string.h>
#ifndef _WIN32
#include <sys/time.h>
#endif
#include <curl/curl.h>

View File

@ -30,8 +30,10 @@
#include <string.h>
/* somewhat unix-specific */
#ifndef _WIN32
#include <sys/time.h>
#include <unistd.h>
#endif
/* curl stuff */
#include <curl/curl.h>

View File

@ -26,7 +26,10 @@
* </DESC>
*/
#include <stdio.h>
#ifndef _WIN32
#include <unistd.h>
#endif
#include <curl/curl.h>
int main(void)

View File

@ -27,7 +27,9 @@
*/
#include <stdio.h>
#include <stdlib.h>
#ifndef _WIN32
#include <unistd.h>
#endif
#include <curl/curl.h>

View File

@ -27,7 +27,9 @@
*/
#include <stdio.h>
#include <stdlib.h>
#ifndef _WIN32
#include <unistd.h>
#endif
#include <curl/curl.h>

View File

@ -27,7 +27,13 @@
*/
#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)