ftpsget.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2015, 2017, 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.haxx.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  ***************************************************************************/
22 
23 #include <stdio.h>
24 
25 #include <curl/curl.h>
26 
27 /* <DESC>
28  * Get a single file from an FTPS server.
29  * </DESC>
30  */
31 
32 struct FtpFile {
33  const char *filename;
34  FILE *stream;
35 };
36 
37 static size_t my_fwrite(void *buffer, size_t size, size_t nmemb,
38  void *stream)
39 {
40  struct FtpFile *out = (struct FtpFile *)stream;
41  if(out && !out->stream) {
42  /* open file for writing */
43  out->stream = fopen(out->filename, "wb");
44  if(!out->stream)
45  return -1; /* failure, can't open file to write */
46  }
47  return fwrite(buffer, size, nmemb, out->stream);
48 }
49 
50 
51 int main(void)
52 {
53  CURL *curl;
54  CURLcode res;
55  struct FtpFile ftpfile = {
56  "yourfile.bin", /* name to store the file as if successful */
57  NULL
58  };
59 
61 
62  curl = curl_easy_init();
63  if(curl) {
64  /*
65  * You better replace the URL with one that works! Note that we use an
66  * FTP:// URL with standard explicit FTPS. You can also do FTPS:// URLs if
67  * you want to do the rarer kind of transfers: implicit.
68  */
69  curl_easy_setopt(curl, CURLOPT_URL,
70  "ftp://user@server/home/user/file.txt");
71  /* Define our callback to get called when there's data to be written */
72  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
73  /* Set a pointer to our struct to pass to the callback */
74  curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
75 
76  /* We activate SSL and we require it for both control and data */
77  curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
78 
79  /* Switch on full protocol/debug output */
80  curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
81 
82  res = curl_easy_perform(curl);
83 
84  /* always cleanup */
85  curl_easy_cleanup(curl);
86 
87  if(CURLE_OK != res) {
88  /* we failed */
89  fprintf(stderr, "curl told us %d\n", res);
90  }
91  }
92 
93  if(ftpfile.stream)
94  fclose(ftpfile.stream); /* close the local file */
95 
97 
98  return 0;
99 }
Definition: ftpget.c:31
CURLcode
Definition: curl.h:454
static int res
#define curl_easy_setopt(handle, option, value)
Definition: typecheck-gcc.h:41
char buffer[]
Definition: unit1308.c:48
FILE * stream
Definition: ftpget.c:33
#define CURL_GLOBAL_DEFAULT
Definition: curl.h:2521
CURL_EXTERN CURL * curl_easy_init(void)
Definition: easy.c:343
static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
Definition: ftpsget.c:37
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
Definition: curl.h:455
CURL_EXTERN CURLcode curl_global_init(long flags)
curl_global_init() globally initializes curl given a bitwise set of the different features of what to...
Definition: easy.c:271
void CURL
Definition: curl.h:102
size_t size
Definition: unit1302.c:52
#define fprintf
Definition: curl_printf.h:41
CURL_EXTERN void curl_global_cleanup(void)
curl_global_cleanup() globally cleanups curl, uses the value of "init_flags" to determine what needs ...
Definition: easy.c:312
const char * filename
Definition: ftpget.c:32
static CURL * curl
Definition: sessioninfo.c:35
int main(void)
Definition: ftpsget.c:51
size_t fwrite(const void *, size_t, size_t, FILE *)
CURL_EXTERN CURLcode curl_easy_perform(CURL *curl)


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:09