Git init
[external/curl.git] / docs / examples / sepheaders.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13
14 #include <curl/curl.h>
15 #include <curl/types.h>
16 #include <curl/easy.h>
17
18 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
19 {
20   int written = fwrite(ptr, size, nmemb, (FILE *)stream);
21   return written;
22 }
23
24 int main(int argc, char **argv)
25 {
26   CURL *curl_handle;
27   static const char *headerfilename = "head.out";
28   FILE *headerfile;
29   static const char *bodyfilename = "body.out";
30   FILE *bodyfile;
31
32   curl_global_init(CURL_GLOBAL_ALL);
33
34   /* init the curl session */
35   curl_handle = curl_easy_init();
36
37   /* set URL to get */
38   curl_easy_setopt(curl_handle, CURLOPT_URL, "http://example.com");
39
40   /* no progress meter please */
41   curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
42
43   /* send all data to this function  */
44   curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
45
46   /* open the files */
47   headerfile = fopen(headerfilename,"w");
48   if (headerfile == NULL) {
49     curl_easy_cleanup(curl_handle);
50     return -1;
51   }
52   bodyfile = fopen(bodyfilename,"w");
53   if (bodyfile == NULL) {
54     curl_easy_cleanup(curl_handle);
55     return -1;
56   }
57
58   /* we want the headers to this file handle */
59   curl_easy_setopt(curl_handle,   CURLOPT_WRITEHEADER, headerfile);
60
61   /*
62    * Notice here that if you want the actual data sent anywhere else but
63    * stdout, you should consider using the CURLOPT_WRITEDATA option.  */
64
65   /* get it! */
66   curl_easy_perform(curl_handle);
67
68   /* close the header file */
69   fclose(headerfile);
70
71   /* cleanup curl stuff */
72   curl_easy_cleanup(curl_handle);
73
74   return 0;
75 }