1e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET/*************************************************************************** 29bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels * _ _ ____ _ 39bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels * Project ___| | | | _ \| | 49bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels * / __| | | | |_) | | 59bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels * | (__| |_| | _ <| |___ 69bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels * \___|\___/|_| \_\_____| 79bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels * 8e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al. 9e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * 10e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * This software is licensed as described in the file COPYING, which 11e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * you should have received as part of this distribution. The terms 12e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * are also available at http://curl.haxx.se/docs/copyright.html. 13e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * 14e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * You may opt to use, copy, modify, merge, publish, distribute and/or sell 15e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * copies of the Software, and permit persons to whom the Software is 16e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * furnished to do so, under the terms of the COPYING file. 17e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * 18e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 19e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * KIND, either express or implied. 20e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * 21e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET ***************************************************************************/ 229bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels#include <stdio.h> 239bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels 249bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels#include <curl/curl.h> 259bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels 269bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels/* 279bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels * Similar to ftpget.c but this also stores the received response-lines 289bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels * in a separate file using our own callback! 299bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels * 309bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels * This functionality was introduced in libcurl 7.9.3. 319bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels */ 329bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels 339bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsstatic size_t 349bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelswrite_response(void *ptr, size_t size, size_t nmemb, void *data) 359bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels{ 369bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels FILE *writehere = (FILE *)data; 379bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels return fwrite(ptr, size, nmemb, writehere); 389bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels} 399bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels 40e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETint main(void) 419bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels{ 429bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels CURL *curl; 439bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels CURLcode res; 449bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels FILE *ftpfile; 459bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels FILE *respfile; 469bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels 479bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels /* local file name to store the file as */ 489bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels ftpfile = fopen("ftp-list", "wb"); /* b is binary, needed on win32 */ 499bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels 509bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels /* local file name to store the FTP server's response lines in */ 519bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels respfile = fopen("ftp-responses", "wb"); /* b is binary, needed on win32 */ 529bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels 539bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels curl = curl_easy_init(); 549bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels if(curl) { 559bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels /* Get a file listing from sunet */ 569bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels curl_easy_setopt(curl, CURLOPT_URL, "ftp://ftp.example.com/"); 579bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels curl_easy_setopt(curl, CURLOPT_WRITEDATA, ftpfile); 589bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels /* If you intend to use this on windows with a libcurl DLL, you must use 599bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels CURLOPT_WRITEFUNCTION as well */ 609bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_response); 61e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET curl_easy_setopt(curl, CURLOPT_HEADERDATA, respfile); 629bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels res = curl_easy_perform(curl); 63e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET /* Check for errors */ 64e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET if(res != CURLE_OK) 65e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET fprintf(stderr, "curl_easy_perform() failed: %s\n", 66e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET curl_easy_strerror(res)); 679bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels 689bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels /* always cleanup */ 699bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels curl_easy_cleanup(curl); 709bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels } 719bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels 729bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels fclose(ftpfile); /* close the local file */ 739bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels fclose(respfile); /* close the response file */ 749bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels 759bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels return 0; 769bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels} 77