sources: update source headers
[platform/upstream/curl.git] / tests / libtest / lib505.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at http://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22 #include "test.h"
23
24 #ifdef HAVE_SYS_SOCKET_H
25 #include <sys/socket.h>
26 #endif
27 #ifdef HAVE_SYS_TYPES_H
28 #include <sys/types.h>
29 #endif
30 #ifdef HAVE_SYS_STAT_H
31 #include <sys/stat.h>
32 #endif
33 #ifdef HAVE_FCNTL_H
34 #include <fcntl.h>
35 #endif
36
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40
41 #include "memdebug.h"
42
43 /*
44  * This example shows an FTP upload, with a rename of the file just after
45  * a successful upload.
46  *
47  * Example based on source code provided by Erick Nuwendam. Thanks!
48  */
49
50 int test(char *URL)
51 {
52   CURL *curl;
53   CURLcode res = CURLE_OK;
54   FILE *hd_src ;
55   int hd ;
56   struct_stat file_info;
57   struct curl_slist *hl;
58   int error;
59
60   struct curl_slist *headerlist=NULL;
61   const char *buf_1 = "RNFR 505";
62   const char *buf_2 = "RNTO 505-forreal";
63
64   if (!libtest_arg2) {
65     fprintf(stderr, "Usage: <url> <file-to-upload>\n");
66     return -1;
67   }
68
69   hd_src = fopen(libtest_arg2, "rb");
70   if(NULL == hd_src) {
71     error = ERRNO;
72     fprintf(stderr, "fopen() failed with error: %d %s\n",
73             error, strerror(error));
74     fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
75     return -2; /* if this happens things are major weird */
76   }
77
78   /* get the file size of the local file */
79   hd = fstat(fileno(hd_src), &file_info);
80   if(hd == -1) {
81     /* can't open file, bail out */
82     error = ERRNO;
83     fprintf(stderr, "fstat() failed with error: %d %s\n",
84             error, strerror(error));
85     fprintf(stderr, "ERROR: cannot open file %s\n", libtest_arg2);
86     fclose(hd_src);
87     return -1;
88   }
89
90   if(! file_info.st_size) {
91     fprintf(stderr, "ERROR: file %s has zero size!\n", libtest_arg2);
92     fclose(hd_src);
93     return -4;
94   }
95
96   if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
97     fprintf(stderr, "curl_global_init() failed\n");
98     fclose(hd_src);
99     return TEST_ERR_MAJOR_BAD;
100   }
101
102   /* get a curl handle */
103   if ((curl = curl_easy_init()) == NULL) {
104     fprintf(stderr, "curl_easy_init() failed\n");
105     curl_global_cleanup();
106     fclose(hd_src);
107     return TEST_ERR_MAJOR_BAD;
108   }
109
110   /* build a list of commands to pass to libcurl */
111
112   if ((hl = curl_slist_append(headerlist, buf_1)) == NULL) {
113     fprintf(stderr, "curl_slist_append() failed\n");
114     curl_easy_cleanup(curl);
115     curl_global_cleanup();
116     fclose(hd_src);
117     return TEST_ERR_MAJOR_BAD;
118   }
119   if ((headerlist = curl_slist_append(hl, buf_2)) == NULL) {
120     fprintf(stderr, "curl_slist_append() failed\n");
121     curl_slist_free_all(hl);
122     curl_easy_cleanup(curl);
123     curl_global_cleanup();
124     fclose(hd_src);
125     return TEST_ERR_MAJOR_BAD;
126   }
127   headerlist = hl;
128
129   /* enable uploading */
130   test_setopt(curl, CURLOPT_UPLOAD, 1L);
131
132   /* enable verbose */
133   test_setopt(curl, CURLOPT_VERBOSE, 1L);
134
135   /* specify target */
136   test_setopt(curl,CURLOPT_URL, URL);
137
138   /* pass in that last of FTP commands to run after the transfer */
139   test_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
140
141   /* now specify which file to upload */
142   test_setopt(curl, CURLOPT_INFILE, hd_src);
143
144   /* and give the size of the upload (optional) */
145   test_setopt(curl, CURLOPT_INFILESIZE_LARGE,
146                    (curl_off_t)file_info.st_size);
147
148   /* Now run off and do what you've been told! */
149   res = curl_easy_perform(curl);
150
151 test_cleanup:
152
153   /* clean up the FTP commands list */
154   curl_slist_free_all(headerlist);
155
156   /* close the local file */
157   fclose(hd_src);
158
159   curl_easy_cleanup(curl);
160   curl_global_cleanup();
161
162   return res;
163 }