ftpupload.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 #include <stdio.h>
23 #include <string.h>
24 
25 #include <curl/curl.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #ifdef WIN32
31 #include <io.h>
32 #else
33 #include <unistd.h>
34 #endif
35 
36 /* <DESC>
37  * Performs an FTP upload and renames the file just after a successful
38  * transfer.
39  * </DESC>
40  */
41 
42 #define LOCAL_FILE "/tmp/uploadthis.txt"
43 #define UPLOAD_FILE_AS "while-uploading.txt"
44 #define REMOTE_URL "ftp://example.com/" UPLOAD_FILE_AS
45 #define RENAME_FILE_TO "renamed-and-fine.txt"
46 
47 /* NOTE: if you want this example to work on Windows with libcurl as a
48  DLL, you MUST also provide a read callback with CURLOPT_READFUNCTION.
49  Failing to do so will give you a crash since a DLL may not use the
50  variable's memory when passed in to it from an app like this. */
51 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
52 {
53  curl_off_t nread;
54  /* in real-world cases, this would probably get this data differently
55  as this fread() stuff is exactly what the library already would do
56  by default internally */
57  size_t retcode = fread(ptr, size, nmemb, stream);
58 
59  nread = (curl_off_t)retcode;
60 
61  fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
62  " bytes from file\n", nread);
63  return retcode;
64 }
65 
66 int main(void)
67 {
68  CURL *curl;
69  CURLcode res;
70  FILE *hd_src;
71  struct stat file_info;
72  curl_off_t fsize;
73 
74  struct curl_slist *headerlist = NULL;
75  static const char buf_1 [] = "RNFR " UPLOAD_FILE_AS;
76  static const char buf_2 [] = "RNTO " RENAME_FILE_TO;
77 
78  /* get the file size of the local file */
79  if(stat(LOCAL_FILE, &file_info)) {
80  printf("Couldnt open '%s': %s\n", LOCAL_FILE, strerror(errno));
81  return 1;
82  }
83  fsize = (curl_off_t)file_info.st_size;
84 
85  printf("Local file size: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", fsize);
86 
87  /* get a FILE * of the same file */
88  hd_src = fopen(LOCAL_FILE, "rb");
89 
90  /* In windows, this will init the winsock stuff */
92 
93  /* get a curl handle */
94  curl = curl_easy_init();
95  if(curl) {
96  /* build a list of commands to pass to libcurl */
97  headerlist = curl_slist_append(headerlist, buf_1);
98  headerlist = curl_slist_append(headerlist, buf_2);
99 
100  /* we want to use our own read function */
101  curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
102 
103  /* enable uploading */
104  curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
105 
106  /* specify target */
107  curl_easy_setopt(curl, CURLOPT_URL, REMOTE_URL);
108 
109  /* pass in that last of FTP commands to run after the transfer */
110  curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
111 
112  /* now specify which file to upload */
113  curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
114 
115  /* Set the size of the file to upload (optional). If you give a *_LARGE
116  option you MUST make sure that the type of the passed-in argument is a
117  curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
118  make sure that to pass in a type 'long' argument. */
119  curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
120  (curl_off_t)fsize);
121 
122  /* Now run off and do what you've been told! */
123  res = curl_easy_perform(curl);
124  /* Check for errors */
125  if(res != CURLE_OK)
126  fprintf(stderr, "curl_easy_perform() failed: %s\n",
127  curl_easy_strerror(res));
128 
129  /* clean up the FTP commands list */
130  curl_slist_free_all(headerlist);
131 
132  /* always cleanup */
133  curl_easy_cleanup(curl);
134  }
135  fclose(hd_src); /* close the local file */
136 
138  return 0;
139 }
int stat(const char *path, struct stat *buffer)
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
FILE * stream
Definition: ftpget.c:33
#define LOCAL_FILE
Definition: ftpupload.c:42
#define RENAME_FILE_TO
Definition: ftpupload.c:45
CURL_EXTERN struct curl_slist * curl_slist_append(struct curl_slist *, const char *)
Definition: slist.c:89
#define printf
Definition: curl_printf.h:40
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
Definition: curl.h:455
size_t fread(void *, size_t, size_t, FILE *)
#define UPLOAD_FILE_AS
Definition: ftpupload.c:43
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
static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
Definition: ftpupload.c:51
size_t size
Definition: unit1302.c:52
#define fprintf
Definition: curl_printf.h:41
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
#define CURL_GLOBAL_ALL
Definition: curl.h:2519
int main(void)
Definition: ftpupload.c:66
static CURL * curl
Definition: sessioninfo.c:35
#define REMOTE_URL
Definition: ftpupload.c:44
CURL_EXTERN const char * curl_easy_strerror(CURLcode)
Definition: strerror.c:57
CURL_EXTERN void curl_slist_free_all(struct curl_slist *)
Definition: slist.c:129
CURL_EXTERN CURLcode curl_easy_perform(CURL *curl)
#define CURL_FORMAT_CURL_OFF_T
Definition: system.h:373


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