doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ftpget.c
Go to the documentation of this file.
1 /*****************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * $Id: ftpget.c,v 1.3 2003/12/08 14:13:19 bagder Exp $
9  */
10 
11 #include <stdio.h>
12 
13 #include <curl/curl.h>
14 #include <curl/types.h>
15 #include <curl/easy.h>
16 
17 /*
18  * This is an example showing how to get a single file from an FTP server.
19  * It delays the actual destination file creation until the first write
20  * callback so that it won't create an empty file in case the remote file
21  * doesn't exist or something else fails.
22  */
23 
24 struct FtpFile {
25  char *filename;
26  FILE *stream;
27 };
28 
29 int my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
30 {
31  struct FtpFile *out=(struct FtpFile *)stream;
32  if(out && !out->stream) {
33  /* open file for writing */
34  out->stream=fopen(out->filename, "wb");
35  if(!out->stream)
36  return -1; /* failure, can't open file to write */
37  }
38  return fwrite(buffer, size, nmemb, out->stream);
39 }
40 
41 
42 int main(void)
43 {
44  CURL *curl;
45  CURLcode res;
46  struct FtpFile ftpfile={
47  "curl.tar.gz", /* name to store the file as if succesful */
48  NULL
49  };
50 
52 
53  curl = curl_easy_init();
54  if(curl) {
55  /* Get curl 7.9.2 from sunet.se's FTP site: */
56  curl_easy_setopt(curl, CURLOPT_URL,
57  "ftp://ftp.sunet.se/pub/www/utilities/curl/curl-7.9.2.tar.gz");
58  /* Define our callback to get called when there's data to be written */
59  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
60  /* Set a pointer to our struct to pass to the callback */
61  curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
62 
63  /* Switch on full protocol/debug output */
64  curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE);
65 
66  res = curl_easy_perform(curl);
67 
68  /* always cleanup */
69  curl_easy_cleanup(curl);
70 
71  if(CURLE_OK != res) {
72  /* we failed */
73  fprintf(stderr, "curl told us %d\n", res);
74  }
75  }
76 
77  if(ftpfile.stream)
78  fclose(ftpfile.stream); /* close the local file */
79 
81 
82  return 0;
83 }
Definition: ftpget.c:24
CURLcode curl_global_init(long flags)
Globally initializes cURL given a bitwise set of the different features to initialize.
Definition: easy.c:147
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
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
#define NULL
Definition: Lib.h:88
GLuint buffer
Definition: glext.h:3108
#define CURL_GLOBAL_DEFAULT
Definition: curl.h:1155
int main(void)
Definition: ftpget.c:42
Definition: curl.h:210
GLsizeiptr size
Definition: glext.h:3112
void CURL
Definition: types.h:25
GLuint res
Definition: glext.h:5385
#define TRUE
Definition: mprintf.c:69
void curl_easy_cleanup(CURL *curl)
Definition: easy.c:288
#define CURLOPT_WRITEDATA
Definition: curl.h:805
size_t fwrite(const void *, size_t, size_t, FILE *)
char * filename
Definition: ftpget.c:25
CURL * curl_easy_init(void)
Definition: easy.c:195
int my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
Definition: ftpget.c:29
FILE * stream
Definition: ftpget.c:26