Annotation of embedaddon/curl/docs/examples/ftpuploadfrommem.c, revision 1.1.1.1

1.1       misho       1: /***************************************************************************
                      2:  *                                  _   _ ____  _
                      3:  *  Project                     ___| | | |  _ \| |
                      4:  *                             / __| | | | |_) | |
                      5:  *                            | (__| |_| |  _ <| |___
                      6:  *                             \___|\___/|_| \_\_____|
                      7:  *
                      8:  * Copyright (C) 1998 - 2017, 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 https://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: /* <DESC>
                     23:  * FTP upload a file from memory
                     24:  * </DESC>
                     25:  */
                     26: #include <stdio.h>
                     27: #include <string.h>
                     28: #include <curl/curl.h>
                     29: 
                     30: static const char data[]=
                     31:   "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
                     32:   "Nam rhoncus odio id venenatis volutpat. Vestibulum dapibus "
                     33:   "bibendum ullamcorper. Maecenas finibus elit augue, vel "
                     34:   "condimentum odio maximus nec. In hac habitasse platea dictumst. "
                     35:   "Vestibulum vel dolor et turpis rutrum finibus ac at nulla. "
                     36:   "Vivamus nec neque ac elit blandit pretium vitae maximus ipsum. "
                     37:   "Quisque sodales magna vel erat auctor, sed pellentesque nisi "
                     38:   "rhoncus. Donec vehicula maximus pretium. Aliquam eu tincidunt "
                     39:   "lorem.";
                     40: 
                     41: struct WriteThis {
                     42:   const char *readptr;
                     43:   size_t sizeleft;
                     44: };
                     45: 
                     46: static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
                     47: {
                     48:   struct WriteThis *upload = (struct WriteThis *)userp;
                     49:   size_t max = size*nmemb;
                     50: 
                     51:   if(max < 1)
                     52:     return 0;
                     53: 
                     54:   if(upload->sizeleft) {
                     55:     size_t copylen = max;
                     56:     if(copylen > upload->sizeleft)
                     57:       copylen = upload->sizeleft;
                     58:     memcpy(ptr, upload->readptr, copylen);
                     59:     upload->readptr += copylen;
                     60:     upload->sizeleft -= copylen;
                     61:     return copylen;
                     62:   }
                     63: 
                     64:   return 0;                          /* no more data left to deliver */
                     65: }
                     66: 
                     67: int main(void)
                     68: {
                     69:   CURL *curl;
                     70:   CURLcode res;
                     71: 
                     72:   struct WriteThis upload;
                     73: 
                     74:   upload.readptr = data;
                     75:   upload.sizeleft = strlen(data);
                     76: 
                     77:   /* In windows, this will init the winsock stuff */
                     78:   res = curl_global_init(CURL_GLOBAL_DEFAULT);
                     79:   /* Check for errors */
                     80:   if(res != CURLE_OK) {
                     81:     fprintf(stderr, "curl_global_init() failed: %s\n",
                     82:             curl_easy_strerror(res));
                     83:     return 1;
                     84:   }
                     85: 
                     86:   /* get a curl handle */
                     87:   curl = curl_easy_init();
                     88:   if(curl) {
                     89:     /* First set the URL, the target file */
                     90:     curl_easy_setopt(curl, CURLOPT_URL,
                     91:                      "ftp://example.com/path/to/upload/file");
                     92: 
                     93:     /* User and password for the FTP login */
                     94:     curl_easy_setopt(curl, CURLOPT_USERPWD, "login:secret");
                     95: 
                     96:     /* Now specify we want to UPLOAD data */
                     97:     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
                     98: 
                     99:     /* we want to use our own read function */
                    100:     curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
                    101: 
                    102:     /* pointer to pass to our read function */
                    103:     curl_easy_setopt(curl, CURLOPT_READDATA, &upload);
                    104: 
                    105:     /* get verbose debug output please */
                    106:     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
                    107: 
                    108:     /* Set the expected upload size. */
                    109:     curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
                    110:                      (curl_off_t)upload.sizeleft);
                    111: 
                    112:     /* Perform the request, res will get the return code */
                    113:     res = curl_easy_perform(curl);
                    114:     /* Check for errors */
                    115:     if(res != CURLE_OK)
                    116:       fprintf(stderr, "curl_easy_perform() failed: %s\n",
                    117:               curl_easy_strerror(res));
                    118: 
                    119:     /* always cleanup */
                    120:     curl_easy_cleanup(curl);
                    121:   }
                    122:   curl_global_cleanup();
                    123:   return 0;
                    124: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>