new libcurl example code stuff
[platform/upstream/curl.git] / docs / examples / sepheaders.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4
5 #include <curl/curl.h>
6 #include <curl/types.h>
7 #include <curl/easy.h>
8
9 size_t  write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
10 {
11   written = fwrite(ptr,size,nmemb,outfile);
12   return written;
13 }
14
15 int main(int argc, char **argv)
16 {
17   CURL *curl_handle;
18   char *headerfilename = "head.out";
19   FILE *headerfile;
20   char *bodyfilename = "body.out";
21   FILE *bodyfile;
22
23   /* init the curl session */
24   curl_handle = curl_easy_init();
25
26   /* set URL to get */
27   curl_easy_setopt(curl_handle, CURLOPT_URL, "http://curl.haxx.se");
28
29   /* no progress meter please */
30   curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1);
31
32   /* shut up completely */
33   curl_easy_setopt(curl_handle, CURLOPT_MUTE, 1);
34
35   /* send all data to this function  */
36   curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
37
38   /* open the files */
39   headerfile = fopen(headerfilename,"w");
40   if (headerfile == NULL) {
41     curl_easy_cleanup(curl_handle);
42     return -1;
43   }
44   bodyfile = fopen(bodyfilename,"w");
45   if (bodyfile == NULL) {
46     curl_easy_cleanup(curl_handle);
47     return -1;
48   }
49
50   /* we want the headers to this file handle */
51   curl_easy_setopt(curl_handle,   CURLOPT_WRITEHEADER ,headerfile);
52
53   /* get it! */
54   curl_easy_perform(curl_handle);
55
56   /* close the header file */
57   fclose(headerfile);
58
59   /* cleanup curl stuff */
60   curl_easy_cleanup(curl_handle);
61
62   return 0;
63 }