From: Viktor Szakats Date: Mon, 13 Oct 2025 14:30:18 +0000 (+0200) Subject: examples: improve global init, error checks and returning errors X-Git-Tag: rc-8_17_0-2~87 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=4c7507daf913c2e7f3ff96b1ff92e467a3d2ece5;p=thirdparty%2Fcurl.git 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 --- diff --git a/docs/examples/10-at-a-time.c b/docs/examples/10-at-a-time.c index 38a0f24ac6..48e0030040 100644 --- a/docs/examples/10-at-a-time.c +++ b/docs/examples/10-at-a-time.c @@ -109,7 +109,10 @@ int main(void) int msgs_left = -1; int left = 0; - curl_global_init(CURL_GLOBAL_ALL); + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + cm = curl_multi_init(); /* Limit the amount of simultaneous connections curl should allow: */ diff --git a/docs/examples/address-scope.c b/docs/examples/address-scope.c index 43e9cf8986..82607e2df5 100644 --- a/docs/examples/address-scope.c +++ b/docs/examples/address-scope.c @@ -37,7 +37,10 @@ int main(void) #if !defined(_WIN32) && !defined(MSDOS) && !defined(__AMIGA__) /* Windows/MS-DOS users need to find how to use if_nametoindex() */ CURL *curl; - CURLcode res; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -57,6 +60,9 @@ int main(void) /* always cleanup */ curl_easy_cleanup(curl); } -#endif + curl_global_cleanup(); + return (int)res; +#else return 0; +#endif } diff --git a/docs/examples/altsvc.c b/docs/examples/altsvc.c index 8a1ae8ed3b..4021c90002 100644 --- a/docs/examples/altsvc.c +++ b/docs/examples/altsvc.c @@ -31,7 +31,10 @@ int main(void) { CURL *curl; - CURLcode res; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -54,5 +57,6 @@ int main(void) /* always cleanup */ curl_easy_cleanup(curl); } - return 0; + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/anyauthput.c b/docs/examples/anyauthput.c index 505d16b217..81bb1fb907 100644 --- a/docs/examples/anyauthput.c +++ b/docs/examples/anyauthput.c @@ -111,7 +111,11 @@ int main(int argc, char **argv) #endif /* In Windows, this inits the Winsock stuff */ - curl_global_init(CURL_GLOBAL_ALL); + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) { + fclose(fp); + return (int)res; + } /* get a curl handle */ curl = curl_easy_init(); @@ -161,5 +165,5 @@ int main(int argc, char **argv) fclose(fp); /* close the local file */ curl_global_cleanup(); - return 0; + return (int)res; } diff --git a/docs/examples/block_ip.c b/docs/examples/block_ip.c index 181cd8270d..b99fab58c3 100644 --- a/docs/examples/block_ip.c +++ b/docs/examples/block_ip.c @@ -301,9 +301,10 @@ int main(void) if(!filter) return 1; - if(curl_global_init(CURL_GLOBAL_DEFAULT)) { + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) { free(filter); - return 1; + return (int)res; } curl = curl_easy_init(); diff --git a/docs/examples/cacertinmem.c b/docs/examples/cacertinmem.c index e552ce5d69..d72dc49001 100644 --- a/docs/examples/cacertinmem.c +++ b/docs/examples/cacertinmem.c @@ -56,8 +56,6 @@ static size_t writefunction(void *ptr, size_t size, size_t nmemb, void *stream) static CURLcode sslctx_function(CURL *curl, void *sslctx, void *pointer) { - CURLcode rv = CURLE_ABORTED_BY_CALLBACK; - /** This example uses two (fake) certificates **/ /* replace the XXX with the actual CA certificates */ static const char mypem[] = @@ -89,14 +87,14 @@ static CURLcode sslctx_function(CURL *curl, void *sslctx, void *pointer) (void)pointer; if(!cts || !cbio) { - return rv; + return CURLE_ABORTED_BY_CALLBACK; } inf = PEM_X509_INFO_read_bio(cbio, NULL, NULL, NULL); if(!inf) { BIO_free(cbio); - return rv; + return CURLE_ABORTED_BY_CALLBACK; } for(i = 0; i < sk_X509_INFO_num(inf); i++) { @@ -112,16 +110,18 @@ static CURLcode sslctx_function(CURL *curl, void *sslctx, void *pointer) sk_X509_INFO_pop_free(inf, X509_INFO_free); BIO_free(cbio); - rv = CURLE_OK; - return rv; + return CURLE_OK; } int main(void) { CURL *ch; - CURLcode rv; + CURLcode res; + + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; - curl_global_init(CURL_GLOBAL_ALL); ch = curl_easy_init(); curl_easy_setopt(ch, CURLOPT_VERBOSE, 0L); curl_easy_setopt(ch, CURLOPT_HEADER, 0L); @@ -145,8 +145,8 @@ int main(void) /* first try: retrieve page without ca certificates -> should fail * unless libcurl was built --with-ca-fallback enabled at build-time */ - rv = curl_easy_perform(ch); - if(rv == CURLE_OK) + res = curl_easy_perform(ch); + if(res == CURLE_OK) printf("*** transfer succeeded ***\n"); else printf("*** transfer failed ***\n"); @@ -166,13 +166,13 @@ int main(void) * "modifications" to the SSL CONTEXT just before link init */ curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, sslctx_function); - rv = curl_easy_perform(ch); - if(rv == CURLE_OK) + res = curl_easy_perform(ch); + if(res == CURLE_OK) printf("*** transfer succeeded ***\n"); else printf("*** transfer failed ***\n"); curl_easy_cleanup(ch); curl_global_cleanup(); - return (int)rv; + return (int)res; } diff --git a/docs/examples/certinfo.c b/docs/examples/certinfo.c index 795be6c3d1..0443aa42f4 100644 --- a/docs/examples/certinfo.c +++ b/docs/examples/certinfo.c @@ -41,7 +41,9 @@ int main(void) CURL *curl; CURLcode res; - curl_global_init(CURL_GLOBAL_DEFAULT); + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -83,5 +85,5 @@ int main(void) curl_global_cleanup(); - return 0; + return (int)res; } diff --git a/docs/examples/chkspeed.c b/docs/examples/chkspeed.c index 32bd92005d..d4b2abbab8 100644 --- a/docs/examples/chkspeed.c +++ b/docs/examples/chkspeed.c @@ -156,7 +156,9 @@ int main(int argc, char *argv[]) } /* init libcurl */ - curl_global_init(CURL_GLOBAL_ALL); + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; /* init the curl session */ curl_handle = curl_easy_init(); diff --git a/docs/examples/connect-to.c b/docs/examples/connect-to.c index ad1e304649..253c531c42 100644 --- a/docs/examples/connect-to.c +++ b/docs/examples/connect-to.c @@ -30,8 +30,12 @@ int main(void) { + struct curl_slist *host; CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; /* Each single string should be written using the format @@ -41,8 +45,7 @@ int main(void) */ /* instead of curl.se:443, it resolves and uses example.com:443 but in other aspects work as if it still is curl.se */ - struct curl_slist *host = curl_slist_append(NULL, - "curl.se:443:example.com:443"); + host = curl_slist_append(NULL, "curl.se:443:example.com:443"); curl = curl_easy_init(); if(curl) { @@ -66,5 +69,7 @@ int main(void) curl_slist_free_all(host); + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/cookie_interface.c b/docs/examples/cookie_interface.c index cb68977121..cebbd3cdcf 100644 --- a/docs/examples/cookie_interface.c +++ b/docs/examples/cookie_interface.c @@ -72,7 +72,10 @@ main(void) CURL *curl; CURLcode res; - curl_global_init(CURL_GLOBAL_ALL); + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + curl = curl_easy_init(); if(curl) { char nline[512]; diff --git a/docs/examples/crawler.c b/docs/examples/crawler.c index 6b737652d4..a7ca5fa2fe 100644 --- a/docs/examples/crawler.c +++ b/docs/examples/crawler.c @@ -182,10 +182,14 @@ int main(void) int pending; int complete; int still_running; + CURLcode res; + + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; signal(SIGINT, sighandler); LIBXML_TEST_VERSION - curl_global_init(CURL_GLOBAL_DEFAULT); multi_handle = curl_multi_init(); curl_multi_setopt(multi_handle, CURLMOPT_MAX_TOTAL_CONNECTIONS, max_con); curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, 6L); diff --git a/docs/examples/debug.c b/docs/examples/debug.c index 5303c833f5..85fdea95d3 100644 --- a/docs/examples/debug.c +++ b/docs/examples/debug.c @@ -128,6 +128,10 @@ int main(void) CURLcode res; struct data config; + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + config.trace_ascii = 1; /* enable ASCII tracing */ curl = curl_easy_init(); @@ -151,5 +155,6 @@ int main(void) /* always cleanup */ curl_easy_cleanup(curl); } - return 0; + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/default-scheme.c b/docs/examples/default-scheme.c index 13e1e08fcb..b4ea5fa3bf 100644 --- a/docs/examples/default-scheme.c +++ b/docs/examples/default-scheme.c @@ -33,6 +33,10 @@ int main(void) CURL *curl; CURLcode res; + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_URL, "example.com"); @@ -53,5 +57,6 @@ int main(void) /* always cleanup */ curl_easy_cleanup(curl); } - return 0; + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/ephiperfifo.c b/docs/examples/ephiperfifo.c index 3fddb2b743..c22a360101 100644 --- a/docs/examples/ephiperfifo.c +++ b/docs/examples/ephiperfifo.c @@ -449,6 +449,7 @@ void sigint_handler(int signo) int main(int argc, char **argv) { + CURLcode res; struct GlobalInfo g; struct itimerspec its; struct epoll_event ev; @@ -456,6 +457,10 @@ int main(int argc, char **argv) (void)argc; (void)argv; + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + g_should_exit_ = 0; signal(SIGINT, sigint_handler); @@ -463,12 +468,14 @@ int main(int argc, char **argv) g.epfd = epoll_create1(EPOLL_CLOEXEC); if(g.epfd == -1) { perror("epoll_create1 failed"); + curl_global_cleanup(); return 1; } g.tfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); if(g.tfd == -1) { perror("timerfd_create failed"); + curl_global_cleanup(); return 1; } @@ -481,8 +488,10 @@ int main(int argc, char **argv) ev.data.fd = g.tfd; epoll_ctl(g.epfd, EPOLL_CTL_ADD, g.tfd, &ev); - if(init_fifo(&g)) + if(init_fifo(&g)) { + curl_global_cleanup(); return 1; + } g.multi = curl_multi_init(); /* setup the generic multi interface options we want */ @@ -508,6 +517,7 @@ int main(int argc, char **argv) } else { perror("epoll_wait"); + curl_global_cleanup(); return 1; } } @@ -530,5 +540,6 @@ int main(int argc, char **argv) curl_multi_cleanup(g.multi); clean_fifo(&g); + curl_global_cleanup(); return 0; } diff --git a/docs/examples/evhiperfifo.c b/docs/examples/evhiperfifo.c index 364a0f42fe..79dfd34521 100644 --- a/docs/examples/evhiperfifo.c +++ b/docs/examples/evhiperfifo.c @@ -415,10 +415,15 @@ static int init_fifo(struct GlobalInfo *g) int main(int argc, char **argv) { + CURLcode res; struct GlobalInfo g; (void)argc; (void)argv; + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + memset(&g, 0, sizeof(g)); g.loop = ev_default_loop(0); @@ -439,5 +444,6 @@ int main(int argc, char **argv) ev_loop(g.loop, 0); curl_multi_cleanup(g.multi); + curl_global_cleanup(); return 0; } diff --git a/docs/examples/externalsocket.c b/docs/examples/externalsocket.c index 84c9ba4eb2..99e719b745 100644 --- a/docs/examples/externalsocket.c +++ b/docs/examples/externalsocket.c @@ -103,14 +103,9 @@ int main(void) struct sockaddr_in servaddr; /* socket address structure */ curl_socket_t sockfd; -#ifdef _WIN32 - WSADATA wsaData; - int initwsa = WSAStartup(MAKEWORD(2, 2), &wsaData); - if(initwsa) { - printf("WSAStartup failed: %d\n", initwsa); - return 1; - } -#endif + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -175,8 +170,7 @@ int main(void) } } -#ifdef _WIN32 - WSACleanup(); -#endif + curl_global_cleanup(); + return 0; } diff --git a/docs/examples/fileupload.c b/docs/examples/fileupload.c index 29c3c3c3c1..f827c68390 100644 --- a/docs/examples/fileupload.c +++ b/docs/examples/fileupload.c @@ -46,9 +46,15 @@ int main(void) curl_off_t speed_upload, total_time; FILE *fd; + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + fd = fopen("debugit", "rb"); /* open file to upload */ - if(!fd) + if(!fd) { + curl_global_cleanup(); return 1; /* cannot continue */ + } /* to get the file size */ #ifdef UNDER_CE @@ -58,6 +64,7 @@ int main(void) if(fstat(fileno(fd), &file_info) != 0) { #endif fclose(fd); + curl_global_cleanup(); return 1; /* cannot continue */ } @@ -100,5 +107,6 @@ int main(void) curl_easy_cleanup(curl); } fclose(fd); + curl_global_cleanup(); return 0; } diff --git a/docs/examples/ftp-delete.c b/docs/examples/ftp-delete.c index b879da48d3..f5c553ff68 100644 --- a/docs/examples/ftp-delete.c +++ b/docs/examples/ftp-delete.c @@ -37,14 +37,15 @@ static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream) return size * nmemb; } - int main(void) { CURL *curl; CURLcode res; struct curl_slist *headerlist = NULL; - curl_global_init(CURL_GLOBAL_DEFAULT); + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -80,5 +81,5 @@ int main(void) curl_global_cleanup(); - return 0; + return (int)res; } diff --git a/docs/examples/ftp-wildcard.c b/docs/examples/ftp-wildcard.c index ddc6e4aae4..0861f2f048 100644 --- a/docs/examples/ftp-wildcard.c +++ b/docs/examples/ftp-wildcard.c @@ -32,67 +32,6 @@ struct callback_data { FILE *output; }; -static long file_is_coming(struct curl_fileinfo *finfo, - void *data, - int remains); - -static long file_is_downloaded(void *data); - -static size_t write_it(char *buff, size_t size, size_t nmemb, - void *cb_data); - -int main(int argc, char **argv) -{ - /* curl easy handle */ - CURL *handle; - - /* help data */ - struct callback_data data = { 0 }; - - /* global initialization */ - CURLcode rc = curl_global_init(CURL_GLOBAL_ALL); - if(rc) - return (int)rc; - - /* initialization of easy handle */ - handle = curl_easy_init(); - if(!handle) { - curl_global_cleanup(); - return CURLE_OUT_OF_MEMORY; - } - - /* turn on wildcard matching */ - curl_easy_setopt(handle, CURLOPT_WILDCARDMATCH, 1L); - - /* callback is called before download of concrete file started */ - curl_easy_setopt(handle, CURLOPT_CHUNK_BGN_FUNCTION, file_is_coming); - - /* callback is called after data from the file have been transferred */ - curl_easy_setopt(handle, CURLOPT_CHUNK_END_FUNCTION, file_is_downloaded); - - /* this callback writes contents into files */ - curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_it); - - /* put transfer data into callbacks */ - curl_easy_setopt(handle, CURLOPT_CHUNK_DATA, &data); - curl_easy_setopt(handle, CURLOPT_WRITEDATA, &data); - - /* curl_easy_setopt(handle, CURLOPT_VERBOSE, 1L); */ - - /* set a URL containing wildcard pattern (only in the last part) */ - if(argc == 2) - curl_easy_setopt(handle, CURLOPT_URL, argv[1]); - else - curl_easy_setopt(handle, CURLOPT_URL, "ftp://example.com/test/*"); - - /* and start transfer! */ - rc = curl_easy_perform(handle); - - curl_easy_cleanup(handle); - curl_global_cleanup(); - return (int)rc; -} - static long file_is_coming(struct curl_fileinfo *finfo, void *input, int remains) { @@ -151,3 +90,55 @@ static size_t write_it(char *buff, size_t size, size_t nmemb, written = fwrite(buff, size, nmemb, stdout); return written; } + +int main(int argc, char **argv) +{ + /* curl easy handle */ + CURL *handle; + + /* help data */ + struct callback_data data = { 0 }; + + /* global initialization */ + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + + /* initialization of easy handle */ + handle = curl_easy_init(); + if(!handle) { + curl_global_cleanup(); + return CURLE_OUT_OF_MEMORY; + } + + /* turn on wildcard matching */ + curl_easy_setopt(handle, CURLOPT_WILDCARDMATCH, 1L); + + /* callback is called before download of concrete file started */ + curl_easy_setopt(handle, CURLOPT_CHUNK_BGN_FUNCTION, file_is_coming); + + /* callback is called after data from the file have been transferred */ + curl_easy_setopt(handle, CURLOPT_CHUNK_END_FUNCTION, file_is_downloaded); + + /* this callback writes contents into files */ + curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_it); + + /* put transfer data into callbacks */ + curl_easy_setopt(handle, CURLOPT_CHUNK_DATA, &data); + curl_easy_setopt(handle, CURLOPT_WRITEDATA, &data); + + /* curl_easy_setopt(handle, CURLOPT_VERBOSE, 1L); */ + + /* set a URL containing wildcard pattern (only in the last part) */ + if(argc == 2) + curl_easy_setopt(handle, CURLOPT_URL, argv[1]); + else + curl_easy_setopt(handle, CURLOPT_URL, "ftp://example.com/test/*"); + + /* and start transfer! */ + res = curl_easy_perform(handle); + + curl_easy_cleanup(handle); + curl_global_cleanup(); + return (int)res; +} diff --git a/docs/examples/ftpget.c b/docs/examples/ftpget.c index 95369c1c02..f4ba1e52aa 100644 --- a/docs/examples/ftpget.c +++ b/docs/examples/ftpget.c @@ -57,7 +57,9 @@ int main(void) NULL }; - curl_global_init(CURL_GLOBAL_DEFAULT); + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -90,5 +92,5 @@ int main(void) curl_global_cleanup(); - return 0; + return (int)res; } diff --git a/docs/examples/ftpgetinfo.c b/docs/examples/ftpgetinfo.c index 485b26bddc..549d3003a8 100644 --- a/docs/examples/ftpgetinfo.c +++ b/docs/examples/ftpgetinfo.c @@ -49,7 +49,9 @@ int main(void) curl_off_t filesize = 0; const char *filename = strrchr(ftpurl, '/') + 1; - curl_global_init(CURL_GLOBAL_DEFAULT); + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -89,5 +91,5 @@ int main(void) curl_global_cleanup(); - return 0; + return (int)res; } diff --git a/docs/examples/ftpgetresp.c b/docs/examples/ftpgetresp.c index 7b40f77c94..7b6a6e3d2d 100644 --- a/docs/examples/ftpgetresp.c +++ b/docs/examples/ftpgetresp.c @@ -47,15 +47,22 @@ int main(void) FILE *ftpfile; FILE *respfile; + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + /* local filename to store the file as */ ftpfile = fopen(FTPBODY, "wb"); /* b is binary, needed on Windows */ - if(!ftpfile) + if(!ftpfile) { + curl_global_cleanup(); return 1; + } /* local filename to store the FTP server's response lines in */ respfile = fopen(FTPHEADERS, "wb"); /* b is binary, needed on Windows */ if(!respfile) { fclose(ftpfile); + curl_global_cleanup(); return 1; } @@ -81,5 +88,5 @@ int main(void) fclose(ftpfile); /* close the local file */ fclose(respfile); /* close the response file */ - return 0; + return (int)res; } diff --git a/docs/examples/ftpsget.c b/docs/examples/ftpsget.c index dfe80b9f8b..c2aee89c84 100644 --- a/docs/examples/ftpsget.c +++ b/docs/examples/ftpsget.c @@ -59,7 +59,9 @@ int main(void) NULL }; - curl_global_init(CURL_GLOBAL_DEFAULT); + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -97,5 +99,5 @@ int main(void) curl_global_cleanup(); - return 0; + return (int)res; } diff --git a/docs/examples/ftpupload.c b/docs/examples/ftpupload.c index 4f3b679226..7588a82953 100644 --- a/docs/examples/ftpupload.c +++ b/docs/examples/ftpupload.c @@ -109,7 +109,11 @@ int main(void) printf("Local file size: %lu bytes.\n", (unsigned long)fsize); /* In Windows, this inits the Winsock stuff */ - curl_global_init(CURL_GLOBAL_ALL); + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) { + fclose(hd_src); + return (int)res; + } /* get a curl handle */ curl = curl_easy_init(); @@ -155,5 +159,5 @@ int main(void) fclose(hd_src); /* close the local file */ curl_global_cleanup(); - return 0; + return (int)res; } diff --git a/docs/examples/ftpuploadfrommem.c b/docs/examples/ftpuploadfrommem.c index 3748d68a05..70242376ae 100644 --- a/docs/examples/ftpuploadfrommem.c +++ b/docs/examples/ftpuploadfrommem.c @@ -122,5 +122,5 @@ int main(void) curl_easy_cleanup(curl); } curl_global_cleanup(); - return 0; + return (int)res; } diff --git a/docs/examples/ftpuploadresume.c b/docs/examples/ftpuploadresume.c index 67495aec68..544b746873 100644 --- a/docs/examples/ftpuploadresume.c +++ b/docs/examples/ftpuploadresume.c @@ -154,7 +154,10 @@ int main(void) { CURL *curlhandle = NULL; - curl_global_init(CURL_GLOBAL_ALL); + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + curlhandle = curl_easy_init(); upload(curlhandle, "ftp://user:pass@example.com/path/file", "C:\\file", diff --git a/docs/examples/getinfo.c b/docs/examples/getinfo.c index 9c178c2c8c..d6257afa79 100644 --- a/docs/examples/getinfo.c +++ b/docs/examples/getinfo.c @@ -31,7 +31,10 @@ int main(void) { CURL *curl; - CURLcode res; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -50,5 +53,6 @@ int main(void) /* always cleanup */ curl_easy_cleanup(curl); } - return 0; + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/getinmemory.c b/docs/examples/getinmemory.c index 173247d915..589047097b 100644 --- a/docs/examples/getinmemory.c +++ b/docs/examples/getinmemory.c @@ -66,11 +66,13 @@ int main(void) struct MemoryStruct chunk; + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + chunk.memory = malloc(1); /* grown as needed by the realloc above */ chunk.size = 0; /* no data at this point */ - curl_global_init(CURL_GLOBAL_ALL); - /* init the curl session */ curl_handle = curl_easy_init(); @@ -114,5 +116,5 @@ int main(void) /* we are done with libcurl, so clean it up */ curl_global_cleanup(); - return 0; + return (int)res; } diff --git a/docs/examples/getredirect.c b/docs/examples/getredirect.c index 91c778d3c1..908cd5f850 100644 --- a/docs/examples/getredirect.c +++ b/docs/examples/getredirect.c @@ -35,6 +35,10 @@ int main(void) char *location; long response_code; + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_URL, "https://example.com"); @@ -68,5 +72,6 @@ int main(void) /* always cleanup */ curl_easy_cleanup(curl); } - return 0; + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/getreferrer.c b/docs/examples/getreferrer.c index c46f7825a0..ae42f4605f 100644 --- a/docs/examples/getreferrer.c +++ b/docs/examples/getreferrer.c @@ -32,10 +32,12 @@ int main(void) { CURL *curl; + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + curl = curl_easy_init(); if(curl) { - CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "https://example.com"); curl_easy_setopt(curl, CURLOPT_REFERER, "https://example.org/referrer"); @@ -55,5 +57,6 @@ int main(void) /* always cleanup */ curl_easy_cleanup(curl); } + curl_global_cleanup(); return 0; } diff --git a/docs/examples/ghiper.c b/docs/examples/ghiper.c index 7d8449cdf0..50308fd47c 100644 --- a/docs/examples/ghiper.c +++ b/docs/examples/ghiper.c @@ -421,9 +421,15 @@ int main(void) int fd; GIOChannel* ch; + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + fd = init_fifo(); - if(fd == CURL_SOCKET_BAD) + if(fd == CURL_SOCKET_BAD) { + curl_global_cleanup(); return 1; + } ch = g_io_channel_unix_new(fd); g_io_add_watch(ch, G_IO_IN, fifo_cb, g); gmain = g_main_loop_new(NULL, FALSE); @@ -438,5 +444,6 @@ int main(void) g_main_loop_run(gmain); curl_multi_cleanup(g->multi); + curl_global_cleanup(); return 0; } diff --git a/docs/examples/headerapi.c b/docs/examples/headerapi.c index ede0c5cbda..428374bac6 100644 --- a/docs/examples/headerapi.c +++ b/docs/examples/headerapi.c @@ -40,9 +40,12 @@ int main(void) { CURL *curl; + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + curl = curl_easy_init(); if(curl) { - CURLcode res; struct curl_header *header; curl_easy_setopt(curl, CURLOPT_URL, "https://example.com"); /* example.com is redirected, so we tell libcurl to follow redirection */ @@ -77,5 +80,6 @@ int main(void) /* always cleanup */ curl_easy_cleanup(curl); } + curl_global_cleanup(); return 0; } diff --git a/docs/examples/hiperfifo.c b/docs/examples/hiperfifo.c index 1af1eb0c72..cc22e70abb 100644 --- a/docs/examples/hiperfifo.c +++ b/docs/examples/hiperfifo.c @@ -414,14 +414,21 @@ static void clean_fifo(struct GlobalInfo *g) int main(int argc, char **argv) { + CURLcode res; struct GlobalInfo g; (void)argc; (void)argv; + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + memset(&g, 0, sizeof(g)); g.evbase = event_base_new(); - if(init_fifo(&g)) + if(init_fifo(&g)) { + curl_global_cleanup(); return 1; + } g.multi = curl_multi_init(); evtimer_assign(&g.timer_event, g.evbase, timer_cb, &g); @@ -443,5 +450,6 @@ int main(int argc, char **argv) event_del(&g.timer_event); event_base_free(g.evbase); curl_multi_cleanup(g.multi); + curl_global_cleanup(); return 0; } diff --git a/docs/examples/hsts-preload.c b/docs/examples/hsts-preload.c index ba9fe87a94..303b17f525 100644 --- a/docs/examples/hsts-preload.c +++ b/docs/examples/hsts-preload.c @@ -80,7 +80,10 @@ static CURLSTScode hstswrite(CURL *easy, struct curl_hstsentry *e, int main(void) { CURL *curl; - CURLcode res; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -114,5 +117,6 @@ int main(void) /* always cleanup */ curl_easy_cleanup(curl); } - return 0; + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/htmltidy.c b/docs/examples/htmltidy.c index 498bb85bdc..952132fbc4 100644 --- a/docs/examples/htmltidy.c +++ b/docs/examples/htmltidy.c @@ -76,55 +76,58 @@ void dumpNode(TidyDoc doc, TidyNode tnod, int indent) int main(int argc, char **argv) { - if(argc == 2) { - CURL *curl; - char curl_errbuf[CURL_ERROR_SIZE]; - TidyDoc tdoc; - TidyBuffer docbuf = {0}; - TidyBuffer tidy_errbuf = {0}; - int err; + CURL *curl; + char curl_errbuf[CURL_ERROR_SIZE]; + TidyDoc tdoc; + TidyBuffer docbuf = {0}; + TidyBuffer tidy_errbuf = {0}; + CURLcode res; - curl = curl_easy_init(); - curl_easy_setopt(curl, CURLOPT_URL, argv[1]); - curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errbuf); - curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); - curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb); + if(argc != 2) { + printf("usage: %s \n", argv[0]); + return 1; + } + + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + + curl = curl_easy_init(); + curl_easy_setopt(curl, CURLOPT_URL, argv[1]); + curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errbuf); + curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); + curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb); - tdoc = tidyCreate(); - tidyOptSetBool(tdoc, TidyForceOutput, yes); /* try harder */ - tidyOptSetInt(tdoc, TidyWrapLen, 4096); - tidySetErrorBuffer(tdoc, &tidy_errbuf); - tidyBufInit(&docbuf); + tdoc = tidyCreate(); + tidyOptSetBool(tdoc, TidyForceOutput, yes); /* try harder */ + tidyOptSetInt(tdoc, TidyWrapLen, 4096); + tidySetErrorBuffer(tdoc, &tidy_errbuf); + tidyBufInit(&docbuf); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &docbuf); - err = curl_easy_perform(curl); - if(!err) { - err = tidyParseBuffer(tdoc, &docbuf); /* parse the input */ - if(err >= 0) { - err = tidyCleanAndRepair(tdoc); /* fix any problems */ - if(err >= 0) { - err = tidyRunDiagnostics(tdoc); /* load tidy error buffer */ - if(err >= 0) { - dumpNode(tdoc, tidyGetRoot(tdoc), 0); /* walk the tree */ - fprintf(stderr, "%s\n", tidy_errbuf.bp); /* show errors */ - } + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &docbuf); + res = curl_easy_perform(curl); + if(!res) { + res = tidyParseBuffer(tdoc, &docbuf); /* parse the input */ + if(res >= 0) { + res = tidyCleanAndRepair(tdoc); /* fix any problems */ + if(res >= 0) { + res = tidyRunDiagnostics(tdoc); /* load tidy error buffer */ + if(res >= 0) { + dumpNode(tdoc, tidyGetRoot(tdoc), 0); /* walk the tree */ + fprintf(stderr, "%s\n", tidy_errbuf.bp); /* show errors */ } } } - else - fprintf(stderr, "%s\n", curl_errbuf); - - /* clean-up */ - curl_easy_cleanup(curl); - tidyBufFree(&docbuf); - tidyBufFree(&tidy_errbuf); - tidyRelease(tdoc); - return err; - } else - printf("usage: %s \n", argv[0]); + fprintf(stderr, "%s\n", curl_errbuf); - return 0; + /* clean-up */ + curl_easy_cleanup(curl); + curl_global_cleanup(); + tidyBufFree(&docbuf); + tidyBufFree(&tidy_errbuf); + tidyRelease(tdoc); + return (int)res; } diff --git a/docs/examples/htmltitle.cpp b/docs/examples/htmltitle.cpp index 8d75f588fc..e4979ee859 100644 --- a/docs/examples/htmltitle.cpp +++ b/docs/examples/htmltitle.cpp @@ -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; } diff --git a/docs/examples/http-options.c b/docs/examples/http-options.c index 586b55f12a..9520670bae 100644 --- a/docs/examples/http-options.c +++ b/docs/examples/http-options.c @@ -31,7 +31,10 @@ int main(void) { CURL *curl; - CURLcode res; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -55,5 +58,6 @@ int main(void) /* always cleanup */ curl_easy_cleanup(curl); } + curl_global_cleanup(); return 0; } diff --git a/docs/examples/http-post.c b/docs/examples/http-post.c index 901ee1e3f0..fb91822bb8 100644 --- a/docs/examples/http-post.c +++ b/docs/examples/http-post.c @@ -34,7 +34,9 @@ int main(void) CURLcode res; /* In Windows, this inits the Winsock stuff */ - curl_global_init(CURL_GLOBAL_ALL); + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; /* get a curl handle */ curl = curl_easy_init(); @@ -57,5 +59,5 @@ int main(void) curl_easy_cleanup(curl); } curl_global_cleanup(); - return 0; + return (int)res; } diff --git a/docs/examples/http2-download.c b/docs/examples/http2-download.c index a524cac27f..6343234667 100644 --- a/docs/examples/http2-download.c +++ b/docs/examples/http2-download.c @@ -190,11 +190,13 @@ static int setup(struct transfer *t, int num) */ int main(int argc, char **argv) { + CURLcode res; struct transfer trans[NUM_HANDLES]; CURLM *multi_handle; int i; int still_running = 0; /* keep number of running handles */ int num_transfers; + if(argc > 1) { /* if given a number, do that many transfers */ num_transfers = atoi(argv[1]); @@ -204,12 +206,18 @@ int main(int argc, char **argv) else num_transfers = 3; /* suitable default */ + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + /* init a multi stack */ multi_handle = curl_multi_init(); for(i = 0; i < num_transfers; i++) { - if(setup(&trans[i], i)) + if(setup(&trans[i], i)) { + curl_global_cleanup(); return 1; + } /* add the individual transfer */ curl_multi_add_handle(multi_handle, trans[i].easy); diff --git a/docs/examples/http2-pushinmemory.c b/docs/examples/http2-pushinmemory.c index 873883ca50..3e2b4af1c2 100644 --- a/docs/examples/http2-pushinmemory.c +++ b/docs/examples/http2-pushinmemory.c @@ -130,6 +130,10 @@ int main(void) int i; struct CURLMsg *m; + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + /* init a multi stack */ multi = curl_multi_init(); @@ -155,7 +159,6 @@ int main(void) if(mcode) break; - /* * When doing server push, libcurl itself created and added one or more * easy handles but *we* need to clean them up when they are done. @@ -173,8 +176,8 @@ int main(void) } - curl_multi_cleanup(multi); + curl_global_cleanup(); /* 'pushindex' is now the number of received transfers */ for(i = 0; i < pushindex; i++) { diff --git a/docs/examples/http2-serverpush.c b/docs/examples/http2-serverpush.c index cea54e3af1..0d0e991776 100644 --- a/docs/examples/http2-serverpush.c +++ b/docs/examples/http2-serverpush.c @@ -212,6 +212,7 @@ static int server_push_callback(CURL *parent, */ int main(int argc, char *argv[]) { + CURLcode res; CURL *easy; CURLM *multi_handle; int transfers = 1; /* we start with one */ @@ -221,6 +222,10 @@ int main(int argc, char *argv[]) if(argc == 2) url = argv[1]; + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + /* init a multi stack */ multi_handle = curl_multi_init(); @@ -229,6 +234,7 @@ int main(int argc, char *argv[]) /* set options */ if(setup(easy, url)) { fprintf(stderr, "failed\n"); + curl_global_cleanup(); return 1; } @@ -270,7 +276,7 @@ int main(int argc, char *argv[]) } while(transfers); /* as long as we have transfers going */ curl_multi_cleanup(multi_handle); - + curl_global_cleanup(); return 0; } diff --git a/docs/examples/http2-upload.c b/docs/examples/http2-upload.c index a22161d76d..9cc3c5652c 100644 --- a/docs/examples/http2-upload.c +++ b/docs/examples/http2-upload.c @@ -292,6 +292,7 @@ static int setup(struct input *i, int num, const char *upload) */ int main(int argc, char **argv) { + CURLcode res; struct input trans[NUM_HANDLES]; CURLM *multi_handle; int i; @@ -313,12 +314,18 @@ int main(int argc, char **argv) else num_transfers = 3; + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + /* init a multi stack */ multi_handle = curl_multi_init(); for(i = 0; i < num_transfers; i++) { - if(setup(&trans[i], i, filename)) + if(setup(&trans[i], i, filename)) { + curl_global_cleanup(); return 1; + } /* add the individual transfer */ curl_multi_add_handle(multi_handle, trans[i].hnd); @@ -348,5 +355,7 @@ int main(int argc, char **argv) curl_easy_cleanup(trans[i].hnd); } + curl_global_cleanup(); + return 0; } diff --git a/docs/examples/http3-present.c b/docs/examples/http3-present.c index 56ba0f5724..084f265c46 100644 --- a/docs/examples/http3-present.c +++ b/docs/examples/http3-present.c @@ -32,7 +32,9 @@ int main(void) { curl_version_info_data *ver; - curl_global_init(CURL_GLOBAL_ALL); + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; ver = curl_version_info(CURLVERSION_NOW); if(ver->features & CURL_VERSION_HTTP2) diff --git a/docs/examples/http3.c b/docs/examples/http3.c index 573ea20d2b..323f6d7d17 100644 --- a/docs/examples/http3.c +++ b/docs/examples/http3.c @@ -31,7 +31,10 @@ int main(void) { CURL *curl; - CURLcode res; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -50,5 +53,6 @@ int main(void) /* always cleanup */ curl_easy_cleanup(curl); } + curl_global_cleanup(); return 0; } diff --git a/docs/examples/httpcustomheader.c b/docs/examples/httpcustomheader.c index a3881674c1..0657313ddf 100644 --- a/docs/examples/httpcustomheader.c +++ b/docs/examples/httpcustomheader.c @@ -31,7 +31,10 @@ int main(void) { CURL *curl; - CURLcode res; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -68,5 +71,6 @@ int main(void) /* free the custom headers */ curl_slist_free_all(chunk); } - return 0; + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/httpput-postfields.c b/docs/examples/httpput-postfields.c index 5f410837b8..b855f454ae 100644 --- a/docs/examples/httpput-postfields.c +++ b/docs/examples/httpput-postfields.c @@ -58,7 +58,9 @@ int main(int argc, char **argv) url = argv[1]; /* In Windows, this inits the Winsock stuff */ - curl_global_init(CURL_GLOBAL_ALL); + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; /* get a curl handle */ curl = curl_easy_init(); @@ -100,5 +102,5 @@ int main(int argc, char **argv) } curl_global_cleanup(); - return 0; + return (int)res; } diff --git a/docs/examples/httpput.c b/docs/examples/httpput.c index 1951cb232e..2aef62fc67 100644 --- a/docs/examples/httpput.c +++ b/docs/examples/httpput.c @@ -101,7 +101,11 @@ int main(int argc, char **argv) } /* In Windows, this inits the Winsock stuff */ - curl_global_init(CURL_GLOBAL_ALL); + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) { + fclose(hd_src); + return (int)res; + } /* get a curl handle */ curl = curl_easy_init(); @@ -137,5 +141,5 @@ int main(int argc, char **argv) fclose(hd_src); /* close the local file */ curl_global_cleanup(); - return 0; + return (int)res; } diff --git a/docs/examples/https.c b/docs/examples/https.c index c1cba877df..23729afcaa 100644 --- a/docs/examples/https.c +++ b/docs/examples/https.c @@ -31,9 +31,10 @@ int main(void) { CURL *curl; - CURLcode res; - curl_global_init(CURL_GLOBAL_DEFAULT); + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -79,5 +80,5 @@ int main(void) curl_global_cleanup(); - return 0; + return (int)res; } diff --git a/docs/examples/imap-append.c b/docs/examples/imap-append.c index 1839deac1d..e8851ce977 100644 --- a/docs/examples/imap-append.c +++ b/docs/examples/imap-append.c @@ -87,7 +87,10 @@ static size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp) int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -126,5 +129,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/imap-authzid.c b/docs/examples/imap-authzid.c index eb615c6f3d..b7c2e43f99 100644 --- a/docs/examples/imap-authzid.c +++ b/docs/examples/imap-authzid.c @@ -39,7 +39,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -69,5 +72,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/imap-copy.c b/docs/examples/imap-copy.c index a221be0ca8..8adb8b8c6d 100644 --- a/docs/examples/imap-copy.c +++ b/docs/examples/imap-copy.c @@ -39,7 +39,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -69,5 +72,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/imap-create.c b/docs/examples/imap-create.c index 6a9b565345..51fbe5f142 100644 --- a/docs/examples/imap-create.c +++ b/docs/examples/imap-create.c @@ -39,7 +39,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -65,5 +68,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/imap-delete.c b/docs/examples/imap-delete.c index e43ab2e982..a7682f7664 100644 --- a/docs/examples/imap-delete.c +++ b/docs/examples/imap-delete.c @@ -39,7 +39,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -65,5 +68,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/imap-examine.c b/docs/examples/imap-examine.c index 34217bfa42..a46d450d21 100644 --- a/docs/examples/imap-examine.c +++ b/docs/examples/imap-examine.c @@ -39,7 +39,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -65,5 +68,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/imap-fetch.c b/docs/examples/imap-fetch.c index 416fe88096..937d3e05b8 100644 --- a/docs/examples/imap-fetch.c +++ b/docs/examples/imap-fetch.c @@ -39,7 +39,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -63,5 +66,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/imap-list.c b/docs/examples/imap-list.c index 0253b543e6..2d882503d0 100644 --- a/docs/examples/imap-list.c +++ b/docs/examples/imap-list.c @@ -39,7 +39,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -64,5 +67,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/imap-lsub.c b/docs/examples/imap-lsub.c index cf45a5fc2d..74472d42ec 100644 --- a/docs/examples/imap-lsub.c +++ b/docs/examples/imap-lsub.c @@ -39,7 +39,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -66,5 +69,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/imap-multi.c b/docs/examples/imap-multi.c index 42fa7381cd..e2d5dd4e9c 100644 --- a/docs/examples/imap-multi.c +++ b/docs/examples/imap-multi.c @@ -39,44 +39,48 @@ int main(void) { CURL *curl; - CURLM *mcurl; - int still_running = 1; - curl_global_init(CURL_GLOBAL_DEFAULT); + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); - if(!curl) - return 1; + if(curl) { + CURLM *mcurl; - mcurl = curl_multi_init(); - if(!mcurl) - return 2; + mcurl = curl_multi_init(); + if(mcurl) { + int still_running = 1; - /* Set username and password */ - curl_easy_setopt(curl, CURLOPT_USERNAME, "user"); - curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret"); + /* Set username and password */ + curl_easy_setopt(curl, CURLOPT_USERNAME, "user"); + curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret"); - /* This fetches message 1 from the user's inbox */ - curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com/INBOX/;UID=1"); + /* This fetches message 1 from the user's inbox */ + curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com/" + "INBOX/;UID=1"); - /* Tell the multi stack about our easy handle */ - curl_multi_add_handle(mcurl, curl); + /* Tell the multi stack about our easy handle */ + curl_multi_add_handle(mcurl, curl); - do { - CURLMcode mc = curl_multi_perform(mcurl, &still_running); + do { + CURLMcode mc = curl_multi_perform(mcurl, &still_running); - if(still_running) - /* wait for activity, timeout or "nothing" */ - mc = curl_multi_poll(mcurl, NULL, 0, 1000, NULL); + if(still_running) + /* wait for activity, timeout or "nothing" */ + mc = curl_multi_poll(mcurl, NULL, 0, 1000, NULL); - if(mc) - break; - } while(still_running); + if(mc) + break; + } while(still_running); + + /* Always cleanup */ + curl_multi_remove_handle(mcurl, curl); + curl_multi_cleanup(mcurl); + } + curl_easy_cleanup(curl); + } - /* Always cleanup */ - curl_multi_remove_handle(mcurl, curl); - curl_multi_cleanup(mcurl); - curl_easy_cleanup(curl); curl_global_cleanup(); return 0; diff --git a/docs/examples/imap-noop.c b/docs/examples/imap-noop.c index 9e5a3da2d5..1d8607590f 100644 --- a/docs/examples/imap-noop.c +++ b/docs/examples/imap-noop.c @@ -39,7 +39,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -65,5 +68,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/imap-search.c b/docs/examples/imap-search.c index 141b06f649..b4e1576b38 100644 --- a/docs/examples/imap-search.c +++ b/docs/examples/imap-search.c @@ -39,7 +39,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -69,5 +72,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/imap-ssl.c b/docs/examples/imap-ssl.c index e632c30e86..59edd130e5 100644 --- a/docs/examples/imap-ssl.c +++ b/docs/examples/imap-ssl.c @@ -40,7 +40,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -90,5 +93,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/imap-store.c b/docs/examples/imap-store.c index d04a6072cc..95d8f07476 100644 --- a/docs/examples/imap-store.c +++ b/docs/examples/imap-store.c @@ -39,7 +39,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -80,5 +83,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/imap-tls.c b/docs/examples/imap-tls.c index 8fbc96bb55..9009174ccf 100644 --- a/docs/examples/imap-tls.c +++ b/docs/examples/imap-tls.c @@ -40,7 +40,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -90,5 +93,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/interface.c b/docs/examples/interface.c index f1a2016ced..0698b6a8e2 100644 --- a/docs/examples/interface.c +++ b/docs/examples/interface.c @@ -31,7 +31,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -48,5 +51,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/ipv6.c b/docs/examples/ipv6.c index 1b698705d0..1a55d64049 100644 --- a/docs/examples/ipv6.c +++ b/docs/examples/ipv6.c @@ -31,7 +31,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -44,5 +47,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/keepalive.c b/docs/examples/keepalive.c index e06d7ff37b..8d55c71337 100644 --- a/docs/examples/keepalive.c +++ b/docs/examples/keepalive.c @@ -31,7 +31,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -54,5 +57,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/localport.c b/docs/examples/localport.c index 7e88ce48a7..2700457211 100644 --- a/docs/examples/localport.c +++ b/docs/examples/localport.c @@ -31,7 +31,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -49,5 +52,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/log_failed_transfers.c b/docs/examples/log_failed_transfers.c index 66e1931677..fe5f02f882 100644 --- a/docs/examples/log_failed_transfers.c +++ b/docs/examples/log_failed_transfers.c @@ -207,6 +207,7 @@ static size_t mywrite(char *ptr, size_t size, size_t nmemb, void *userdata) int main(void) { + CURLcode res; unsigned i; int total_failed = 0; char errbuf[CURL_ERROR_SIZE] = { 0, }; @@ -222,9 +223,10 @@ int main(void) transfer[1].bodyfile = "400.txt"; transfer[1].logfile = "400_transfer_log.txt"; - if(curl_global_init(CURL_GLOBAL_DEFAULT)) { + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) { fprintf(stderr, "curl_global_init failed\n"); - return 1; + return (int)res; } /* You could enable global tracing for extra verbosity when verbosity is @@ -334,5 +336,7 @@ int main(void) printf("\n"); } + curl_global_cleanup(); + return total_failed ? 1 : 0; } diff --git a/docs/examples/maxconnects.c b/docs/examples/maxconnects.c index 2e8e5b50a8..e89f971cf4 100644 --- a/docs/examples/maxconnects.c +++ b/docs/examples/maxconnects.c @@ -31,7 +31,10 @@ int main(void) { CURL *curl; - CURLcode res; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -62,5 +65,8 @@ int main(void) /* always cleanup */ curl_easy_cleanup(curl); } + + curl_global_cleanup(); + return 0; } diff --git a/docs/examples/multi-app.c b/docs/examples/multi-app.c index 5bbf580f82..8deee68370 100644 --- a/docs/examples/multi-app.c +++ b/docs/examples/multi-app.c @@ -52,6 +52,10 @@ int main(void) CURLMsg *msg; /* for picking up messages with the transfer status */ int msgs_left; /* how many messages are left */ + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + /* Allocate one curl handle per transfer */ for(i = 0; i < HANDLECOUNT; i++) handles[i] = curl_easy_init(); @@ -110,6 +114,7 @@ int main(void) } curl_multi_cleanup(multi_handle); + curl_global_cleanup(); return 0; } diff --git a/docs/examples/multi-debugcallback.c b/docs/examples/multi-debugcallback.c index 738732279d..a27c238a57 100644 --- a/docs/examples/multi-debugcallback.c +++ b/docs/examples/multi-debugcallback.c @@ -127,6 +127,10 @@ int main(void) int still_running = 0; /* keep number of running handles */ + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + http_handle = curl_easy_init(); /* set the options (I left out a few, you get the point anyway) */ @@ -157,5 +161,7 @@ int main(void) curl_easy_cleanup(http_handle); + curl_global_cleanup(); + return 0; } diff --git a/docs/examples/multi-double.c b/docs/examples/multi-double.c index 99bd736a9c..8c5c5d687a 100644 --- a/docs/examples/multi-double.c +++ b/docs/examples/multi-double.c @@ -42,6 +42,10 @@ int main(void) int still_running = 1; /* keep number of running handles */ + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + http_handle = curl_easy_init(); http_handle2 = curl_easy_init(); @@ -89,5 +93,7 @@ int main(void) curl_easy_cleanup(http_handle); curl_easy_cleanup(http_handle2); + curl_global_cleanup(); + return 0; } diff --git a/docs/examples/multi-event.c b/docs/examples/multi-event.c index 23dff05ed2..62dc73b33f 100644 --- a/docs/examples/multi-event.c +++ b/docs/examples/multi-event.c @@ -219,12 +219,15 @@ static int handle_socket(CURL *easy, curl_socket_t s, int action, void *userp, int main(int argc, char **argv) { + CURLcode res; + if(argc <= 1) return 0; - if(curl_global_init(CURL_GLOBAL_ALL)) { + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) { fprintf(stderr, "Could not init curl\n"); - return 1; + return (int)res; } base = event_base_new(); diff --git a/docs/examples/multi-formadd.c b/docs/examples/multi-formadd.c index 58c7e641c4..84312ffd16 100644 --- a/docs/examples/multi-formadd.c +++ b/docs/examples/multi-formadd.c @@ -48,6 +48,10 @@ int main(void) struct curl_slist *headerlist = NULL; static const char buf[] = "Expect:"; + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + CURL_IGNORE_DEPRECATION( /* Fill in the file upload field. This makes libcurl load data from the given file name when curl_easy_perform() is called. */ @@ -116,5 +120,6 @@ int main(void) /* free slist */ curl_slist_free_all(headerlist); } + curl_global_cleanup(); return 0; } diff --git a/docs/examples/multi-legacy.c b/docs/examples/multi-legacy.c index b0c37ea8d5..63c238f247 100644 --- a/docs/examples/multi-legacy.c +++ b/docs/examples/multi-legacy.c @@ -58,6 +58,10 @@ int main(void) CURLMsg *msg; /* for picking up messages with the transfer status */ int msgs_left; /* how many messages are left */ + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + /* Allocate one curl handle per transfer */ for(i = 0; i < HANDLECOUNT; i++) handles[i] = curl_easy_init(); @@ -187,5 +191,7 @@ int main(void) for(i = 0; i < HANDLECOUNT; i++) curl_easy_cleanup(handles[i]); + curl_global_cleanup(); + return 0; } diff --git a/docs/examples/multi-post.c b/docs/examples/multi-post.c index 84af48f4bf..45416b8511 100644 --- a/docs/examples/multi-post.c +++ b/docs/examples/multi-post.c @@ -43,6 +43,10 @@ int main(void) struct curl_slist *headerlist = NULL; static const char buf[] = "Expect:"; + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + curl = curl_easy_init(); multi_handle = curl_multi_init(); @@ -100,5 +104,6 @@ int main(void) /* free slist */ curl_slist_free_all(headerlist); } + curl_global_cleanup(); return 0; } diff --git a/docs/examples/multi-single.c b/docs/examples/multi-single.c index 0ead96f487..3ba26e79db 100644 --- a/docs/examples/multi-single.c +++ b/docs/examples/multi-single.c @@ -41,7 +41,9 @@ int main(void) CURLM *multi_handle; int still_running = 1; /* keep number of running handles */ - curl_global_init(CURL_GLOBAL_DEFAULT); + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; http_handle = curl_easy_init(); diff --git a/docs/examples/multi-uv.c b/docs/examples/multi-uv.c index cc27668d57..b4f5587c0f 100644 --- a/docs/examples/multi-uv.c +++ b/docs/examples/multi-uv.c @@ -226,13 +226,16 @@ static int cb_socket(CURL *easy, curl_socket_t s, int action, int main(int argc, char **argv) { + CURLcode res; struct datauv uv = { 0 }; int running_handles; if(argc <= 1) return 0; - curl_global_init(CURL_GLOBAL_ALL); + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; uv.loop = uv_default_loop(); uv_timer_init(uv.loop, &uv.timeout); @@ -251,6 +254,7 @@ int main(int argc, char **argv) curl_multi_socket_action(uv.multi, CURL_SOCKET_TIMEOUT, 0, &running_handles); uv_run(uv.loop, UV_RUN_DEFAULT); curl_multi_cleanup(uv.multi); + curl_global_cleanup(); curl_global_cleanup(); diff --git a/docs/examples/multithread.c b/docs/examples/multithread.c index a47758062f..5818dfebee 100644 --- a/docs/examples/multithread.c +++ b/docs/examples/multithread.c @@ -72,11 +72,14 @@ static void *pull_one_url(void *pindex) int main(void) { + CURLcode res; pthread_t tid[NUMT]; int i; /* Must initialize libcurl before any threads are started */ - curl_global_init(CURL_GLOBAL_ALL); + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; for(i = 0; i < NUMT; i++) { int error = pthread_create(&tid[i], diff --git a/docs/examples/netrc.c b/docs/examples/netrc.c index 42e1b6341e..148a7e422b 100644 --- a/docs/examples/netrc.c +++ b/docs/examples/netrc.c @@ -31,7 +31,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -45,5 +48,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/persistent.c b/docs/examples/persistent.c index ffcf343fbf..7cde055d69 100644 --- a/docs/examples/persistent.c +++ b/docs/examples/persistent.c @@ -32,9 +32,10 @@ int main(void) { CURL *curl; - CURLcode res; - curl_global_init(CURL_GLOBAL_ALL); + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -68,5 +69,5 @@ int main(void) curl_global_cleanup(); - return 0; + return (int)res; } diff --git a/docs/examples/pop3-authzid.c b/docs/examples/pop3-authzid.c index 3281b322bb..8cfe80bca2 100644 --- a/docs/examples/pop3-authzid.c +++ b/docs/examples/pop3-authzid.c @@ -39,7 +39,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -68,5 +71,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/pop3-dele.c b/docs/examples/pop3-dele.c index fe3795c245..84592da4f9 100644 --- a/docs/examples/pop3-dele.c +++ b/docs/examples/pop3-dele.c @@ -39,7 +39,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -68,5 +71,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/pop3-list.c b/docs/examples/pop3-list.c index 2cd44e41cd..05ecc66233 100644 --- a/docs/examples/pop3-list.c +++ b/docs/examples/pop3-list.c @@ -39,7 +39,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -62,5 +65,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/pop3-multi.c b/docs/examples/pop3-multi.c index 54eb7ecc32..4d418a7d09 100644 --- a/docs/examples/pop3-multi.c +++ b/docs/examples/pop3-multi.c @@ -38,46 +38,50 @@ int main(void) { + CURLcode res; CURL *curl; - CURLM *mcurl; - int still_running = 1; - curl_global_init(CURL_GLOBAL_DEFAULT); + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); - if(!curl) - return 1; + if(curl) { + CURLM *mcurl; - mcurl = curl_multi_init(); - if(!mcurl) - return 2; + mcurl = curl_multi_init(); + if(mcurl) { + int still_running = 1; - /* Set username and password */ - curl_easy_setopt(curl, CURLOPT_USERNAME, "user"); - curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret"); + /* Set username and password */ + curl_easy_setopt(curl, CURLOPT_USERNAME, "user"); + curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret"); - /* This retrieves message 1 from the user's mailbox */ - curl_easy_setopt(curl, CURLOPT_URL, "pop3://pop.example.com/1"); + /* This retrieves message 1 from the user's mailbox */ + curl_easy_setopt(curl, CURLOPT_URL, "pop3://pop.example.com/1"); - /* Tell the multi stack about our easy handle */ - curl_multi_add_handle(mcurl, curl); + /* Tell the multi stack about our easy handle */ + curl_multi_add_handle(mcurl, curl); - do { - CURLMcode mc = curl_multi_perform(mcurl, &still_running); + do { + CURLMcode mc = curl_multi_perform(mcurl, &still_running); - if(still_running) - /* wait for activity, timeout or "nothing" */ - mc = curl_multi_poll(mcurl, NULL, 0, 1000, NULL); + if(still_running) + /* wait for activity, timeout or "nothing" */ + mc = curl_multi_poll(mcurl, NULL, 0, 1000, NULL); - if(mc) - break; + if(mc) + break; - } while(still_running); + } while(still_running); + + /* Always cleanup */ + curl_multi_remove_handle(mcurl, curl); + curl_multi_cleanup(mcurl); + } + curl_easy_cleanup(curl); + } - /* Always cleanup */ - curl_multi_remove_handle(mcurl, curl); - curl_multi_cleanup(mcurl); - curl_easy_cleanup(curl); curl_global_cleanup(); return 0; diff --git a/docs/examples/pop3-noop.c b/docs/examples/pop3-noop.c index 16181d2875..0ab6eb2d14 100644 --- a/docs/examples/pop3-noop.c +++ b/docs/examples/pop3-noop.c @@ -39,7 +39,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -68,5 +71,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/pop3-retr.c b/docs/examples/pop3-retr.c index 8e690f972f..c89c77e5d6 100644 --- a/docs/examples/pop3-retr.c +++ b/docs/examples/pop3-retr.c @@ -39,7 +39,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -62,5 +65,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/pop3-ssl.c b/docs/examples/pop3-ssl.c index 23dd959ea0..63a9edca70 100644 --- a/docs/examples/pop3-ssl.c +++ b/docs/examples/pop3-ssl.c @@ -40,7 +40,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -89,5 +92,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/pop3-stat.c b/docs/examples/pop3-stat.c index 419859bfa6..afaf79c9a4 100644 --- a/docs/examples/pop3-stat.c +++ b/docs/examples/pop3-stat.c @@ -39,7 +39,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -68,5 +71,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/pop3-tls.c b/docs/examples/pop3-tls.c index b2f504c475..a4a7208986 100644 --- a/docs/examples/pop3-tls.c +++ b/docs/examples/pop3-tls.c @@ -40,7 +40,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -89,5 +92,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/pop3-top.c b/docs/examples/pop3-top.c index 7ceba881b2..7584503b0c 100644 --- a/docs/examples/pop3-top.c +++ b/docs/examples/pop3-top.c @@ -39,7 +39,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -65,5 +68,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/pop3-uidl.c b/docs/examples/pop3-uidl.c index 496e5b08d4..aec1034231 100644 --- a/docs/examples/pop3-uidl.c +++ b/docs/examples/pop3-uidl.c @@ -39,7 +39,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -65,5 +68,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/post-callback.c b/docs/examples/post-callback.c index fa0c575e65..7c5a87e9ac 100644 --- a/docs/examples/post-callback.c +++ b/docs/examples/post-callback.c @@ -79,7 +79,7 @@ int main(void) if(res != CURLE_OK) { fprintf(stderr, "curl_global_init() failed: %s\n", curl_easy_strerror(res)); - return 1; + return (int)res; } /* get a curl handle */ diff --git a/docs/examples/postinmemory.c b/docs/examples/postinmemory.c index cbdc77f75c..2a8f2b6dd0 100644 --- a/docs/examples/postinmemory.c +++ b/docs/examples/postinmemory.c @@ -63,10 +63,13 @@ int main(void) struct MemoryStruct chunk; static const char *postthis = "Field=1&Field=2&Field=3"; + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + chunk.memory = malloc(1); /* grown as needed by realloc above */ chunk.size = 0; /* no data at this point */ - curl_global_init(CURL_GLOBAL_ALL); curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.org/"); diff --git a/docs/examples/postit2-formadd.c b/docs/examples/postit2-formadd.c index 88fb924750..81f8bfd27c 100644 --- a/docs/examples/postit2-formadd.c +++ b/docs/examples/postit2-formadd.c @@ -57,7 +57,9 @@ int main(int argc, char *argv[]) struct curl_slist *headerlist = NULL; static const char buf[] = "Expect:"; - curl_global_init(CURL_GLOBAL_ALL); + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; CURL_IGNORE_DEPRECATION( /* Fill in the file upload field */ diff --git a/docs/examples/postit2.c b/docs/examples/postit2.c index d16d3fccf6..c42ea812c1 100644 --- a/docs/examples/postit2.c +++ b/docs/examples/postit2.c @@ -53,7 +53,9 @@ int main(int argc, char *argv[]) struct curl_slist *headerlist = NULL; static const char buf[] = "Expect:"; - curl_global_init(CURL_GLOBAL_ALL); + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { diff --git a/docs/examples/progressfunc.c b/docs/examples/progressfunc.c index e164f03ca5..012fd1e6ae 100644 --- a/docs/examples/progressfunc.c +++ b/docs/examples/progressfunc.c @@ -70,9 +70,12 @@ static int xferinfo(void *p, int main(void) { CURL *curl; - CURLcode res = CURLE_OK; struct myprogress prog; + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + curl = curl_easy_init(); if(curl) { prog.lastruntime = 0; @@ -93,5 +96,6 @@ int main(void) /* always cleanup */ curl_easy_cleanup(curl); } + curl_global_cleanup(); return (int)res; } diff --git a/docs/examples/protofeats.c b/docs/examples/protofeats.c index 3e762218ae..fef4a3798d 100644 --- a/docs/examples/protofeats.c +++ b/docs/examples/protofeats.c @@ -37,7 +37,9 @@ int main(void) curl_version_info_data *ver; const char *const *ptr; - curl_global_init(CURL_GLOBAL_ALL); + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; ver = curl_version_info(CURLVERSION_NOW); printf("Protocols:\n"); diff --git a/docs/examples/range.c b/docs/examples/range.c index c8229fca4d..ec85ed878d 100644 --- a/docs/examples/range.c +++ b/docs/examples/range.c @@ -30,7 +30,10 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -41,5 +44,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/resolve.c b/docs/examples/resolve.c index 6514e93d8d..4ccc3ff1d8 100644 --- a/docs/examples/resolve.c +++ b/docs/examples/resolve.c @@ -32,7 +32,6 @@ int main(void) { CURL *curl; - CURLcode res = CURLE_OK; /* Each single name resolve string should be written using the format HOST:PORT:ADDRESS where HOST is the name libcurl tries to resolve, PORT @@ -42,6 +41,10 @@ int main(void) struct curl_slist *host = curl_slist_append(NULL, "example.com:443:127.0.0.1"); + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_RESOLVE, host); @@ -53,6 +56,7 @@ int main(void) } curl_slist_free_all(host); + curl_global_cleanup(); return (int)res; } diff --git a/docs/examples/rtsp-options.c b/docs/examples/rtsp-options.c index d1ddbf01ff..50d5e2f27e 100644 --- a/docs/examples/rtsp-options.c +++ b/docs/examples/rtsp-options.c @@ -31,7 +31,10 @@ int main(void) { CURL *curl; - CURLcode res; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -51,5 +54,6 @@ int main(void) /* always cleanup */ curl_easy_cleanup(curl); } + curl_global_cleanup(); return 0; } diff --git a/docs/examples/sendrecv.c b/docs/examples/sendrecv.c index 8cbb5b0722..71faeb4ad3 100644 --- a/docs/examples/sendrecv.c +++ b/docs/examples/sendrecv.c @@ -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; } diff --git a/docs/examples/sepheaders.c b/docs/examples/sepheaders.c index 097183f821..cea07fd566 100644 --- a/docs/examples/sepheaders.c +++ b/docs/examples/sepheaders.c @@ -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"); + + /* 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); + + /* 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); + 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); - /* 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); - - /* send all data to this function */ - curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data); + /* close the body file */ + fclose(bodyfile); - /* open the header file */ - headerfile = fopen(headerfilename, "wb"); - if(!headerfile) { + /* cleanup curl stuff */ curl_easy_cleanup(curl_handle); - return -1; - } - - /* open the body file */ - bodyfile = fopen(bodyfilename, "wb"); - if(!bodyfile) { - curl_easy_cleanup(curl_handle); - fclose(headerfile); - 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); - - /* close the body file */ - fclose(bodyfile); - - /* cleanup curl stuff */ - curl_easy_cleanup(curl_handle); - curl_global_cleanup(); return (int)res; diff --git a/docs/examples/sessioninfo.c b/docs/examples/sessioninfo.c index bbdcfe8d23..c7ea52ccc2 100644 --- a/docs/examples/sessioninfo.c +++ b/docs/examples/sessioninfo.c @@ -96,9 +96,9 @@ static size_t wrfu(void *ptr, size_t size, size_t nmemb, void *stream) int main(void) { - CURLcode res = CURLE_OK; - - curl_global_init(CURL_GLOBAL_DEFAULT); + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { diff --git a/docs/examples/sftpget.c b/docs/examples/sftpget.c index 4d33939a40..36653c5b82 100644 --- a/docs/examples/sftpget.c +++ b/docs/examples/sftpget.c @@ -62,13 +62,14 @@ static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, int main(void) { CURL *curl; - CURLcode res; struct FtpFile ftpfile = { "yourfile.bin", /* name to store the file as if successful */ NULL }; - curl_global_init(CURL_GLOBAL_DEFAULT); + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -108,5 +109,5 @@ int main(void) curl_global_cleanup(); - return 0; + return (int)res; } diff --git a/docs/examples/sftpuploadresume.c b/docs/examples/sftpuploadresume.c index f1f7b0c275..4bb0f3e48e 100644 --- a/docs/examples/sftpuploadresume.c +++ b/docs/examples/sftpuploadresume.c @@ -125,7 +125,10 @@ int main(void) const char *filename = "filename"; CURL *curlhandle = NULL; - curl_global_init(CURL_GLOBAL_ALL); + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + curlhandle = curl_easy_init(); if(!sftpResumeUpload(curlhandle, remote, filename)) { diff --git a/docs/examples/shared-connection-cache.c b/docs/examples/shared-connection-cache.c index dc6805a9f1..a8549d7766 100644 --- a/docs/examples/shared-connection-cache.c +++ b/docs/examples/shared-connection-cache.c @@ -51,6 +51,10 @@ int main(void) CURLSH *share; int i; + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + share = curl_share_init(); curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT); @@ -63,8 +67,6 @@ int main(void) for(i = 0; i < 3; i++) { CURL *curl = curl_easy_init(); if(curl) { - CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "https://curl.se/"); /* use the share object */ @@ -83,5 +85,6 @@ int main(void) } curl_share_cleanup(share); - return 0; + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/simple.c b/docs/examples/simple.c index 53c8e4754f..29ed14313e 100644 --- a/docs/examples/simple.c +++ b/docs/examples/simple.c @@ -31,7 +31,10 @@ int main(void) { CURL *curl; - CURLcode res; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -49,5 +52,6 @@ int main(void) /* always cleanup */ curl_easy_cleanup(curl); } + curl_global_cleanup(); return 0; } diff --git a/docs/examples/simplepost.c b/docs/examples/simplepost.c index 7ced982fe0..b1175ba924 100644 --- a/docs/examples/simplepost.c +++ b/docs/examples/simplepost.c @@ -31,10 +31,13 @@ int main(void) { + static const char *postthis = "moo mooo moo moo"; + CURL *curl; - CURLcode res; - static const char *postthis = "moo mooo moo moo"; + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -54,5 +57,6 @@ int main(void) /* always cleanup */ curl_easy_cleanup(curl); } - return 0; + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/simplessl.c b/docs/examples/simplessl.c index 220dc62bcd..5da79a79a0 100644 --- a/docs/examples/simplessl.c +++ b/docs/examples/simplessl.c @@ -77,7 +77,11 @@ int main(void) if(!headerfile) return 1; - curl_global_init(CURL_GLOBAL_DEFAULT); + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) { + fclose(headerfile); + return (int)res; + } curl = curl_easy_init(); if(curl) { diff --git a/docs/examples/smooth-gtk-thread.c b/docs/examples/smooth-gtk-thread.c index 3b615cb344..29d134db47 100644 --- a/docs/examples/smooth-gtk-thread.c +++ b/docs/examples/smooth-gtk-thread.c @@ -169,7 +169,9 @@ int main(int argc, char **argv) GtkWidget *top_window, *outside_frame, *inside_frame, *progress_bar; /* Must initialize libcurl before any threads are started */ - curl_global_init(CURL_GLOBAL_ALL); + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; /* Init thread */ g_thread_init(NULL); diff --git a/docs/examples/smtp-authzid.c b/docs/examples/smtp-authzid.c index daaeab1694..addc17691e 100644 --- a/docs/examples/smtp-authzid.c +++ b/docs/examples/smtp-authzid.c @@ -95,10 +95,13 @@ static size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp) int main(void) { CURL *curl; - CURLcode res = CURLE_OK; struct curl_slist *recipients = NULL; struct upload_status upload_ctx = { 0 }; + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + curl = curl_easy_init(); if(curl) { /* This is the URL for your mailserver. In this example we connect to the @@ -158,5 +161,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/smtp-expn.c b/docs/examples/smtp-expn.c index 202d1d089f..baec49be69 100644 --- a/docs/examples/smtp-expn.c +++ b/docs/examples/smtp-expn.c @@ -42,9 +42,12 @@ int main(void) { CURL *curl; - CURLcode res; struct curl_slist *recipients = NULL; + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + curl = curl_easy_init(); if(curl) { /* This is the URL for your mailserver */ @@ -77,5 +80,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return 0; } diff --git a/docs/examples/smtp-mail.c b/docs/examples/smtp-mail.c index 29918de7ff..6583fc4709 100644 --- a/docs/examples/smtp-mail.c +++ b/docs/examples/smtp-mail.c @@ -92,10 +92,13 @@ static size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp) int main(void) { CURL *curl; - CURLcode res = CURLE_OK; struct curl_slist *recipients = NULL; struct upload_status upload_ctx = { 0 }; + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + curl = curl_easy_init(); if(curl) { /* This is the URL for your mailserver */ @@ -146,5 +149,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/smtp-mime.c b/docs/examples/smtp-mime.c index 7a2a9c618b..d26021ef34 100644 --- a/docs/examples/smtp-mime.c +++ b/docs/examples/smtp-mime.c @@ -71,7 +71,10 @@ static const char inline_html[] = int main(void) { CURL *curl; - CURLcode res = CURLE_OK; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -164,5 +167,7 @@ int main(void) curl_mime_free(mime); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/smtp-multi.c b/docs/examples/smtp-multi.c index 8837c57fd8..f7619465ee 100644 --- a/docs/examples/smtp-multi.c +++ b/docs/examples/smtp-multi.c @@ -85,68 +85,72 @@ static size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp) int main(void) { CURL *curl; - CURLM *mcurl; - int still_running = 1; - struct curl_slist *recipients = NULL; - struct upload_status upload_ctx = { 0 }; - curl_global_init(CURL_GLOBAL_DEFAULT); + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); - if(!curl) - return 1; - - mcurl = curl_multi_init(); - if(!mcurl) - return 2; - - /* This is the URL for your mailserver */ - curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com"); - - /* Note that this option is not strictly required, omitting it results in - * libcurl sending the MAIL FROM command with empty sender data. All - * autoresponses should have an empty reverse-path, and should be directed - * to the address in the reverse-path which triggered them. Otherwise, they - * could cause an endless loop. See RFC 5321 Section 4.5.5 for more details. - */ - curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM_MAIL); - - /* Add two recipients, in this particular case they correspond to the - * To: and Cc: addressees in the header, but they could be any kind of - * recipient. */ - recipients = curl_slist_append(recipients, TO_MAIL); - recipients = curl_slist_append(recipients, CC_MAIL); - curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients); - - /* We are using a callback function to specify the payload (the headers and - * body of the message). You could just use the CURLOPT_READDATA option to - * specify a FILE pointer to read from. */ - curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source); - curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx); - curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); - - /* Tell the multi stack about our easy handle */ - curl_multi_add_handle(mcurl, curl); - - do { - CURLMcode mc = curl_multi_perform(mcurl, &still_running); - - if(still_running) - /* wait for activity, timeout or "nothing" */ - mc = curl_multi_poll(mcurl, NULL, 0, 1000, NULL); - - if(mc) - break; - - } while(still_running); - - /* Free the list of recipients */ - curl_slist_free_all(recipients); - - /* Always cleanup */ - curl_multi_remove_handle(mcurl, curl); - curl_multi_cleanup(mcurl); - curl_easy_cleanup(curl); + if(curl) { + CURLM *mcurl; + + mcurl = curl_multi_init(); + if(mcurl) { + int still_running = 1; + struct curl_slist *recipients = NULL; + struct upload_status upload_ctx = { 0 }; + + /* This is the URL for your mailserver */ + curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com"); + + /* Note that this option is not strictly required, omitting it results + * in libcurl sending the MAIL FROM command with empty sender data. All + * autoresponses should have an empty reverse-path, and should be + * directed to the address in the reverse-path which triggered them. + * Otherwise, they could cause an endless loop. See RFC 5321 Section + * 4.5.5 for more details. + */ + curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM_MAIL); + + /* Add two recipients, in this particular case they correspond to the + * To: and Cc: addressees in the header, but they could be any kind of + * recipient. */ + recipients = curl_slist_append(recipients, TO_MAIL); + recipients = curl_slist_append(recipients, CC_MAIL); + curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients); + + /* We are using a callback function to specify the payload (the headers + * and body of the message). You could just use the CURLOPT_READDATA + * option to specify a FILE pointer to read from. */ + curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source); + curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx); + curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); + + /* Tell the multi stack about our easy handle */ + curl_multi_add_handle(mcurl, curl); + + do { + CURLMcode mc = curl_multi_perform(mcurl, &still_running); + + if(still_running) + /* wait for activity, timeout or "nothing" */ + mc = curl_multi_poll(mcurl, NULL, 0, 1000, NULL); + + if(mc) + break; + + } while(still_running); + + /* Free the list of recipients */ + curl_slist_free_all(recipients); + + /* Always cleanup */ + curl_multi_remove_handle(mcurl, curl); + curl_multi_cleanup(mcurl); + } + curl_easy_cleanup(curl); + } + curl_global_cleanup(); return 0; diff --git a/docs/examples/smtp-ssl.c b/docs/examples/smtp-ssl.c index c88b4fe7ca..f952441c9c 100644 --- a/docs/examples/smtp-ssl.c +++ b/docs/examples/smtp-ssl.c @@ -89,10 +89,13 @@ static size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp) int main(void) { CURL *curl; - CURLcode res = CURLE_OK; struct curl_slist *recipients = NULL; struct upload_status upload_ctx = { 0 }; + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + curl = curl_easy_init(); if(curl) { /* Set username and password */ @@ -166,5 +169,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/smtp-tls.c b/docs/examples/smtp-tls.c index 34a00bd548..3eaea9d879 100644 --- a/docs/examples/smtp-tls.c +++ b/docs/examples/smtp-tls.c @@ -89,10 +89,13 @@ static size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp) int main(void) { CURL *curl; - CURLcode res = CURLE_OK; struct curl_slist *recipients = NULL; struct upload_status upload_ctx = { 0 }; + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + curl = curl_easy_init(); if(curl) { /* Set username and password */ @@ -169,5 +172,7 @@ int main(void) curl_easy_cleanup(curl); } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/smtp-vrfy.c b/docs/examples/smtp-vrfy.c index 0135efef2d..76dd8069ad 100644 --- a/docs/examples/smtp-vrfy.c +++ b/docs/examples/smtp-vrfy.c @@ -45,9 +45,12 @@ int main(void) { CURL *curl; - CURLcode res; struct curl_slist *recipients = NULL; + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + curl = curl_easy_init(); if(curl) { /* This is the URL for your mailserver */ @@ -77,5 +80,7 @@ int main(void) curl_easy_cleanup(curl); } - return 0; + curl_global_cleanup(); + + return (int)res; } diff --git a/docs/examples/synctime.c b/docs/examples/synctime.c index d5f5a5bfde..da2c7ea0f3 100644 --- a/docs/examples/synctime.c +++ b/docs/examples/synctime.c @@ -245,6 +245,7 @@ static int conf_init(struct conf *conf) int main(int argc, char *argv[]) { + CURLcode res; CURL *curl; struct conf conf[1]; int RetValue; @@ -285,7 +286,10 @@ int main(int argc, char *argv[]) snprintf(conf->timeserver, MAX_STRING, "%s", DefaultTimeServer[0]); /* Init CURL before usage */ - curl_global_init(CURL_GLOBAL_ALL); + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + curl = curl_easy_init(); if(curl) { struct tm *lt; diff --git a/docs/examples/threaded-ssl.c b/docs/examples/threaded-ssl.c index fb60b7f160..7f09a76fc1 100644 --- a/docs/examples/threaded-ssl.c +++ b/docs/examples/threaded-ssl.c @@ -72,13 +72,16 @@ static void *pull_one_url(void *pindex) int main(int argc, char **argv) { + CURLcode res; pthread_t tid[NUMT]; int i; (void)argc; (void)argv; /* Must initialize libcurl before any threads are started */ - curl_global_init(CURL_GLOBAL_ALL); + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; for(i = 0; i < NUMT; i++) { int error = pthread_create(&tid[i], diff --git a/docs/examples/unixsocket.c b/docs/examples/unixsocket.c index 63acd4b6a2..738ffc2c97 100644 --- a/docs/examples/unixsocket.c +++ b/docs/examples/unixsocket.c @@ -41,7 +41,10 @@ int main(void) { CURL *curl; - CURLcode res; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -63,5 +66,6 @@ int main(void) /* always cleanup */ curl_easy_cleanup(curl); } - return 0; + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/url2file.c b/docs/examples/url2file.c index c55d2a77c7..2e8e225fce 100644 --- a/docs/examples/url2file.c +++ b/docs/examples/url2file.c @@ -38,8 +38,7 @@ static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) int main(int argc, char *argv[]) { - CURLcode res = CURLE_OK; - + CURLcode res; CURL *curl_handle; static const char *pagefilename = "page.out"; FILE *pagefile; @@ -49,7 +48,11 @@ int main(int argc, char *argv[]) return 1; } - curl_global_init(CURL_GLOBAL_ALL); + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) { + fprintf(stderr, "Could not init curl\n"); + return (int)res; + } /* init the curl session */ curl_handle = curl_easy_init(); diff --git a/docs/examples/urlapi.c b/docs/examples/urlapi.c index 2ed78eb11f..8953146e4d 100644 --- a/docs/examples/urlapi.c +++ b/docs/examples/urlapi.c @@ -35,11 +35,14 @@ int main(void) { CURL *curl; - CURLcode res; CURLU *urlp; CURLUcode uc; + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + /* get a curl handle */ curl = curl_easy_init(); @@ -73,5 +76,6 @@ int main(void) cleanup: curl_url_cleanup(urlp); curl_easy_cleanup(curl); - return 0; + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/usercertinmem.c b/docs/examples/usercertinmem.c index 2a3c2b0006..d95e2a1cdd 100644 --- a/docs/examples/usercertinmem.c +++ b/docs/examples/usercertinmem.c @@ -157,7 +157,10 @@ static CURLcode sslctx_function(CURL *curl, void *sslctx, void *pointer) int main(void) { CURL *ch; - CURLcode rv; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl_global_init(CURL_GLOBAL_ALL); ch = curl_easy_init(); @@ -180,8 +183,8 @@ int main(void) curl_easy_setopt(ch, CURLOPT_SSLKEYTYPE, "PEM"); /* first try: retrieve page without user certificate and key -> fails */ - rv = curl_easy_perform(ch); - if(rv == CURLE_OK) + res = curl_easy_perform(ch); + if(res == CURLE_OK) printf("*** transfer succeeded ***\n"); else printf("*** transfer failed ***\n"); @@ -191,13 +194,13 @@ int main(void) * "modifications" to the SSL CONTEXT just before link init */ curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, sslctx_function); - rv = curl_easy_perform(ch); - if(rv == CURLE_OK) + res = curl_easy_perform(ch); + if(res == CURLE_OK) printf("*** transfer succeeded ***\n"); else printf("*** transfer failed ***\n"); curl_easy_cleanup(ch); curl_global_cleanup(); - return (int)rv; + return (int)res; } diff --git a/docs/examples/websocket-cb.c b/docs/examples/websocket-cb.c index 09d6c647dd..aa81d89ab1 100644 --- a/docs/examples/websocket-cb.c +++ b/docs/examples/websocket-cb.c @@ -44,7 +44,10 @@ static size_t writecb(char *b, size_t size, size_t nitems, void *p) int main(void) { CURL *curl; - CURLcode res; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); if(curl) { @@ -64,5 +67,6 @@ int main(void) /* always cleanup */ curl_easy_cleanup(curl); } - return 0; + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/websocket-updown.c b/docs/examples/websocket-updown.c index 0257c6077e..f95c151cac 100644 --- a/docs/examples/websocket-updown.c +++ b/docs/examples/websocket-updown.c @@ -87,39 +87,42 @@ int main(int argc, const char *argv[]) { CURL *easy; struct read_ctx rctx; - CURLcode res; const char *payload = "Hello, friend!"; + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + memset(&rctx, 0, sizeof(rctx)); easy = curl_easy_init(); - if(!easy) - return 1; - - if(argc == 2) - curl_easy_setopt(easy, CURLOPT_URL, argv[1]); - else - curl_easy_setopt(easy, CURLOPT_URL, "wss://example.com"); + if(easy) { + if(argc == 2) + curl_easy_setopt(easy, CURLOPT_URL, argv[1]); + else + curl_easy_setopt(easy, CURLOPT_URL, "wss://example.com"); - curl_easy_setopt(easy, CURLOPT_WRITEFUNCTION, writecb); - curl_easy_setopt(easy, CURLOPT_WRITEDATA, easy); - curl_easy_setopt(easy, CURLOPT_READFUNCTION, readcb); - /* tell curl that we want to send the payload */ - rctx.easy = easy; - rctx.blen = strlen(payload); - memcpy(rctx.buf, payload, rctx.blen); - curl_easy_setopt(easy, CURLOPT_READDATA, &rctx); - curl_easy_setopt(easy, CURLOPT_UPLOAD, 1L); + curl_easy_setopt(easy, CURLOPT_WRITEFUNCTION, writecb); + curl_easy_setopt(easy, CURLOPT_WRITEDATA, easy); + curl_easy_setopt(easy, CURLOPT_READFUNCTION, readcb); + /* tell curl that we want to send the payload */ + rctx.easy = easy; + rctx.blen = strlen(payload); + memcpy(rctx.buf, payload, rctx.blen); + curl_easy_setopt(easy, CURLOPT_READDATA, &rctx); + curl_easy_setopt(easy, CURLOPT_UPLOAD, 1L); - /* Perform the request, res gets the return code */ - res = curl_easy_perform(easy); - /* Check for errors */ - if(res != CURLE_OK) - fprintf(stderr, "curl_easy_perform() failed: %s\n", - curl_easy_strerror(res)); + /* Perform the request, res gets the return code */ + res = curl_easy_perform(easy); + /* Check for errors */ + if(res != CURLE_OK) + fprintf(stderr, "curl_easy_perform() failed: %s\n", + curl_easy_strerror(res)); - /* always cleanup */ - curl_easy_cleanup(easy); - return 0; + /* always cleanup */ + curl_easy_cleanup(easy); + } + curl_global_cleanup(); + return (int)res; } diff --git a/docs/examples/websocket.c b/docs/examples/websocket.c index be676839f7..be6cff64a2 100644 --- a/docs/examples/websocket.c +++ b/docs/examples/websocket.c @@ -138,31 +138,34 @@ static CURLcode websocket(CURL *curl) int main(int argc, const char *argv[]) { CURL *curl; - CURLcode res; + + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; curl = curl_easy_init(); - if(!curl) { - return 1; /* memory failure */ - } - if(argc == 2) - curl_easy_setopt(curl, CURLOPT_URL, argv[1]); - else - curl_easy_setopt(curl, CURLOPT_URL, "wss://example.com"); + if(curl) { + if(argc == 2) + curl_easy_setopt(curl, CURLOPT_URL, argv[1]); + else + curl_easy_setopt(curl, CURLOPT_URL, "wss://example.com"); - curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 2L); /* websocket style */ + curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 2L); /* websocket style */ - /* Perform the request, res gets the return code */ - res = curl_easy_perform(curl); - /* Check for errors */ - if(res != CURLE_OK) - fprintf(stderr, "curl_easy_perform() failed: %s\n", - curl_easy_strerror(res)); - else { - /* connected and ready */ - res = websocket(curl); - } + /* Perform the request, res gets the return code */ + res = curl_easy_perform(curl); + /* Check for errors */ + if(res != CURLE_OK) + fprintf(stderr, "curl_easy_perform() failed: %s\n", + curl_easy_strerror(res)); + else { + /* connected and ready */ + res = websocket(curl); + } - /* always cleanup */ - curl_easy_cleanup(curl); + /* always cleanup */ + curl_easy_cleanup(curl); + } + curl_global_cleanup(); return (int)res; } diff --git a/docs/examples/xmlstream.c b/docs/examples/xmlstream.c index 91c5387888..4f8401be4a 100644 --- a/docs/examples/xmlstream.c +++ b/docs/examples/xmlstream.c @@ -131,8 +131,11 @@ int main(void) XML_SetElementHandler(parser, startElement, endElement); XML_SetCharacterDataHandler(parser, characterDataHandler); + res = curl_global_init(CURL_GLOBAL_ALL); + if(res) + return (int)res; + /* Initialize a libcurl handle. */ - curl_global_init(CURL_GLOBAL_DEFAULT); curl_handle = curl_easy_init(); curl_easy_setopt(curl_handle, CURLOPT_URL, "https://www.w3schools.com/xml/simple.xml"); @@ -166,5 +169,5 @@ int main(void) curl_easy_cleanup(curl_handle); curl_global_cleanup(); - return 0; + return (int)res; }