examples: improve global init, error checks and returning errors

- add `curl_global_init()` and `curl_global_cleanup()` where missing.
- check the result of `curl_global_init()` where missing.
- return the last curl error from `main()`.
- drop Win32-specific socket initialization in favor of `curl_global_init()`.
- rename some outliers to `res` for curl result code.
- fix cleanup in some error cases.

Inspired by Joshua's report on examples.

Closes #19053
This commit is contained in:
Viktor Szakats 2025-10-13 16:30:18 +02:00
parent 3049c8e0a0
commit 4c7507daf9
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
129 changed files with 990 additions and 485 deletions

View file

@ -83,6 +83,10 @@ int main(void)
const char *request = "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n";
size_t request_len = strlen(request);
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
/* A general note of caution here: if you are using curl_easy_recv() or
curl_easy_send() to implement HTTP or _any_ other protocol libcurl
supports "natively", you are doing it wrong and you should stop.
@ -93,7 +97,6 @@ int main(void)
curl = curl_easy_init();
if(curl) {
CURLcode res;
curl_socket_t sockfd;
size_t nsent_total = 0;
@ -175,5 +178,6 @@ int main(void)
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}