ftpupload.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * $Id$
  9. */
  10. #include "curl/curl.h"
  11. #include "setup.h"
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <fcntl.h>
  15. #include "testconfig.h"
  16. /*
  17. * This example shows an FTP upload, with a rename of the file just after
  18. * a successful upload.
  19. *
  20. * Example based on source code provided by Erick Nuwendam. Thanks!
  21. */
  22. #define LOCAL_FILE LIBCURL_SOURCE_DIR "/Testing/ftpupload.c"
  23. #define UPLOAD_FILE_AS "while-uploading.txt"
  24. #define REMOTE_URL "ftp://public.kitware.com/incoming/" UPLOAD_FILE_AS
  25. #define RENAME_FILE_TO "renamed-and-fine.txt"
  26. int main(int argc, char **argv)
  27. {
  28. CURL *curl;
  29. CURLcode res;
  30. FILE *ftpfile;
  31. FILE * hd_src ;
  32. int hd ;
  33. struct stat file_info;
  34. struct curl_slist *headerlist=NULL;
  35. char buf_1 [] = "RNFR " UPLOAD_FILE_AS;
  36. char buf_2 [] = "RNTO " RENAME_FILE_TO;
  37. /* get the file size of the local file */
  38. hd = open(LOCAL_FILE, O_RDONLY) ;
  39. fstat(hd, &file_info);
  40. close(hd) ;
  41. /* get a FILE * of the same file, could also be made with
  42. fdopen() from the previous descriptor, but hey this is just
  43. an example! */
  44. hd_src = fopen(LOCAL_FILE, "rb");
  45. /* In windows, this will init the winsock stuff */
  46. curl_global_init(CURL_GLOBAL_ALL);
  47. /* get a curl handle */
  48. curl = curl_easy_init();
  49. if(curl) {
  50. /* build a list of commands to pass to libcurl */
  51. headerlist = curl_slist_append(headerlist, buf_1);
  52. headerlist = curl_slist_append(headerlist, buf_2);
  53. /* enable uploading */
  54. curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;
  55. /* specify target */
  56. curl_easy_setopt(curl,CURLOPT_URL, REMOTE_URL);
  57. /* pass in that last of FTP commands to run after the transfer */
  58. curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
  59. /* now specify which file to upload */
  60. curl_easy_setopt(curl, CURLOPT_INFILE, hd_src);
  61. /* and give the size of the upload (optional) */
  62. curl_easy_setopt(curl, CURLOPT_INFILESIZE, (long)file_info.st_size);
  63. /* Now run off and do what you've been told! */
  64. res = curl_easy_perform(curl);
  65. /* clean up the FTP commands list */
  66. curl_slist_free_all (headerlist);
  67. /* always cleanup */
  68. curl_easy_cleanup(curl);
  69. }
  70. fclose(hd_src); /* close the local file */
  71. curl_global_cleanup();
  72. return 0;
  73. }