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