diff options
author | Cédric Deltheil <cedric@moodstocks.com> | 2013-02-24 18:17:30 +0100 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2013-03-09 16:38:18 +0100 |
commit | 6645d2ae8f2f813eb2deb908e7b1f6fc9772c2da (patch) | |
tree | 1890fd73b6d1b36b1402bf006403a71b1ee23acc /docs/examples | |
parent | cb07bca689d4cbb43547daacad9690412d177a6d (diff) |
examples/getinmemory.c: abort the transfer if not enough memory
No more use exit(3) but instead tell libcurl that no byte has been
written to let it return a `CURLE_WRITE_ERROR`. In addition, check
curl easy handle return code.
Diffstat (limited to 'docs/examples')
-rw-r--r-- | docs/examples/getinmemory.c | 40 |
1 files changed, 24 insertions, 16 deletions
diff --git a/docs/examples/getinmemory.c b/docs/examples/getinmemory.c index 78e6deb10..8e31e8f00 100644 --- a/docs/examples/getinmemory.c +++ b/docs/examples/getinmemory.c @@ -42,10 +42,10 @@ WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) struct MemoryStruct *mem = (struct MemoryStruct *)userp; mem->memory = realloc(mem->memory, mem->size + realsize + 1); - if (mem->memory == NULL) { + if(mem->memory == NULL) { /* out of memory! */ printf("not enough memory (realloc returned NULL)\n"); - exit(EXIT_FAILURE); + return 0; } memcpy(&(mem->memory[mem->size]), contents, realsize); @@ -59,6 +59,7 @@ WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) int main(void) { CURL *curl_handle; + CURLcode res; struct MemoryStruct chunk; @@ -84,24 +85,31 @@ int main(void) curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); /* get it! */ - curl_easy_perform(curl_handle); + res = curl_easy_perform(curl_handle); + + /* check for errors */ + if(res != CURLE_OK) { + fprintf(stderr, "curl_easy_perform() failed: %s\n", + curl_easy_strerror(res)); + } + else { + /* + * Now, our chunk.memory points to a memory block that is chunk.size + * bytes big and contains the remote file. + * + * Do something nice with it! + * + * You should be aware of the fact that at this point we might have an + * allocated data block, and nothing has yet deallocated that data. So when + * you're done with it, you should free() it as a nice application. + */ + + printf("%lu bytes retrieved\n", (long)chunk.size); + } /* cleanup curl stuff */ curl_easy_cleanup(curl_handle); - /* - * Now, our chunk.memory points to a memory block that is chunk.size - * bytes big and contains the remote file. - * - * Do something nice with it! - * - * You should be aware of the fact that at this point we might have an - * allocated data block, and nothing has yet deallocated that data. So when - * you're done with it, you should free() it as a nice application. - */ - - printf("%lu bytes retrieved\n", (long)chunk.size); - if(chunk.memory) free(chunk.memory); |