From 384c8f356087178e4779d99d3e0e7f25331293aa Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Wed, 25 Oct 2006 05:59:46 +0000 Subject: Use curl_global_init() and curl_global_cleanup(). Improve cleanup in case of initialization failure. --- tests/libtest/lib505.c | 85 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 33 deletions(-) (limited to 'tests/libtest/lib505.c') diff --git a/tests/libtest/lib505.c b/tests/libtest/lib505.c index 7de0bc1e9..85ce38b32 100644 --- a/tests/libtest/lib505.c +++ b/tests/libtest/lib505.c @@ -42,6 +42,7 @@ int test(char *URL) FILE *hd_src ; int hd ; struct_stat file_info; + struct curl_slist *hl; struct curl_slist *headerlist=NULL; const char *buf_1 = "RNFR 505"; @@ -73,51 +74,69 @@ int test(char *URL) return -2; /* if this happens things are major weird */ } - /* In windows, this will init the winsock stuff */ - curl_global_init(CURL_GLOBAL_ALL); + if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) { + fprintf(stderr, "curl_global_init() failed\n"); + fclose(hd_src); + return TEST_ERR_MAJOR_BAD; + } /* get a curl handle */ - curl = curl_easy_init(); - if(curl) { - struct curl_slist *hl; - /* build a list of commands to pass to libcurl */ - hl = curl_slist_append(headerlist, buf_1); - if(hl) { - headerlist = curl_slist_append(hl, buf_2); - if(hl) - headerlist = hl; - } + if ((curl = curl_easy_init()) == NULL) { + fprintf(stderr, "curl_easy_init() failed\n"); + curl_global_cleanup(); + fclose(hd_src); + return TEST_ERR_MAJOR_BAD; + } - /* enable uploading */ - curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ; + /* build a list of commands to pass to libcurl */ - /* enable verbose */ - curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE) ; + if ((hl = curl_slist_append(headerlist, buf_1)) == NULL) { + fprintf(stderr, "curl_slist_append() failed\n"); + curl_easy_cleanup(curl); + curl_global_cleanup(); + fclose(hd_src); + return TEST_ERR_MAJOR_BAD; + } + if ((headerlist = curl_slist_append(hl, buf_2)) == NULL) { + fprintf(stderr, "curl_slist_append() failed\n"); + curl_slist_free_all(hl); + curl_easy_cleanup(curl); + curl_global_cleanup(); + fclose(hd_src); + return TEST_ERR_MAJOR_BAD; + } + headerlist = hl; - /* specify target */ - curl_easy_setopt(curl,CURLOPT_URL, URL); + /* enable uploading */ + curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ; - /* pass in that last of FTP commands to run after the transfer */ - curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist); + /* enable verbose */ + curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE) ; - /* now specify which file to upload */ - curl_easy_setopt(curl, CURLOPT_INFILE, hd_src); + /* specify target */ + curl_easy_setopt(curl,CURLOPT_URL, URL); - /* and give the size of the upload (optional) */ - curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, - (curl_off_t)file_info.st_size); + /* pass in that last of FTP commands to run after the transfer */ + curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist); - /* Now run off and do what you've been told! */ - res = curl_easy_perform(curl); + /* now specify which file to upload */ + curl_easy_setopt(curl, CURLOPT_INFILE, hd_src); - /* clean up the FTP commands list */ - curl_slist_free_all (headerlist); + /* and give the size of the upload (optional) */ + curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, + (curl_off_t)file_info.st_size); - /* always cleanup */ - curl_easy_cleanup(curl); - } - fclose(hd_src); /* close the local file */ + /* Now run off and do what you've been told! */ + res = curl_easy_perform(curl); + + /* clean up the FTP commands list */ + curl_slist_free_all(headerlist); + /* close the local file */ + fclose(hd_src); + + curl_easy_cleanup(curl); curl_global_cleanup(); + return res; } -- cgit v1.2.3