ftpupload.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 <stdio.h>
00023 #include <string.h>
00024 
00025 #include <curl/curl.h>
00026 #include <sys/types.h>
00027 #include <sys/stat.h>
00028 #include <fcntl.h>
00029 #include <errno.h>
00030 #ifdef WIN32
00031 #include <io.h>
00032 #else
00033 #include <unistd.h>
00034 #endif
00035 
00036 /* <DESC>
00037  * Performs an FTP upload and renames the file just after a successful
00038  * transfer.
00039  * </DESC>
00040  */
00041 
00042 #define LOCAL_FILE      "/tmp/uploadthis.txt"
00043 #define UPLOAD_FILE_AS  "while-uploading.txt"
00044 #define REMOTE_URL      "ftp://example.com/"  UPLOAD_FILE_AS
00045 #define RENAME_FILE_TO  "renamed-and-fine.txt"
00046 
00047 /* NOTE: if you want this example to work on Windows with libcurl as a
00048    DLL, you MUST also provide a read callback with CURLOPT_READFUNCTION.
00049    Failing to do so will give you a crash since a DLL may not use the
00050    variable's memory when passed in to it from an app like this. */
00051 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
00052 {
00053   curl_off_t nread;
00054   /* in real-world cases, this would probably get this data differently
00055      as this fread() stuff is exactly what the library already would do
00056      by default internally */
00057   size_t retcode = fread(ptr, size, nmemb, stream);
00058 
00059   nread = (curl_off_t)retcode;
00060 
00061   fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
00062           " bytes from file\n", nread);
00063   return retcode;
00064 }
00065 
00066 int main(void)
00067 {
00068   CURL *curl;
00069   CURLcode res;
00070   FILE *hd_src;
00071   struct stat file_info;
00072   curl_off_t fsize;
00073 
00074   struct curl_slist *headerlist=NULL;
00075   static const char buf_1 [] = "RNFR " UPLOAD_FILE_AS;
00076   static const char buf_2 [] = "RNTO " RENAME_FILE_TO;
00077 
00078   /* get the file size of the local file */
00079   if(stat(LOCAL_FILE, &file_info)) {
00080     printf("Couldnt open '%s': %s\n", LOCAL_FILE, strerror(errno));
00081     return 1;
00082   }
00083   fsize = (curl_off_t)file_info.st_size;
00084 
00085   printf("Local file size: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", fsize);
00086 
00087   /* get a FILE * of the same file */
00088   hd_src = fopen(LOCAL_FILE, "rb");
00089 
00090   /* In windows, this will init the winsock stuff */
00091   curl_global_init(CURL_GLOBAL_ALL);
00092 
00093   /* get a curl handle */
00094   curl = curl_easy_init();
00095   if(curl) {
00096     /* build a list of commands to pass to libcurl */
00097     headerlist = curl_slist_append(headerlist, buf_1);
00098     headerlist = curl_slist_append(headerlist, buf_2);
00099 
00100     /* we want to use our own read function */
00101     curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
00102 
00103     /* enable uploading */
00104     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
00105 
00106     /* specify target */
00107     curl_easy_setopt(curl, CURLOPT_URL, REMOTE_URL);
00108 
00109     /* pass in that last of FTP commands to run after the transfer */
00110     curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
00111 
00112     /* now specify which file to upload */
00113     curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
00114 
00115     /* Set the size of the file to upload (optional).  If you give a *_LARGE
00116        option you MUST make sure that the type of the passed-in argument is a
00117        curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
00118        make sure that to pass in a type 'long' argument. */
00119     curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
00120                      (curl_off_t)fsize);
00121 
00122     /* Now run off and do what you've been told! */
00123     res = curl_easy_perform(curl);
00124     /* Check for errors */
00125     if(res != CURLE_OK)
00126       fprintf(stderr, "curl_easy_perform() failed: %s\n",
00127               curl_easy_strerror(res));
00128 
00129     /* clean up the FTP commands list */
00130     curl_slist_free_all(headerlist);
00131 
00132     /* always cleanup */
00133     curl_easy_cleanup(curl);
00134   }
00135   fclose(hd_src); /* close the local file */
00136 
00137   curl_global_cleanup();
00138   return 0;
00139 }


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