blob: 821e95fd8844074382b77d12c57d4201e638418a [file] [log] [blame]
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001/*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 */
9
10#include <stdio.h>
11#include <fcntl.h>
12#include <sys/stat.h>
13#include <unistd.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 * Here's an article on how to setup a PUT handler for Apache:
24 * http://www.apacheweek.com/features/put
25 */
26
27static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
28{
29 size_t retcode;
30
31 /* in real-world cases, this would probably get this data differently
32 as this fread() stuff is exactly what the library already would do
33 by default internally */
34 retcode = fread(ptr, size, nmemb, stream);
35
36 fprintf(stderr, "*** We read %d bytes from file\n", retcode);
37
38 return retcode;
39}
40
41int main(int argc, char **argv)
42{
43 CURL *curl;
44 CURLcode res;
45 FILE * hd_src ;
46 int hd ;
47 struct stat file_info;
48
49 char *file;
50 char *url;
51
52 if(argc < 3)
53 return 1;
54
55 file= argv[1];
56 url = argv[2];
57
58 /* get the file size of the local file */
59 hd = open(file, O_RDONLY) ;
60 fstat(hd, &file_info);
61 close(hd) ;
62
63 /* get a FILE * of the same file, could also be made with
64 fdopen() from the previous descriptor, but hey this is just
65 an example! */
66 hd_src = fopen(file, "rb");
67
68 /* In windows, this will init the winsock stuff */
69 curl_global_init(CURL_GLOBAL_ALL);
70
71 /* get a curl handle */
72 curl = curl_easy_init();
73 if(curl) {
74 /* we want to use our own read function */
75 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
76
77 /* enable uploading */
78 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
79
80 /* HTTP PUT please */
81 curl_easy_setopt(curl, CURLOPT_PUT, 1L);
82
83 /* specify target URL, and note that this URL should include a file
84 name, not only a directory */
85 curl_easy_setopt(curl, CURLOPT_URL, url);
86
87 /* now specify which file to upload */
88 curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
89
90 /* provide the size of the upload, we specicially typecast the value
91 to curl_off_t since we must be sure to use the correct data size */
92 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
93 (curl_off_t)file_info.st_size);
94
95 /* Now run off and do what you've been told! */
96 res = curl_easy_perform(curl);
97
98 /* always cleanup */
99 curl_easy_cleanup(curl);
100 }
101 fclose(hd_src); /* close the local file */
102
103 curl_global_cleanup();
104 return 0;
105}