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

@ -38,60 +38,65 @@ static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
int main(void)
{
CURLcode res;
CURL *curl_handle;
static const char *headerfilename = "head.out";
FILE *headerfile;
static const char *bodyfilename = "body.out";
FILE *bodyfile;
curl_global_init(CURL_GLOBAL_ALL);
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
/* init the curl session */
curl_handle = curl_easy_init();
if(curl_handle) {
static const char *headerfilename = "head.out";
FILE *headerfile;
static const char *bodyfilename = "body.out";
FILE *bodyfile;
/* set URL to get */
curl_easy_setopt(curl_handle, CURLOPT_URL, "https://example.com");
/* set URL to get */
curl_easy_setopt(curl_handle, CURLOPT_URL, "https://example.com");
/* no progress meter please */
curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
/* no progress meter please */
curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
/* open the header file */
headerfile = fopen(headerfilename, "wb");
if(!headerfile) {
curl_easy_cleanup(curl_handle);
return -1;
}
/* open the header file */
headerfile = fopen(headerfilename, "wb");
if(!headerfile) {
curl_easy_cleanup(curl_handle);
curl_global_cleanup();
return -1;
}
/* open the body file */
bodyfile = fopen(bodyfilename, "wb");
if(!bodyfile) {
curl_easy_cleanup(curl_handle);
/* open the body file */
bodyfile = fopen(bodyfilename, "wb");
if(!bodyfile) {
curl_easy_cleanup(curl_handle);
fclose(headerfile);
curl_global_cleanup();
return -1;
}
/* we want the headers be written to this file handle */
curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, headerfile);
/* we want the body be written to this file handle instead of stdout */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, bodyfile);
/* get it! */
res = curl_easy_perform(curl_handle);
/* close the header file */
fclose(headerfile);
return -1;
/* close the body file */
fclose(bodyfile);
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
}
/* we want the headers be written to this file handle */
curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, headerfile);
/* we want the body be written to this file handle instead of stdout */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, bodyfile);
/* get it! */
res = curl_easy_perform(curl_handle);
/* close the header file */
fclose(headerfile);
/* close the body file */
fclose(bodyfile);
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
curl_global_cleanup();
return (int)res;