Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / tests / libtest / lib541.c
blobf1f8678394611d2f58e66c66d4a3e19bd253fac2
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: lib541.c,v 1.1.1.1 2008-09-23 16:32:06 hoffman Exp $
9 */
11 #include "setup.h" /* struct_stat etc. */
12 #include "test.h"
14 #ifdef HAVE_SYS_SOCKET_H
15 #include <sys/socket.h>
16 #endif
17 #ifdef HAVE_SYS_TYPES_H
18 #include <sys/types.h>
19 #endif
20 #ifdef HAVE_SYS_STAT_H
21 #include <sys/stat.h>
22 #endif
23 #ifdef HAVE_FCNTL_H
24 #include <fcntl.h>
25 #endif
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
32 * Two FTP uploads, the second with no content sent.
35 int test(char *URL)
37 CURL *curl;
38 CURLcode res = CURLE_OK;
39 FILE *hd_src ;
40 int hd ;
41 struct_stat file_info;
42 int error;
44 if (!libtest_arg2) {
45 fprintf(stderr, "Usage: <url> <file-to-upload>\n");
46 return -1;
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;
60 if(! file_info.st_size) {
61 fprintf(stderr, "WARNING: file %s has no size!\n", libtest_arg2);
62 return -4;
65 /* get a FILE * of the same file, could also be made with
66 fdopen() from the previous descriptor, but hey this is just
67 an example! */
68 hd_src = fopen(libtest_arg2, "rb");
69 if(NULL == hd_src) {
70 error = ERRNO;
71 fprintf(stderr, "fopen() failed with error: %d %s\n",
72 error, strerror(error));
73 fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
74 return -2; /* if this happens things are major weird */
77 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
78 fprintf(stderr, "curl_global_init() failed\n");
79 fclose(hd_src);
80 return TEST_ERR_MAJOR_BAD;
83 /* get a curl handle */
84 if ((curl = curl_easy_init()) == NULL) {
85 fprintf(stderr, "curl_easy_init() failed\n");
86 curl_global_cleanup();
87 fclose(hd_src);
88 return TEST_ERR_MAJOR_BAD;
91 /* enable uploading */
92 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
94 /* enable verbose */
95 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
97 /* specify target */
98 curl_easy_setopt(curl,CURLOPT_URL, URL);
100 /* now specify which file to upload */
101 curl_easy_setopt(curl, CURLOPT_INFILE, hd_src);
103 /* Now run off and do what you've been told! */
104 res = curl_easy_perform(curl);
106 /* and now upload the exact same again, but without rewinding so it already
107 is at end of file */
108 res = curl_easy_perform(curl);
110 /* close the local file */
111 fclose(hd_src);
113 curl_easy_cleanup(curl);
114 curl_global_cleanup();
116 return res;