]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/curl/docs/examples/httpput.c
hello world
[icculus/iodoom3.git] / neo / curl / docs / examples / httpput.c
1 /*****************************************************************************
2  *                                  _   _ ____  _     
3  *  Project                     ___| | | |  _ \| |    
4  *                             / __| | | | |_) | |    
5  *                            | (__| |_| |  _ <| |___ 
6  *                             \___|\___/|_| \_\_____|
7  *
8  * $Id: httpput.c,v 1.5 2004/01/05 22:29:30 bagder Exp $
9  */
10
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <sys/stat.h>
14
15 #include <curl/curl.h>
16
17 /*
18  * This example shows a HTTP PUT operation. PUTs a file given as a command
19  * line argument to the URL also given on the command line.
20  *
21  * This example also uses its own read callback.
22  */
23
24 size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
25 {
26   size_t retcode;
27
28   /* in real-world cases, this would probably get this data differently
29      as this fread() stuff is exactly what the library already would do
30      by default internally */
31   retcode = fread(ptr, size, nmemb, stream);
32
33   fprintf(stderr, "*** We read %d bytes from file\n", retcode);
34
35   return retcode;
36 }
37
38 int main(int argc, char **argv)
39 {
40   CURL *curl;
41   CURLcode res;
42   FILE *ftpfile;
43   FILE * hd_src ;
44   int hd ;
45   struct stat file_info;
46
47   char *file;
48   char *url;
49
50   if(argc < 3)
51     return 1;
52  
53   file= argv[1];
54   url = argv[2];
55   
56   /* get the file size of the local file */
57   hd = open(file, O_RDONLY) ;
58   fstat(hd, &file_info);
59   close(hd) ;
60
61   /* get a FILE * of the same file, could also be made with
62      fdopen() from the previous descriptor, but hey this is just 
63      an example! */
64   hd_src = fopen(file, "rb");
65
66   /* In windows, this will init the winsock stuff */
67   curl_global_init(CURL_GLOBAL_ALL);
68
69   /* get a curl handle */
70   curl = curl_easy_init();
71   if(curl) {
72     /* we want to use our own read function */
73     curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
74
75     /* enable uploading */
76     curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;
77
78     /* HTTP PUT please */
79     curl_easy_setopt(curl, CURLOPT_PUT, TRUE);
80
81     /* specify target URL, and note that this URL should include a file
82        name, not only a directory */
83     curl_easy_setopt(curl,CURLOPT_URL, url);
84
85     /* now specify which file to upload */
86     curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
87
88     /* and give the size of the upload */
89     curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, file_info.st_size);
90
91     /* Now run off and do what you've been told! */
92     res = curl_easy_perform(curl);
93
94     /* always cleanup */
95     curl_easy_cleanup(curl);
96   }
97   fclose(hd_src); /* close the local file */
98
99   curl_global_cleanup();
100   return 0;
101 }