lib505.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 "test.h"
23 
24 #ifdef HAVE_FCNTL_H
25 #include <fcntl.h>
26 #endif
27 
28 #include "memdebug.h"
29 
30 /*
31  * This example shows an FTP upload, with a rename of the file just after
32  * a successful upload.
33  *
34  * Example based on source code provided by Erick Nuwendam. Thanks!
35  */
36 
37 int test(char *URL)
38 {
39  CURL *curl;
41  FILE *hd_src;
42  int hd;
43  struct_stat file_info;
44  struct curl_slist *hl;
45 
46  struct curl_slist *headerlist = NULL;
47  const char *buf_1 = "RNFR 505";
48  const char *buf_2 = "RNTO 505-forreal";
49 
50  if(!libtest_arg2) {
51  fprintf(stderr, "Usage: <url> <file-to-upload>\n");
52  return TEST_ERR_USAGE;
53  }
54 
55  hd_src = fopen(libtest_arg2, "rb");
56  if(NULL == hd_src) {
57  fprintf(stderr, "fopen failed with error: %d %s\n",
58  errno, strerror(errno));
59  fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
60  return TEST_ERR_MAJOR_BAD; /* if this happens things are major weird */
61  }
62 
63  /* get the file size of the local file */
64  hd = fstat(fileno(hd_src), &file_info);
65  if(hd == -1) {
66  /* can't open file, bail out */
67  fprintf(stderr, "fstat() failed with error: %d %s\n",
68  errno, strerror(errno));
69  fprintf(stderr, "ERROR: cannot open file %s\n", libtest_arg2);
70  fclose(hd_src);
71  return TEST_ERR_MAJOR_BAD;
72  }
73 
74  if(! file_info.st_size) {
75  fprintf(stderr, "ERROR: file %s has zero size!\n", libtest_arg2);
76  fclose(hd_src);
77  return TEST_ERR_MAJOR_BAD;
78  }
79 
81  fprintf(stderr, "curl_global_init() failed\n");
82  fclose(hd_src);
83  return TEST_ERR_MAJOR_BAD;
84  }
85 
86  /* get a curl handle */
87  curl = curl_easy_init();
88  if(!curl) {
89  fprintf(stderr, "curl_easy_init() failed\n");
91  fclose(hd_src);
92  return TEST_ERR_MAJOR_BAD;
93  }
94 
95  /* build a list of commands to pass to libcurl */
96 
97  hl = curl_slist_append(headerlist, buf_1);
98  if(!hl) {
99  fprintf(stderr, "curl_slist_append() failed\n");
100  curl_easy_cleanup(curl);
102  fclose(hd_src);
103  return TEST_ERR_MAJOR_BAD;
104  }
105  headerlist = curl_slist_append(hl, buf_2);
106  if(!headerlist) {
107  fprintf(stderr, "curl_slist_append() failed\n");
109  curl_easy_cleanup(curl);
111  fclose(hd_src);
112  return TEST_ERR_MAJOR_BAD;
113  }
114  headerlist = hl;
115 
116  /* enable uploading */
117  test_setopt(curl, CURLOPT_UPLOAD, 1L);
118 
119  /* enable verbose */
120  test_setopt(curl, CURLOPT_VERBOSE, 1L);
121 
122  /* specify target */
123  test_setopt(curl, CURLOPT_URL, URL);
124 
125  /* pass in that last of FTP commands to run after the transfer */
126  test_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
127 
128  /* now specify which file to upload */
129  test_setopt(curl, CURLOPT_READDATA, hd_src);
130 
131  /* and give the size of the upload (optional) */
132  test_setopt(curl, CURLOPT_INFILESIZE_LARGE,
133  (curl_off_t)file_info.st_size);
134 
135  /* Now run off and do what you've been told! */
136  res = curl_easy_perform(curl);
137 
138 test_cleanup:
139 
140  /* clean up the FTP commands list */
141  curl_slist_free_all(headerlist);
142 
143  /* close the local file */
144  fclose(hd_src);
145 
146  curl_easy_cleanup(curl);
148 
149  return res;
150 }
#define test_setopt(A, B, C)
Definition: test.h:47
CURLcode
Definition: curl.h:454
static int res
#define TEST_ERR_MAJOR_BAD
Definition: test.h:85
char * libtest_arg2
Definition: first.c:75
CURL_EXTERN struct curl_slist * curl_slist_append(struct curl_slist *, const char *)
Definition: slist.c:89
CURL_EXTERN CURL * curl_easy_init(void)
Definition: easy.c:343
int test(char *URL)
Definition: lib505.c:37
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
CURL_TYPEOF_CURL_OFF_T curl_off_t
Definition: system.h:420
Definition: curl.h:455
#define TEST_ERR_USAGE
Definition: test.h:93
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
#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 fileno(FILE *stream)
static CURL * curl
Definition: sessioninfo.c:35
CURL_EXTERN void curl_slist_free_all(struct curl_slist *)
Definition: slist.c:129
CURL_EXTERN CURLcode curl_easy_perform(CURL *curl)
#define struct_stat
Definition: curl_setup.h:385


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