Git init
[external/curl.git] / tests / libtest / lib505.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  */
9
10 #include "test.h"
11
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
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28
29 #include "memdebug.h"
30
31 /*
32  * This example shows an FTP upload, with a rename of the file just after
33  * a successful upload.
34  *
35  * Example based on source code provided by Erick Nuwendam. Thanks!
36  */
37
38 int test(char *URL)
39 {
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;
47
48   struct curl_slist *headerlist=NULL;
49   const char *buf_1 = "RNFR 505";
50   const char *buf_2 = "RNTO 505-forreal";
51
52   if (!libtest_arg2) {
53     fprintf(stderr, "Usage: <url> <file-to-upload>\n");
54     return -1;
55   }
56
57   hd_src = fopen(libtest_arg2, "rb");
58   if(NULL == hd_src) {
59     error = ERRNO;
60     fprintf(stderr, "fopen() failed with error: %d %s\n",
61             error, strerror(error));
62     fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
63     return -2; /* if this happens things are major weird */
64   }
65
66   /* get the file size of the local file */
67   hd = fstat(fileno(hd_src), &file_info);
68   if(hd == -1) {
69     /* can't open file, bail out */
70     error = ERRNO;
71     fprintf(stderr, "fstat() failed with error: %d %s\n",
72             error, strerror(error));
73     fprintf(stderr, "ERROR: cannot open file %s\n", libtest_arg2);
74     fclose(hd_src);
75     return -1;
76   }
77
78   if(! file_info.st_size) {
79     fprintf(stderr, "ERROR: file %s has zero size!\n", libtest_arg2);
80     fclose(hd_src);
81     return -4;
82   }
83
84   if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
85     fprintf(stderr, "curl_global_init() failed\n");
86     fclose(hd_src);
87     return TEST_ERR_MAJOR_BAD;
88   }
89
90   /* get a curl handle */
91   if ((curl = curl_easy_init()) == NULL) {
92     fprintf(stderr, "curl_easy_init() failed\n");
93     curl_global_cleanup();
94     fclose(hd_src);
95     return TEST_ERR_MAJOR_BAD;
96   }
97
98   /* build a list of commands to pass to libcurl */
99
100   if ((hl = curl_slist_append(headerlist, buf_1)) == NULL) {
101     fprintf(stderr, "curl_slist_append() failed\n");
102     curl_easy_cleanup(curl);
103     curl_global_cleanup();
104     fclose(hd_src);
105     return TEST_ERR_MAJOR_BAD;
106   }
107   if ((headerlist = curl_slist_append(hl, buf_2)) == NULL) {
108     fprintf(stderr, "curl_slist_append() failed\n");
109     curl_slist_free_all(hl);
110     curl_easy_cleanup(curl);
111     curl_global_cleanup();
112     fclose(hd_src);
113     return TEST_ERR_MAJOR_BAD;
114   }
115   headerlist = hl;
116
117   /* enable uploading */
118   test_setopt(curl, CURLOPT_UPLOAD, 1L);
119
120   /* enable verbose */
121   test_setopt(curl, CURLOPT_VERBOSE, 1L);
122
123   /* specify target */
124   test_setopt(curl,CURLOPT_URL, URL);
125
126   /* pass in that last of FTP commands to run after the transfer */
127   test_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
128
129   /* now specify which file to upload */
130   test_setopt(curl, CURLOPT_INFILE, hd_src);
131
132   /* and give the size of the upload (optional) */
133   test_setopt(curl, CURLOPT_INFILESIZE_LARGE,
134                    (curl_off_t)file_info.st_size);
135
136   /* Now run off and do what you've been told! */
137   res = curl_easy_perform(curl);
138
139 test_cleanup:
140
141   /* clean up the FTP commands list */
142   curl_slist_free_all(headerlist);
143
144   /* close the local file */
145   fclose(hd_src);
146
147   curl_easy_cleanup(curl);
148   curl_global_cleanup();
149
150   return res;
151 }