]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/curl/docs/examples/ftpupload.c
hello world
[icculus/iodoom3.git] / neo / curl / docs / examples / ftpupload.c
1 /*****************************************************************************
2  *                                  _   _ ____  _     
3  *  Project                     ___| | | |  _ \| |    
4  *                             / __| | | | |_) | |    
5  *                            | (__| |_| |  _ <| |___ 
6  *                             \___|\___/|_| \_\_____|
7  *
8  * $Id: ftpupload.c,v 1.4 2004/01/05 22:29:30 bagder Exp $
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
18 /*
19  * This example shows an FTP upload, with a rename of the file just after
20  * a successful upload.
21  *
22  * Example based on source code provided by Erick Nuwendam. Thanks!
23  */
24
25 #define LOCAL_FILE      "/tmp/uploadthis.txt"
26 #define UPLOAD_FILE_AS  "while-uploading.txt"
27 #define REMOTE_URL      "ftp://localhost/"  UPLOAD_FILE_AS
28 #define RENAME_FILE_TO  "renamed-and-fine.txt"
29
30 int main(int argc, char **argv)
31 {
32   CURL *curl;
33   CURLcode res;
34   FILE *ftpfile;
35   FILE * hd_src ;
36   int hd ;
37   struct stat file_info;
38
39   struct curl_slist *headerlist=NULL;
40   char buf_1 [] = "RNFR " UPLOAD_FILE_AS;
41   char buf_2 [] = "RNTO " RENAME_FILE_TO;
42
43   /* get the file size of the local file */
44   hd = open(LOCAL_FILE, O_RDONLY) ;
45   fstat(hd, &file_info);
46   close(hd) ;
47
48   /* get a FILE * of the same file, could also be made with
49      fdopen() from the previous descriptor, but hey this is just 
50      an example! */
51   hd_src = fopen(LOCAL_FILE, "rb");
52
53   /* In windows, this will init the winsock stuff */
54   curl_global_init(CURL_GLOBAL_ALL);
55
56   /* get a curl handle */
57   curl = curl_easy_init();
58   if(curl) {
59     /* build a list of commands to pass to libcurl */
60     headerlist = curl_slist_append(headerlist, buf_1);
61     headerlist = curl_slist_append(headerlist, buf_2);
62
63     /* enable uploading */
64     curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;
65
66     /* specify target */
67     curl_easy_setopt(curl,CURLOPT_URL, REMOTE_URL);
68
69     /* pass in that last of FTP commands to run after the transfer */
70     curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
71
72     /* now specify which file to upload */
73     curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
74
75     /* and give the size of the upload (optional) */
76     curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, file_info.st_size);
77
78     /* Now run off and do what you've been told! */
79     res = curl_easy_perform(curl);
80
81     /* clean up the FTP commands list */
82     curl_slist_free_all (headerlist);
83
84     /* always cleanup */
85     curl_easy_cleanup(curl);
86   }
87   fclose(hd_src); /* close the local file */
88
89   curl_global_cleanup();
90   return 0;
91 }