1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: lib505.c,v 1.1.1.1 2008-09-23 16:32:06 hoffman Exp $
11 #include "setup.h" /* struct_stat etc. */
14 #ifdef HAVE_SYS_SOCKET_H
15 #include <sys/socket.h>
17 #ifdef HAVE_SYS_TYPES_H
18 #include <sys/types.h>
20 #ifdef HAVE_SYS_STAT_H
32 * This example shows an FTP upload, with a rename of the file just after
33 * a successful upload.
35 * Example based on source code provided by Erick Nuwendam. Thanks!
41 CURLcode res
= CURLE_OK
;
44 struct_stat file_info
;
45 struct curl_slist
*hl
;
48 struct curl_slist
*headerlist
=NULL
;
49 const char *buf_1
= "RNFR 505";
50 const char *buf_2
= "RNTO 505-forreal";
53 fprintf(stderr
, "Usage: <url> <file-to-upload>\n");
57 /* get the file size of the local file */
58 hd
= stat(libtest_arg2
, &file_info
);
60 /* can't open file, bail out */
62 fprintf(stderr
, "stat() failed with error: %d %s\n",
63 error
, strerror(error
));
64 fprintf(stderr
, "WARNING: cannot open file %s\n", libtest_arg2
);
68 if(! file_info
.st_size
) {
69 fprintf(stderr
, "WARNING: file %s has no size!\n", libtest_arg2
);
73 /* get a FILE * of the same file, could also be made with
74 fdopen() from the previous descriptor, but hey this is just
76 hd_src
= fopen(libtest_arg2
, "rb");
79 fprintf(stderr
, "fopen() failed with error: %d %s\n",
80 error
, strerror(error
));
81 fprintf(stderr
, "Error opening file: %s\n", libtest_arg2
);
82 return -2; /* if this happens things are major weird */
85 if (curl_global_init(CURL_GLOBAL_ALL
) != CURLE_OK
) {
86 fprintf(stderr
, "curl_global_init() failed\n");
88 return TEST_ERR_MAJOR_BAD
;
91 /* get a curl handle */
92 if ((curl
= curl_easy_init()) == NULL
) {
93 fprintf(stderr
, "curl_easy_init() failed\n");
94 curl_global_cleanup();
96 return TEST_ERR_MAJOR_BAD
;
99 /* build a list of commands to pass to libcurl */
101 if ((hl
= curl_slist_append(headerlist
, buf_1
)) == NULL
) {
102 fprintf(stderr
, "curl_slist_append() failed\n");
103 curl_easy_cleanup(curl
);
104 curl_global_cleanup();
106 return TEST_ERR_MAJOR_BAD
;
108 if ((headerlist
= curl_slist_append(hl
, buf_2
)) == NULL
) {
109 fprintf(stderr
, "curl_slist_append() failed\n");
110 curl_slist_free_all(hl
);
111 curl_easy_cleanup(curl
);
112 curl_global_cleanup();
114 return TEST_ERR_MAJOR_BAD
;
118 /* enable uploading */
119 curl_easy_setopt(curl
, CURLOPT_UPLOAD
, 1L);
122 curl_easy_setopt(curl
, CURLOPT_VERBOSE
, 1L);
125 curl_easy_setopt(curl
,CURLOPT_URL
, URL
);
127 /* pass in that last of FTP commands to run after the transfer */
128 curl_easy_setopt(curl
, CURLOPT_POSTQUOTE
, headerlist
);
130 /* now specify which file to upload */
131 curl_easy_setopt(curl
, CURLOPT_INFILE
, hd_src
);
133 /* and give the size of the upload (optional) */
134 curl_easy_setopt(curl
, CURLOPT_INFILESIZE_LARGE
,
135 (curl_off_t
)file_info
.st_size
);
137 /* Now run off and do what you've been told! */
138 res
= curl_easy_perform(curl
);
140 /* clean up the FTP commands list */
141 curl_slist_free_all(headerlist
);
143 /* close the local file */
146 curl_easy_cleanup(curl
);
147 curl_global_cleanup();