examples: check more errors, fix cleanups, scope variables

Inspired by Joshua's report on examples.

Closes #19055
This commit is contained in:
Viktor Szakats 2025-10-13 22:57:01 +02:00
parent 61dcb56743
commit 64ed2ea196
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
42 changed files with 874 additions and 785 deletions

View file

@ -38,10 +38,10 @@ static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
int main(int argc, char *argv[])
{
static const char *pagefilename = "page.out";
CURLcode res;
CURL *curl_handle;
static const char *pagefilename = "page.out";
FILE *pagefile;
if(argc < 2) {
printf("Usage: %s <URL>\n", argv[0]);
@ -56,36 +56,39 @@ int main(int argc, char *argv[])
/* init the curl session */
curl_handle = curl_easy_init();
if(curl_handle) {
FILE *pagefile;
/* set URL to get here */
curl_easy_setopt(curl_handle, CURLOPT_URL, argv[1]);
/* set URL to get here */
curl_easy_setopt(curl_handle, CURLOPT_URL, argv[1]);
/* Switch on full protocol/debug output while testing */
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
/* Switch on full protocol/debug output while testing */
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
/* disable progress meter, set to 0L to enable it */
curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
/* disable progress meter, set to 0L to enable it */
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 file */
pagefile = fopen(pagefilename, "wb");
if(pagefile) {
/* open the file */
pagefile = fopen(pagefilename, "wb");
if(pagefile) {
/* write the page body to this file handle */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);
/* write the page body to this file handle */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);
/* get it! */
res = curl_easy_perform(curl_handle);
/* get it! */
res = curl_easy_perform(curl_handle);
/* close the header file */
fclose(pagefile);
/* close the header file */
fclose(pagefile);
}
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
}
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
curl_global_cleanup();
return (int)res;