xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame^] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 1998 - 2022, 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 https://curl.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 | * SPDX-License-Identifier: curl |
| 22 | * |
| 23 | ***************************************************************************/ |
| 24 | /* <DESC> |
| 25 | * Download a given URL into a local file named page.out. |
| 26 | * </DESC> |
| 27 | */ |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <unistd.h> |
| 31 | |
| 32 | #include <curl/curl.h> |
| 33 | |
| 34 | static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) |
| 35 | { |
| 36 | size_t written = fwrite(ptr, size, nmemb, (FILE *)stream); |
| 37 | return written; |
| 38 | } |
| 39 | |
| 40 | int main(int argc, char *argv[]) |
| 41 | { |
| 42 | CURL *curl_handle; |
| 43 | static const char *pagefilename = "page.out"; |
| 44 | FILE *pagefile; |
| 45 | |
| 46 | if(argc < 2) { |
| 47 | printf("Usage: %s <URL>\n", argv[0]); |
| 48 | return 1; |
| 49 | } |
| 50 | |
| 51 | curl_global_init(CURL_GLOBAL_ALL); |
| 52 | |
| 53 | /* init the curl session */ |
| 54 | curl_handle = curl_easy_init(); |
| 55 | |
| 56 | /* set URL to get here */ |
| 57 | curl_easy_setopt(curl_handle, CURLOPT_URL, argv[1]); |
| 58 | |
| 59 | /* Switch on full protocol/debug output while testing */ |
| 60 | curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L); |
| 61 | |
| 62 | /* disable progress meter, set to 0L to enable it */ |
| 63 | curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L); |
| 64 | |
| 65 | /* send all data to this function */ |
| 66 | curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data); |
| 67 | |
| 68 | /* open the file */ |
| 69 | pagefile = fopen(pagefilename, "wb"); |
| 70 | if(pagefile) { |
| 71 | |
| 72 | /* write the page body to this file handle */ |
| 73 | curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile); |
| 74 | |
| 75 | /* get it! */ |
| 76 | curl_easy_perform(curl_handle); |
| 77 | |
| 78 | /* close the header file */ |
| 79 | fclose(pagefile); |
| 80 | } |
| 81 | |
| 82 | /* cleanup curl stuff */ |
| 83 | curl_easy_cleanup(curl_handle); |
| 84 | |
| 85 | curl_global_cleanup(); |
| 86 | |
| 87 | return 0; |
| 88 | } |