1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24 #include <stdio.h>
25
26 #include <curl/curl.h>
27
28 /* <DESC>
29 * Similar to ftpget.c but also stores the received response-lines
30 * in a separate file using our own callback!
31 * </DESC>
32 */
33 static size_t
write_response(void * ptr,size_t size,size_t nmemb,void * data)34 write_response(void *ptr, size_t size, size_t nmemb, void *data)
35 {
36 FILE *writehere = (FILE *)data;
37 return fwrite(ptr, size, nmemb, writehere);
38 }
39
40 #define FTPBODY "ftp-list"
41 #define FTPHEADERS "ftp-responses"
42
main(void)43 int main(void)
44 {
45 CURL *curl;
46 CURLcode res;
47 FILE *ftpfile;
48 FILE *respfile;
49
50 /* local filename to store the file as */
51 ftpfile = fopen(FTPBODY, "wb"); /* b is binary, needed on Windows */
52
53 /* local filename to store the FTP server's response lines in */
54 respfile = fopen(FTPHEADERS, "wb"); /* b is binary, needed on Windows */
55
56 curl = curl_easy_init();
57 if(curl) {
58 /* Get a file listing from sunet */
59 curl_easy_setopt(curl, CURLOPT_URL, "ftp://ftp.example.com/");
60 curl_easy_setopt(curl, CURLOPT_WRITEDATA, ftpfile);
61 /* If you intend to use this on Windows with a libcurl DLL, you must use
62 CURLOPT_WRITEFUNCTION as well */
63 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_response);
64 curl_easy_setopt(curl, CURLOPT_HEADERDATA, respfile);
65 res = curl_easy_perform(curl);
66 /* Check for errors */
67 if(res != CURLE_OK)
68 fprintf(stderr, "curl_easy_perform() failed: %s\n",
69 curl_easy_strerror(res));
70
71 /* always cleanup */
72 curl_easy_cleanup(curl);
73 }
74
75 fclose(ftpfile); /* close the local file */
76 fclose(respfile); /* close the response file */
77
78 return 0;
79 }
80