adf696c9a10bd31c9a5618f63fb79aed14d594ae
[platform/upstream/curl.git] / docs / examples / url2file.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at http://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25
26 #include <curl/curl.h>
27
28 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
29 {
30   size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
31   return written;
32 }
33
34 int main(int argc, char *argv[])
35 {
36   CURL *curl_handle;
37   static const char *pagefilename = "page.out";
38   FILE *pagefile;
39
40   if(argc < 2 ) {
41     printf("Usage: %s <URL>\n", argv[0]);
42     return 1;
43   }
44
45   curl_global_init(CURL_GLOBAL_ALL);
46
47   /* init the curl session */
48   curl_handle = curl_easy_init();
49
50   /* set URL to get here */
51   curl_easy_setopt(curl_handle, CURLOPT_URL, argv[1]);
52
53   /* Switch on full protocol/debug output while testing */
54   curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
55
56   /* disable progress meter, set to 0L to enable and disable debug output */
57   curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
58
59   /* send all data to this function  */
60   curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
61
62   /* open the file */
63   pagefile = fopen(pagefilename, "wb");
64   if (pagefile) {
65
66     /* write the page body to this file handle */
67     curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);
68
69     /* get it! */
70     curl_easy_perform(curl_handle);
71
72     /* close the header file */
73     fclose(pagefile);
74   }
75
76   /* cleanup curl stuff */
77   curl_easy_cleanup(curl_handle);
78
79   return 0;
80 }