2122c4f68be156f05f0667ee02b3bd927db58877
[external/curl.git] / docs / examples / ftpgetresp.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  */
9
10 #include <stdio.h>
11
12 #include <curl/curl.h>
13 #include <curl/types.h>
14 #include <curl/easy.h>
15
16 /*
17  * Similar to ftpget.c but this also stores the received response-lines
18  * in a separate file using our own callback!
19  *
20  * This functionality was introduced in libcurl 7.9.3.
21  */
22
23 static size_t
24 write_response(void *ptr, size_t size, size_t nmemb, void *data)
25 {
26   FILE *writehere = (FILE *)data;
27   return fwrite(ptr, size, nmemb, writehere);
28 }
29
30 int main(int argc, char **argv)
31 {
32   CURL *curl;
33   CURLcode res;
34   FILE *ftpfile;
35   FILE *respfile;
36
37   /* local file name to store the file as */
38   ftpfile = fopen("ftp-list", "wb"); /* b is binary, needed on win32 */
39
40   /* local file name to store the FTP server's response lines in */
41   respfile = fopen("ftp-responses", "wb"); /* b is binary, needed on win32 */
42
43   curl = curl_easy_init();
44   if(curl) {
45     /* Get a file listing from sunet */
46     curl_easy_setopt(curl, CURLOPT_URL, "ftp://ftp.example.com/");
47     curl_easy_setopt(curl, CURLOPT_WRITEDATA, ftpfile);
48     /* If you intend to use this on windows with a libcurl DLL, you must use
49        CURLOPT_WRITEFUNCTION as well */
50     curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_response);
51     curl_easy_setopt(curl, CURLOPT_WRITEHEADER, respfile);
52     res = curl_easy_perform(curl);
53
54     /* always cleanup */
55     curl_easy_cleanup(curl);
56   }
57
58   fclose(ftpfile); /* close the local file */
59   fclose(respfile); /* close the response file */
60
61   return 0;
62 }