lib505.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * $Id$
  9. */
  10. #include "setup.h" /* struct_stat etc. */
  11. #include "test.h"
  12. #ifdef HAVE_SYS_SOCKET_H
  13. #include <sys/socket.h>
  14. #endif
  15. #ifdef HAVE_SYS_TYPES_H
  16. #include <sys/types.h>
  17. #endif
  18. #ifdef HAVE_SYS_STAT_H
  19. #include <sys/stat.h>
  20. #endif
  21. #ifdef HAVE_FCNTL_H
  22. #include <fcntl.h>
  23. #endif
  24. #ifdef HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #endif
  27. /*
  28. * This example shows an FTP upload, with a rename of the file just after
  29. * a successful upload.
  30. *
  31. * Example based on source code provided by Erick Nuwendam. Thanks!
  32. */
  33. int test(char *URL)
  34. {
  35. CURL *curl;
  36. CURLcode res = CURLE_OK;
  37. FILE *hd_src ;
  38. int hd ;
  39. struct_stat file_info;
  40. struct curl_slist *hl;
  41. int error;
  42. struct curl_slist *headerlist=NULL;
  43. const char *buf_1 = "RNFR 505";
  44. const char *buf_2 = "RNTO 505-forreal";
  45. if (!libtest_arg2) {
  46. fprintf(stderr, "Usage: <url> <file-to-upload>\n");
  47. return -1;
  48. }
  49. /* get the file size of the local file */
  50. hd = stat(libtest_arg2, &file_info);
  51. if(hd == -1) {
  52. /* can't open file, bail out */
  53. error = ERRNO;
  54. fprintf(stderr, "stat() failed with error: %d %s\n",
  55. error, strerror(error));
  56. fprintf(stderr, "WARNING: cannot open file %s\n", libtest_arg2);
  57. return -1;
  58. }
  59. if(! file_info.st_size) {
  60. fprintf(stderr, "WARNING: file %s has no size!\n", libtest_arg2);
  61. return -4;
  62. }
  63. /* get a FILE * of the same file, could also be made with
  64. fdopen() from the previous descriptor, but hey this is just
  65. an example! */
  66. hd_src = fopen(libtest_arg2, "rb");
  67. if(NULL == hd_src) {
  68. error = ERRNO;
  69. fprintf(stderr, "fopen() failed with error: %d %s\n",
  70. error, strerror(error));
  71. fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
  72. return -2; /* if this happens things are major weird */
  73. }
  74. if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  75. fprintf(stderr, "curl_global_init() failed\n");
  76. fclose(hd_src);
  77. return TEST_ERR_MAJOR_BAD;
  78. }
  79. /* get a curl handle */
  80. if ((curl = curl_easy_init()) == NULL) {
  81. fprintf(stderr, "curl_easy_init() failed\n");
  82. curl_global_cleanup();
  83. fclose(hd_src);
  84. return TEST_ERR_MAJOR_BAD;
  85. }
  86. /* build a list of commands to pass to libcurl */
  87. if ((hl = curl_slist_append(headerlist, buf_1)) == NULL) {
  88. fprintf(stderr, "curl_slist_append() failed\n");
  89. curl_easy_cleanup(curl);
  90. curl_global_cleanup();
  91. fclose(hd_src);
  92. return TEST_ERR_MAJOR_BAD;
  93. }
  94. if ((headerlist = curl_slist_append(hl, buf_2)) == NULL) {
  95. fprintf(stderr, "curl_slist_append() failed\n");
  96. curl_slist_free_all(hl);
  97. curl_easy_cleanup(curl);
  98. curl_global_cleanup();
  99. fclose(hd_src);
  100. return TEST_ERR_MAJOR_BAD;
  101. }
  102. headerlist = hl;
  103. /* enable uploading */
  104. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  105. /* enable verbose */
  106. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  107. /* specify target */
  108. curl_easy_setopt(curl,CURLOPT_URL, URL);
  109. /* pass in that last of FTP commands to run after the transfer */
  110. curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
  111. /* now specify which file to upload */
  112. curl_easy_setopt(curl, CURLOPT_INFILE, hd_src);
  113. /* and give the size of the upload (optional) */
  114. curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
  115. (curl_off_t)file_info.st_size);
  116. /* Now run off and do what you've been told! */
  117. res = curl_easy_perform(curl);
  118. /* clean up the FTP commands list */
  119. curl_slist_free_all(headerlist);
  120. /* close the local file */
  121. fclose(hd_src);
  122. curl_easy_cleanup(curl);
  123. curl_global_cleanup();
  124. return res;
  125. }