made changes to work with Win32;
[platform/upstream/curl.git] / docs / examples / ftpupload.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * $Id$
9  */
10
11 #include <stdio.h>
12
13 #include <curl/curl.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #ifdef WIN32
18 #include <io.h>
19 #else
20 #include <unistd.h>
21 #endif
22
23 /*
24  * This example shows an FTP upload, with a rename of the file just after
25  * a successful upload.
26  *
27  * Example based on source code provided by Erick Nuwendam. Thanks!
28  */
29
30 #define LOCAL_FILE      "/tmp/uploadthis.txt"
31 #define UPLOAD_FILE_AS  "while-uploading.txt"
32 #define REMOTE_URL      "ftp://localhost/"  UPLOAD_FILE_AS
33 #define RENAME_FILE_TO  "renamed-and-fine.txt"
34
35 int main(int argc, char **argv)
36 {
37   CURL *curl;
38   CURLcode res;
39   FILE *hd_src;
40   struct stat file_info;
41
42   struct curl_slist *headerlist=NULL;
43   static const char buf_1 [] = "RNFR " UPLOAD_FILE_AS;
44   static const char buf_2 [] = "RNTO " RENAME_FILE_TO;
45
46   /* get the file size of the local file */
47   if (stat(LOCAL_FILE, &file_info)) {
48     printf("Couldnt open '%s': %s\n", LOCAL_FILE, strerror(errno));
49     exit(1);
50   }
51   printf("Local file size: %ld bytes.\n", file_info.st_size);
52
53   /* get a FILE * of the same file */
54   hd_src = fopen(LOCAL_FILE, "rb");
55
56   /* In windows, this will init the winsock stuff */
57   curl_global_init(CURL_GLOBAL_ALL);
58
59   /* get a curl handle */
60   curl = curl_easy_init();
61   if(curl) {
62     /* build a list of commands to pass to libcurl */
63     headerlist = curl_slist_append(headerlist, buf_1);
64     headerlist = curl_slist_append(headerlist, buf_2);
65
66     /* enable uploading */
67     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1) ;
68
69     /* specify target */
70     curl_easy_setopt(curl,CURLOPT_URL, REMOTE_URL);
71
72     /* pass in that last of FTP commands to run after the transfer */
73     curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
74
75     /* now specify which file to upload */
76     curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
77
78     /* NOTE: if you want this example to work on Windows with libcurl as a
79        DLL, you MUST also provide a read callback with
80        CURLOPT_READFUNCTION. Failing to do so will give you a crash since a
81        DLL may not use the variable's memory when passed in to it from an app
82        like this. */
83
84     /* Set the size of the file to upload (optional).  If you give a *_LARGE
85        option you MUST make sure that the type of the passed-in argument is a
86        curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
87        make sure that to pass in a type 'long' argument. */
88     curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
89                      (curl_off_t)file_info.st_size);
90
91     /* Now run off and do what you've been told! */
92     res = curl_easy_perform(curl);
93
94     /* clean up the FTP commands list */
95     curl_slist_free_all (headerlist);
96
97     /* always cleanup */
98     curl_easy_cleanup(curl);
99   }
100   fclose(hd_src); /* close the local file */
101
102   curl_global_cleanup();
103   return 0;
104 }