lib505.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * $Id: lib505.c,v 1.4 2004/02/05 12:34:17 bagder Exp $
  9. */
  10. #include "test.h"
  11. #ifdef HAVE_SYS_SOCKET_H
  12. #include <sys/socket.h>
  13. #endif
  14. #ifdef HAVE_SYS_TYPES_
  15. #include <sys/types.h>
  16. #endif
  17. #ifdef HAVE_SYS_STAT_H
  18. #include <sys/stat.h>
  19. #endif
  20. #ifdef HAVE_FCNTL_H
  21. #include <fcntl.h>
  22. #endif
  23. #ifdef HAVE_UNISTD_H
  24. #include <unistd.h>
  25. #endif
  26. /*
  27. * This example shows an FTP upload, with a rename of the file just after
  28. * a successful upload.
  29. *
  30. * Example based on source code provided by Erick Nuwendam. Thanks!
  31. */
  32. int test(char *URL)
  33. {
  34. CURL *curl;
  35. CURLcode res;
  36. FILE *hd_src ;
  37. int hd ;
  38. struct stat file_info;
  39. struct curl_slist *headerlist=NULL;
  40. const char *buf_1 = "RNFR 505";
  41. const char *buf_2 = "RNTO 505-forreal";
  42. /* get the file size of the local file */
  43. hd = stat(arg2, &file_info);
  44. if(hd == -1) {
  45. /* can't open file, bail out */
  46. return -1;
  47. }
  48. if(! file_info.st_size) {
  49. fprintf(stderr, "WARNING: file %s has no size!\n", arg2);
  50. return -4;
  51. }
  52. /* get a FILE * of the same file, could also be made with
  53. fdopen() from the previous descriptor, but hey this is just
  54. an example! */
  55. hd_src = fopen(arg2, "rb");
  56. if(NULL == hd_src) {
  57. return -2; /* if this happens things are major weird */
  58. }
  59. /* In windows, this will init the winsock stuff */
  60. curl_global_init(CURL_GLOBAL_ALL);
  61. /* get a curl handle */
  62. curl = curl_easy_init();
  63. if(curl) {
  64. /* build a list of commands to pass to libcurl */
  65. headerlist = curl_slist_append(headerlist, buf_1);
  66. headerlist = curl_slist_append(headerlist, buf_2);
  67. /* enable uploading */
  68. curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;
  69. /* enable verbose */
  70. curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE) ;
  71. /* specify target */
  72. curl_easy_setopt(curl,CURLOPT_URL, URL);
  73. /* pass in that last of FTP commands to run after the transfer */
  74. curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
  75. /* now specify which file to upload */
  76. curl_easy_setopt(curl, CURLOPT_INFILE, hd_src);
  77. /* and give the size of the upload (optional) */
  78. curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
  79. file_info.st_size);
  80. /* Now run off and do what you've been told! */
  81. res = curl_easy_perform(curl);
  82. /* clean up the FTP commands list */
  83. curl_slist_free_all (headerlist);
  84. /* always cleanup */
  85. curl_easy_cleanup(curl);
  86. }
  87. fclose(hd_src); /* close the local file */
  88. curl_global_cleanup();
  89. return res;
  90. }