1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22#include <stdio.h>
23#include <fcntl.h>
24#include <sys/stat.h>
25#include <unistd.h>
26
27#include <curl/curl.h>
28#include "printf_macro.h"
29
30/*
31 * This example shows a HTTP PUT operation. PUTs a file given as a command
32 * line argument to the URL also given on the command line.
33 *
34 * This example also uses its own read callback.
35 *
36 * Here's an article on how to setup a PUT handler for Apache:
37 * http://www.apacheweek.com/features/put
38 */
39
40static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
41{
42  size_t retcode;
43
44  /* in real-world cases, this would probably get this data differently
45     as this fread() stuff is exactly what the library already would do
46     by default internally */
47  retcode = fread(ptr, size, nmemb, stream);
48
49  fprintf(stderr, "*** We read %" _FMT_SIZE_T " bytes from file\n", retcode);
50
51  return retcode;
52}
53
54int main(int argc, char **argv)
55{
56  CURL *curl;
57  CURLcode res;
58  FILE * hd_src ;
59  int hd ;
60  struct stat file_info;
61
62  char *file;
63  char *url;
64
65  if(argc < 3)
66    return 1;
67
68  file= argv[1];
69  url = argv[2];
70
71  /* get the file size of the local file */
72  hd = open(file, O_RDONLY) ;
73  fstat(hd, &file_info);
74  close(hd) ;
75
76  /* get a FILE * of the same file, could also be made with
77     fdopen() from the previous descriptor, but hey this is just
78     an example! */
79  hd_src = fopen(file, "rb");
80
81  /* In windows, this will init the winsock stuff */
82  curl_global_init(CURL_GLOBAL_ALL);
83
84  /* get a curl handle */
85  curl = curl_easy_init();
86  if(curl) {
87    /* we want to use our own read function */
88    curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
89
90    /* enable uploading */
91    curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
92
93    /* HTTP PUT please */
94    curl_easy_setopt(curl, CURLOPT_PUT, 1L);
95
96    /* specify target URL, and note that this URL should include a file
97       name, not only a directory */
98    curl_easy_setopt(curl, CURLOPT_URL, url);
99
100    /* now specify which file to upload */
101    curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
102
103    /* provide the size of the upload, we specicially typecast the value
104       to curl_off_t since we must be sure to use the correct data size */
105    curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
106                     (curl_off_t)file_info.st_size);
107
108    /* Now run off and do what you've been told! */
109    res = curl_easy_perform(curl);
110
111    /* always cleanup */
112    curl_easy_cleanup(curl);
113  }
114  fclose(hd_src); /* close the local file */
115
116  curl_global_cleanup();
117  return 0;
118}
119