sftpget.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 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 /* <DESC>
23  * Gets a file using an SFTP URL.
24  * </DESC>
25  */
26 
27 #include <stdio.h>
28 
29 #include <curl/curl.h>
30 
31 /* define this to switch off the use of ssh-agent in this program */
32 #undef DISABLE_SSH_AGENT
33 
34 /*
35  * This is an example showing how to get a single file from an SFTP server.
36  * It delays the actual destination file creation until the first write
37  * callback so that it won't create an empty file in case the remote file
38  * doesn't exist or something else fails.
39  */
40 
41 struct FtpFile {
42  const char *filename;
43  FILE *stream;
44 };
45 
46 static size_t my_fwrite(void *buffer, size_t size, size_t nmemb,
47  void *stream)
48 {
49  struct FtpFile *out = (struct FtpFile *)stream;
50  if(out && !out->stream) {
51  /* open file for writing */
52  out->stream = fopen(out->filename, "wb");
53  if(!out->stream)
54  return -1; /* failure, can't open file to write */
55  }
56  return fwrite(buffer, size, nmemb, out->stream);
57 }
58 
59 
60 int main(void)
61 {
62  CURL *curl;
63  CURLcode res;
64  struct FtpFile ftpfile = {
65  "yourfile.bin", /* name to store the file as if successful */
66  NULL
67  };
68 
70 
71  curl = curl_easy_init();
72  if(curl) {
73  /*
74  * You better replace the URL with one that works!
75  */
76  curl_easy_setopt(curl, CURLOPT_URL,
77  "sftp://user@server/home/user/file.txt");
78  /* Define our callback to get called when there's data to be written */
79  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
80  /* Set a pointer to our struct to pass to the callback */
81  curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
82 
83 #ifndef DISABLE_SSH_AGENT
84  /* We activate ssh agent. For this to work you need
85  to have ssh-agent running (type set | grep SSH_AGENT to check) or
86  pageant on Windows (there is an icon in systray if so) */
87  curl_easy_setopt(curl, CURLOPT_SSH_AUTH_TYPES, CURLSSH_AUTH_AGENT);
88 #endif
89 
90  /* Switch on full protocol/debug output */
91  curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
92 
93  res = curl_easy_perform(curl);
94 
95  /* always cleanup */
96  curl_easy_cleanup(curl);
97 
98  if(CURLE_OK != res) {
99  /* we failed */
100  fprintf(stderr, "curl told us %d\n", res);
101  }
102  }
103 
104  if(ftpfile.stream)
105  fclose(ftpfile.stream); /* close the local file */
106 
108 
109  return 0;
110 }
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: sftpget.c:46
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
Definition: curl.h:455
#define CURLSSH_AUTH_AGENT
Definition: curl.h:717
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
int main(void)
Definition: sftpget.c:60
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
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:16