ftpuploadfrommem.c
Go to the documentation of this file.
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 */
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  }
123  return 0;
124 }
size_t sizeleft
UNITTEST_START char * ptr
Definition: unit1330.c:38
CURLcode
Definition: curl.h:454
static int res
#define curl_easy_setopt(handle, option, value)
Definition: typecheck-gcc.h:41
memcpy(filename, filename1, strlen(filename1))
static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
static int upload(CURL *curlhandle, const char *remotepath, const char *localpath, long timeout, long tries)
#define CURL_GLOBAL_DEFAULT
Definition: curl.h:2521
CURL_EXTERN CURL * curl_easy_init(void)
Definition: easy.c:343
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
CURL_TYPEOF_CURL_OFF_T curl_off_t
Definition: system.h:420
int main(void)
Definition: curl.h:455
const char * readptr
CURL_EXTERN CURLcode curl_global_init(long flags)
curl_global_init() globally initializes curl given a bitwise set of the different features of what to...
Definition: easy.c:271
void CURL
Definition: curl.h:102
size_t size
Definition: unit1302.c:52
#define fprintf
Definition: curl_printf.h:41
static const char data[]
CURL_EXTERN void curl_global_cleanup(void)
curl_global_cleanup() globally cleanups curl, uses the value of "init_flags" to determine what needs ...
Definition: easy.c:312
double max(double a, double b)
static CURL * curl
Definition: sessioninfo.c:35
CURL_EXTERN const char * curl_easy_strerror(CURLcode)
Definition: strerror.c:57
Definition: debug.c:29
CURL_EXTERN CURLcode curl_easy_perform(CURL *curl)


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:09