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

@ -263,7 +263,7 @@ static void parseHtml(const std::string &html,
int main(int argc, char *argv[])
{
CURL *conn = NULL;
CURLcode code;
CURLcode res;
std::string title;
// Ensure one argument is given
@ -273,21 +273,24 @@ int main(int argc, char *argv[])
return EXIT_FAILURE;
}
curl_global_init(CURL_GLOBAL_DEFAULT);
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
// Initialize CURL connection
if(!init(conn, argv[1])) {
fprintf(stderr, "Connection initialization failed\n");
curl_global_cleanup();
return EXIT_FAILURE;
}
// Retrieve content for the URL
code = curl_easy_perform(conn);
res = curl_easy_perform(conn);
curl_easy_cleanup(conn);
if(code != CURLE_OK) {
if(res != CURLE_OK) {
fprintf(stderr, "Failed to get '%s' [%s]\n", argv[1], errorBuffer);
return EXIT_FAILURE;
}
@ -298,5 +301,5 @@ int main(int argc, char *argv[])
// Display the extracted title
printf("Title: %s\n", title.c_str());
return EXIT_SUCCESS;
return (int)res;
}