curl/docs/examples/ftpgetresp.c

75 lines
2.5 KiB
C
Raw Normal View History

/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
2002-01-08 16:25:44 +08:00
* \___|\___/|_| \_\_____|
*
2015-06-18 17:38:54 +08:00
* Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.haxx.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
2002-01-08 16:25:44 +08:00
#include <stdio.h>
#include <curl/curl.h>
2015-06-18 17:38:54 +08:00
/* <DESC>
* Similar to ftpget.c but also stores the received response-lines
2002-01-08 16:25:44 +08:00
* in a separate file using our own callback!
2015-06-18 17:38:54 +08:00
* </DESC>
2002-01-08 16:25:44 +08:00
*/
static size_t
2002-01-08 16:25:44 +08:00
write_response(void *ptr, size_t size, size_t nmemb, void *data)
{
FILE *writehere = (FILE *)data;
return fwrite(ptr, size, nmemb, writehere);
}
2010-12-18 06:34:26 +08:00
int main(void)
2002-01-08 16:25:44 +08:00
{
CURL *curl;
CURLcode res;
FILE *ftpfile;
FILE *respfile;
2002-01-08 16:25:44 +08:00
/* local file name to store the file as */
ftpfile = fopen("ftp-list", "wb"); /* b is binary, needed on win32 */
/* local file name to store the FTP server's response lines in */
respfile = fopen("ftp-responses", "wb"); /* b is binary, needed on win32 */
curl = curl_easy_init();
if(curl) {
/* Get a file listing from sunet */
curl_easy_setopt(curl, CURLOPT_URL, "ftp://ftp.example.com/");
2003-12-08 22:13:19 +08:00
curl_easy_setopt(curl, CURLOPT_WRITEDATA, ftpfile);
/* If you intend to use this on windows with a libcurl DLL, you must use
CURLOPT_WRITEFUNCTION as well */
2002-01-08 16:25:44 +08:00
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_response);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, respfile);
2002-01-08 16:25:44 +08:00
res = curl_easy_perform(curl);
2012-07-04 23:03:52 +08:00
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
2002-01-08 16:25:44 +08:00
/* always cleanup */
curl_easy_cleanup(curl);
}
fclose(ftpfile); /* close the local file */
fclose(respfile); /* close the response file */
return 0;
}