curl/docs/examples/ftpgetresp.c

78 строки
2.5 KiB
C
Исходник Обычный вид История

/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
2002-01-08 11:25:44 +03:00
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2020, 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
2020-11-04 16:02:01 +03:00
* are also available at https://curl.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 11:25:44 +03:00
#include <stdio.h>
#include <curl/curl.h>
2015-06-18 12:38:54 +03:00
/* <DESC>
* Similar to ftpget.c but also stores the received response-lines
2002-01-08 11:25:44 +03:00
* in a separate file using our own callback!
2015-06-18 12:38:54 +03:00
* </DESC>
2002-01-08 11:25:44 +03:00
*/
static size_t
2002-01-08 11:25:44 +03:00
write_response(void *ptr, size_t size, size_t nmemb, void *data)
{
FILE *writehere = (FILE *)data;
return fwrite(ptr, size, nmemb, writehere);
}
#define FTPBODY "ftp-list"
#define FTPHEADERS "ftp-responses"
2010-12-18 01:34:26 +03:00
int main(void)
2002-01-08 11:25:44 +03:00
{
CURL *curl;
CURLcode res;
FILE *ftpfile;
FILE *respfile;
2002-01-08 11:25:44 +03:00
/* local file name to store the file as */
ftpfile = fopen(FTPBODY, "wb"); /* b is binary, needed on win32 */
2002-01-08 11:25:44 +03:00
/* local file name to store the FTP server's response lines in */
respfile = fopen(FTPHEADERS, "wb"); /* b is binary, needed on win32 */
2002-01-08 11:25:44 +03:00
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 17:13:19 +03: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 11:25:44 +03:00
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_response);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, respfile);
2002-01-08 11:25:44 +03:00
res = curl_easy_perform(curl);
2012-07-04 19:03:52 +04:00
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
2002-01-08 11:25:44 +03:00
/* always cleanup */
curl_easy_cleanup(curl);
}
fclose(ftpfile); /* close the local file */
fclose(respfile); /* close the response file */
return 0;
}