1 /*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel (at) 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 http://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 /* define this to switch off the use of ssh-agent in this program */ 28 #undef DISABLE_SSH_AGENT 29 30 /* 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. 35 */ 36 37 struct FtpFile { 38 const char *filename; 39 FILE *stream; 40 }; 41 42 static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, 43 void *stream) 44 { 45 struct FtpFile *out=(struct FtpFile *)stream; 46 if(out && !out->stream) { 47 /* open file for writing */ 48 out->stream=fopen(out->filename, "wb"); 49 if(!out->stream) 50 return -1; /* failure, can't open file to write */ 51 } 52 return fwrite(buffer, size, nmemb, out->stream); 53 } 54 55 56 int main(void) 57 { 58 CURL *curl; 59 CURLcode res; 60 struct FtpFile ftpfile={ 61 "yourfile.bin", /* name to store the file as if successful */ 62 NULL 63 }; 64 65 curl_global_init(CURL_GLOBAL_DEFAULT); 66 67 curl = curl_easy_init(); 68 if(curl) { 69 /* 70 * You better replace the URL with one that works! 71 */ 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); 78 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); 84 #endif 85 86 /* Switch on full protocol/debug output */ 87 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); 88 89 res = curl_easy_perform(curl); 90 91 /* always cleanup */ 92 curl_easy_cleanup(curl); 93 94 if(CURLE_OK != res) { 95 /* we failed */ 96 fprintf(stderr, "curl told us %d\n", res); 97 } 98 } 99 100 if(ftpfile.stream) 101 fclose(ftpfile.stream); /* close the local file */ 102 103 curl_global_cleanup(); 104 105 return 0; 106 } 107