1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
25 #include <curl/curl.h>
27 /* define this to switch off the use of ssh-agent in this program */
28 #undef DISABLE_SSH_AGENT
31 * This is an example showing how to get a single file from an SFTP server.
32 * It delays the actual destination file creation until the first write
33 * callback so that it won't create an empty file in case the remote file
34 * doesn't exist or something else fails.
42 static size_t my_fwrite(void *buffer, size_t size, size_t nmemb,
45 struct FtpFile *out=(struct FtpFile *)stream;
46 if(out && !out->stream) {
47 /* open file for writing */
48 out->stream=fopen(out->filename, "wb");
50 return -1; /* failure, can't open file to write */
52 return fwrite(buffer, size, nmemb, out->stream);
60 struct FtpFile ftpfile={
61 "yourfile.bin", /* name to store the file as if succesful */
65 curl_global_init(CURL_GLOBAL_DEFAULT);
67 curl = curl_easy_init();
70 * You better replace the URL with one that works!
72 curl_easy_setopt(curl, CURLOPT_URL,
73 "sftp://user@server/home/user/file.txt");
74 /* Define our callback to get called when there's data to be written */
75 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
76 /* Set a pointer to our struct to pass to the callback */
77 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
79 #ifndef DISABLE_SSH_AGENT
80 /* We activate ssh agent. For this to work you need
81 to have ssh-agent running (type set | grep SSH_AGENT to check) or
82 pageant on Windows (there is an icon in systray if so) */
83 curl_easy_setopt(curl, CURLOPT_SSH_AUTH_TYPES, CURLSSH_AUTH_AGENT);
86 /* Switch on full protocol/debug output */
87 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
89 res = curl_easy_perform(curl);
92 curl_easy_cleanup(curl);
96 fprintf(stderr, "curl told us %d\n", res);
101 fclose(ftpfile.stream); /* close the local file */
103 curl_global_cleanup();