Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / tests / libtest / lib505.c
blob46ed3ccb98e9a07409890ebd2b2c4bdfac12dcdd
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: lib505.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 * This example shows an FTP upload, with a rename of the file just after
33 * a successful upload.
35 * Example based on source code provided by Erick Nuwendam. Thanks!
38 int test(char *URL)
40 CURL *curl;
41 CURLcode res = CURLE_OK;
42 FILE *hd_src ;
43 int hd ;
44 struct_stat file_info;
45 struct curl_slist *hl;
46 int error;
48 struct curl_slist *headerlist=NULL;
49 const char *buf_1 = "RNFR 505";
50 const char *buf_2 = "RNTO 505-forreal";
52 if (!libtest_arg2) {
53 fprintf(stderr, "Usage: <url> <file-to-upload>\n");
54 return -1;
57 /* get the file size of the local file */
58 hd = stat(libtest_arg2, &file_info);
59 if(hd == -1) {
60 /* can't open file, bail out */
61 error = ERRNO;
62 fprintf(stderr, "stat() failed with error: %d %s\n",
63 error, strerror(error));
64 fprintf(stderr, "WARNING: cannot open file %s\n", libtest_arg2);
65 return -1;
68 if(! file_info.st_size) {
69 fprintf(stderr, "WARNING: file %s has no size!\n", libtest_arg2);
70 return -4;
73 /* get a FILE * of the same file, could also be made with
74 fdopen() from the previous descriptor, but hey this is just
75 an example! */
76 hd_src = fopen(libtest_arg2, "rb");
77 if(NULL == hd_src) {
78 error = ERRNO;
79 fprintf(stderr, "fopen() failed with error: %d %s\n",
80 error, strerror(error));
81 fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
82 return -2; /* if this happens things are major weird */
85 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
86 fprintf(stderr, "curl_global_init() failed\n");
87 fclose(hd_src);
88 return TEST_ERR_MAJOR_BAD;
91 /* get a curl handle */
92 if ((curl = curl_easy_init()) == NULL) {
93 fprintf(stderr, "curl_easy_init() failed\n");
94 curl_global_cleanup();
95 fclose(hd_src);
96 return TEST_ERR_MAJOR_BAD;
99 /* build a list of commands to pass to libcurl */
101 if ((hl = curl_slist_append(headerlist, buf_1)) == NULL) {
102 fprintf(stderr, "curl_slist_append() failed\n");
103 curl_easy_cleanup(curl);
104 curl_global_cleanup();
105 fclose(hd_src);
106 return TEST_ERR_MAJOR_BAD;
108 if ((headerlist = curl_slist_append(hl, buf_2)) == NULL) {
109 fprintf(stderr, "curl_slist_append() failed\n");
110 curl_slist_free_all(hl);
111 curl_easy_cleanup(curl);
112 curl_global_cleanup();
113 fclose(hd_src);
114 return TEST_ERR_MAJOR_BAD;
116 headerlist = hl;
118 /* enable uploading */
119 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
121 /* enable verbose */
122 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
124 /* specify target */
125 curl_easy_setopt(curl,CURLOPT_URL, URL);
127 /* pass in that last of FTP commands to run after the transfer */
128 curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
130 /* now specify which file to upload */
131 curl_easy_setopt(curl, CURLOPT_INFILE, hd_src);
133 /* and give the size of the upload (optional) */
134 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
135 (curl_off_t)file_info.st_size);
137 /* Now run off and do what you've been told! */
138 res = curl_easy_perform(curl);
140 /* clean up the FTP commands list */
141 curl_slist_free_all(headerlist);
143 /* close the local file */
144 fclose(hd_src);
146 curl_easy_cleanup(curl);
147 curl_global_cleanup();
149 return res;