url2file.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 /* <DESC>
00023  * Download a given URL into a local file named page.out.
00024  * </DESC>
00025  */
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <unistd.h>
00029 
00030 #include <curl/curl.h>
00031 
00032 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
00033 {
00034   size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
00035   return written;
00036 }
00037 
00038 int main(int argc, char *argv[])
00039 {
00040   CURL *curl_handle;
00041   static const char *pagefilename = "page.out";
00042   FILE *pagefile;
00043 
00044   if(argc < 2) {
00045     printf("Usage: %s <URL>\n", argv[0]);
00046     return 1;
00047   }
00048 
00049   curl_global_init(CURL_GLOBAL_ALL);
00050 
00051   /* init the curl session */
00052   curl_handle = curl_easy_init();
00053 
00054   /* set URL to get here */
00055   curl_easy_setopt(curl_handle, CURLOPT_URL, argv[1]);
00056 
00057   /* Switch on full protocol/debug output while testing */
00058   curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
00059 
00060   /* disable progress meter, set to 0L to enable and disable debug output */
00061   curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
00062 
00063   /* send all data to this function  */
00064   curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
00065 
00066   /* open the file */
00067   pagefile = fopen(pagefilename, "wb");
00068   if(pagefile) {
00069 
00070     /* write the page body to this file handle */
00071     curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);
00072 
00073     /* get it! */
00074     curl_easy_perform(curl_handle);
00075 
00076     /* close the header file */
00077     fclose(pagefile);
00078   }
00079 
00080   /* cleanup curl stuff */
00081   curl_easy_cleanup(curl_handle);
00082 
00083   return 0;
00084 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:07