doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ftpupload.c
Go to the documentation of this file.
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 */
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 
90  return 0;
91 }
CURLcode curl_global_init(long flags)
Globally initializes cURL given a bitwise set of the different features to initialize.
Definition: easy.c:147
#define O_RDONLY
CURLcode curl_easy_perform(CURL *curl)
Definition: easy.c:260
CURLcode
Definition: curl.h:209
CURLcode curl_easy_setopt(CURL *curl, CURLoption option,...)
Definition: easy.c:217
#define LOCAL_FILE
Definition: ftpupload.c:25
#define RENAME_FILE_TO
Definition: ftpupload.c:28
void curl_global_cleanup(void)
Globally cleanup cURL, uses the value of "init_flags" to determine what needs to be cleaned up and ...
Definition: easy.c:174
int main(int argc, char **argv)
Definition: ftpupload.c:30
#define NULL
Definition: Lib.h:88
#define UPLOAD_FILE_AS
Definition: ftpupload.c:26
struct curl_slist * curl_slist_append(struct curl_slist *, const char *)
Definition: sendf.c:82
void CURL
Definition: types.h:25
void curl_slist_free_all(struct curl_slist *)
Definition: sendf.c:108
GLuint res
Definition: glext.h:5385
#define TRUE
Definition: mprintf.c:69
#define CURL_GLOBAL_ALL
Definition: curl.h:1153
#define REMOTE_URL
Definition: ftpupload.c:27
#define CURLOPT_READDATA
Definition: curl.h:806
void curl_easy_cleanup(CURL *curl)
Definition: easy.c:288
CURL * curl_easy_init(void)
Definition: easy.c:195