lib541.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * Two FTP uploads, the second with no content sent.
  29. */
  30. int test(char *URL)
  31. {
  32. CURL *curl;
  33. CURLcode res = CURLE_OK;
  34. FILE *hd_src ;
  35. int hd ;
  36. struct_stat file_info;
  37. int error;
  38. if (!libtest_arg2) {
  39. fprintf(stderr, "Usage: <url> <file-to-upload>\n");
  40. return -1;
  41. }
  42. /* get the file size of the local file */
  43. hd = stat(libtest_arg2, &file_info);
  44. if(hd == -1) {
  45. /* can't open file, bail out */
  46. error = ERRNO;
  47. fprintf(stderr, "stat() failed with error: %d %s\n",
  48. error, strerror(error));
  49. fprintf(stderr, "WARNING: cannot open file %s\n", libtest_arg2);
  50. return -1;
  51. }
  52. if(! file_info.st_size) {
  53. fprintf(stderr, "WARNING: file %s has no size!\n", libtest_arg2);
  54. return -4;
  55. }
  56. /* get a FILE * of the same file, could also be made with
  57. fdopen() from the previous descriptor, but hey this is just
  58. an example! */
  59. hd_src = fopen(libtest_arg2, "rb");
  60. if(NULL == hd_src) {
  61. error = ERRNO;
  62. fprintf(stderr, "fopen() failed with error: %d %s\n",
  63. error, strerror(error));
  64. fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
  65. return -2; /* if this happens things are major weird */
  66. }
  67. if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  68. fprintf(stderr, "curl_global_init() failed\n");
  69. fclose(hd_src);
  70. return TEST_ERR_MAJOR_BAD;
  71. }
  72. /* get a curl handle */
  73. if ((curl = curl_easy_init()) == NULL) {
  74. fprintf(stderr, "curl_easy_init() failed\n");
  75. curl_global_cleanup();
  76. fclose(hd_src);
  77. return TEST_ERR_MAJOR_BAD;
  78. }
  79. /* enable uploading */
  80. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  81. /* enable verbose */
  82. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  83. /* specify target */
  84. curl_easy_setopt(curl,CURLOPT_URL, URL);
  85. /* now specify which file to upload */
  86. curl_easy_setopt(curl, CURLOPT_INFILE, hd_src);
  87. /* Now run off and do what you've been told! */
  88. res = curl_easy_perform(curl);
  89. /* and now upload the exact same again, but without rewinding so it already
  90. is at end of file */
  91. res = curl_easy_perform(curl);
  92. /* close the local file */
  93. fclose(hd_src);
  94. curl_easy_cleanup(curl);
  95. curl_global_cleanup();
  96. return res;
  97. }