Git init
[external/curl.git] / docs / examples / getinmemory.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  *
9  * Example source code to show how the callback function can be used to
10  * download data into a chunk of memory instead of storing it in a file.
11  */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 #include <curl/curl.h>
18
19 struct MemoryStruct {
20   char *memory;
21   size_t size;
22 };
23
24
25 static size_t
26 WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
27 {
28   size_t realsize = size * nmemb;
29   struct MemoryStruct *mem = (struct MemoryStruct *)data;
30
31   mem->memory = realloc(mem->memory, mem->size + realsize + 1);
32   if (mem->memory == NULL) {
33     /* out of memory! */
34     printf("not enough memory (realloc returned NULL)\n");
35     exit(EXIT_FAILURE);
36   }
37
38   memcpy(&(mem->memory[mem->size]), ptr, realsize);
39   mem->size += realsize;
40   mem->memory[mem->size] = 0;
41
42   return realsize;
43 }
44
45
46 int main(int argc, char **argv)
47 {
48   CURL *curl_handle;
49
50   struct MemoryStruct chunk;
51
52   chunk.memory = malloc(1);  /* will be grown as needed by the realloc above */
53   chunk.size = 0;    /* no data at this point */
54
55   curl_global_init(CURL_GLOBAL_ALL);
56
57   /* init the curl session */
58   curl_handle = curl_easy_init();
59
60   /* specify URL to get */
61   curl_easy_setopt(curl_handle, CURLOPT_URL, "http://www.example.com/");
62
63   /* send all data to this function  */
64   curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
65
66   /* we pass our 'chunk' struct to the callback function */
67   curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
68
69   /* some servers don't like requests that are made without a user-agent
70      field, so we provide one */
71   curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
72
73   /* get it! */
74   curl_easy_perform(curl_handle);
75
76   /* cleanup curl stuff */
77   curl_easy_cleanup(curl_handle);
78
79   /*
80    * Now, our chunk.memory points to a memory block that is chunk.size
81    * bytes big and contains the remote file.
82    *
83    * Do something nice with it!
84    *
85    * You should be aware of the fact that at this point we might have an
86    * allocated data block, and nothing has yet deallocated that data. So when
87    * you're done with it, you should free() it as a nice application.
88    */
89
90   printf("%lu bytes retrieved\n", chunk.size);
91
92   if(chunk.memory)
93     free(chunk.memory);
94
95   /* we're done with libcurl, so clean it up */
96   curl_global_cleanup();
97
98   return 0;
99 }