lib505.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 #include "test.h"
00023 
00024 #ifdef HAVE_FCNTL_H
00025 #include <fcntl.h>
00026 #endif
00027 
00028 #include "memdebug.h"
00029 
00030 /*
00031  * This example shows an FTP upload, with a rename of the file just after
00032  * a successful upload.
00033  *
00034  * Example based on source code provided by Erick Nuwendam. Thanks!
00035  */
00036 
00037 int test(char *URL)
00038 {
00039   CURL *curl;
00040   CURLcode res = CURLE_OK;
00041   FILE *hd_src;
00042   int hd;
00043   struct_stat file_info;
00044   struct curl_slist *hl;
00045   int error;
00046 
00047   struct curl_slist *headerlist=NULL;
00048   const char *buf_1 = "RNFR 505";
00049   const char *buf_2 = "RNTO 505-forreal";
00050 
00051   if(!libtest_arg2) {
00052     fprintf(stderr, "Usage: <url> <file-to-upload>\n");
00053     return TEST_ERR_USAGE;
00054   }
00055 
00056   hd_src = fopen(libtest_arg2, "rb");
00057   if(NULL == hd_src) {
00058     error = ERRNO;
00059     fprintf(stderr, "fopen failed with error: %d %s\n",
00060             error, strerror(error));
00061     fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
00062     return TEST_ERR_MAJOR_BAD; /* if this happens things are major weird */
00063   }
00064 
00065   /* get the file size of the local file */
00066   hd = fstat(fileno(hd_src), &file_info);
00067   if(hd == -1) {
00068     /* can't open file, bail out */
00069     error = ERRNO;
00070     fprintf(stderr, "fstat() failed with error: %d %s\n",
00071             error, strerror(error));
00072     fprintf(stderr, "ERROR: cannot open file %s\n", libtest_arg2);
00073     fclose(hd_src);
00074     return TEST_ERR_MAJOR_BAD;
00075   }
00076 
00077   if(! file_info.st_size) {
00078     fprintf(stderr, "ERROR: file %s has zero size!\n", libtest_arg2);
00079     fclose(hd_src);
00080     return TEST_ERR_MAJOR_BAD;
00081   }
00082 
00083   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
00084     fprintf(stderr, "curl_global_init() failed\n");
00085     fclose(hd_src);
00086     return TEST_ERR_MAJOR_BAD;
00087   }
00088 
00089   /* get a curl handle */
00090   curl = curl_easy_init();
00091   if(!curl) {
00092     fprintf(stderr, "curl_easy_init() failed\n");
00093     curl_global_cleanup();
00094     fclose(hd_src);
00095     return TEST_ERR_MAJOR_BAD;
00096   }
00097 
00098   /* build a list of commands to pass to libcurl */
00099 
00100   hl = curl_slist_append(headerlist, buf_1);
00101   if(!hl) {
00102     fprintf(stderr, "curl_slist_append() failed\n");
00103     curl_easy_cleanup(curl);
00104     curl_global_cleanup();
00105     fclose(hd_src);
00106     return TEST_ERR_MAJOR_BAD;
00107   }
00108   headerlist = curl_slist_append(hl, buf_2);
00109   if(!headerlist) {
00110     fprintf(stderr, "curl_slist_append() failed\n");
00111     curl_slist_free_all(hl);
00112     curl_easy_cleanup(curl);
00113     curl_global_cleanup();
00114     fclose(hd_src);
00115     return TEST_ERR_MAJOR_BAD;
00116   }
00117   headerlist = hl;
00118 
00119   /* enable uploading */
00120   test_setopt(curl, CURLOPT_UPLOAD, 1L);
00121 
00122   /* enable verbose */
00123   test_setopt(curl, CURLOPT_VERBOSE, 1L);
00124 
00125   /* specify target */
00126   test_setopt(curl, CURLOPT_URL, URL);
00127 
00128   /* pass in that last of FTP commands to run after the transfer */
00129   test_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
00130 
00131   /* now specify which file to upload */
00132   test_setopt(curl, CURLOPT_READDATA, hd_src);
00133 
00134   /* and give the size of the upload (optional) */
00135   test_setopt(curl, CURLOPT_INFILESIZE_LARGE,
00136                    (curl_off_t)file_info.st_size);
00137 
00138   /* Now run off and do what you've been told! */
00139   res = curl_easy_perform(curl);
00140 
00141 test_cleanup:
00142 
00143   /* clean up the FTP commands list */
00144   curl_slist_free_all(headerlist);
00145 
00146   /* close the local file */
00147   fclose(hd_src);
00148 
00149   curl_easy_cleanup(curl);
00150   curl_global_cleanup();
00151 
00152   return res;
00153 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:05