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 #include <stdio.h> 00023 00024 #include <curl/curl.h> 00025 00026 /* <DESC> 00027 * Similar to ftpget.c but also stores the received response-lines 00028 * in a separate file using our own callback! 00029 * </DESC> 00030 */ 00031 static size_t 00032 write_response(void *ptr, size_t size, size_t nmemb, void *data) 00033 { 00034 FILE *writehere = (FILE *)data; 00035 return fwrite(ptr, size, nmemb, writehere); 00036 } 00037 00038 #define FTPBODY "ftp-list" 00039 #define FTPHEADERS "ftp-responses" 00040 00041 int main(void) 00042 { 00043 CURL *curl; 00044 CURLcode res; 00045 FILE *ftpfile; 00046 FILE *respfile; 00047 00048 /* local file name to store the file as */ 00049 ftpfile = fopen(FTPBODY, "wb"); /* b is binary, needed on win32 */ 00050 00051 /* local file name to store the FTP server's response lines in */ 00052 respfile = fopen(FTPHEADERS, "wb"); /* b is binary, needed on win32 */ 00053 00054 curl = curl_easy_init(); 00055 if(curl) { 00056 /* Get a file listing from sunet */ 00057 curl_easy_setopt(curl, CURLOPT_URL, "ftp://ftp.example.com/"); 00058 curl_easy_setopt(curl, CURLOPT_WRITEDATA, ftpfile); 00059 /* If you intend to use this on windows with a libcurl DLL, you must use 00060 CURLOPT_WRITEFUNCTION as well */ 00061 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_response); 00062 curl_easy_setopt(curl, CURLOPT_HEADERDATA, respfile); 00063 res = curl_easy_perform(curl); 00064 /* Check for errors */ 00065 if(res != CURLE_OK) 00066 fprintf(stderr, "curl_easy_perform() failed: %s\n", 00067 curl_easy_strerror(res)); 00068 00069 /* always cleanup */ 00070 curl_easy_cleanup(curl); 00071 } 00072 00073 fclose(ftpfile); /* close the local file */ 00074 fclose(respfile); /* close the response file */ 00075 00076 return 0; 00077 }