1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2012, 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
29/*
30 * This example shows a HTTP PUT operation. PUTs a file given as a command
31 * line argument to the URL also given on the command line.
32 *
33 * This example also uses its own read callback.
34 *
35 * Here's an article on how to setup a PUT handler for Apache:
36 * http://www.apacheweek.com/features/put
37 */
38
39static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
40{
41  size_t retcode;
42  curl_off_t nread;
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  nread = (curl_off_t)retcode;
50
51  fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
52          " bytes from file\n", nread);
53
54  return retcode;
55}
56
57int main(int argc, char **argv)
58{
59  CURL *curl;
60  CURLcode res;
61  FILE * hd_src ;
62  int hd ;
63  struct stat file_info;
64
65  char *file;
66  char *url;
67
68  if(argc < 3)
69    return 1;
70
71  file= argv[1];
72  url = argv[2];
73
74  /* get the file size of the local file */
75  hd = open(file, O_RDONLY) ;
76  fstat(hd, &file_info);
77  close(hd) ;
78
79  /* get a FILE * of the same file, could also be made with
80     fdopen() from the previous descriptor, but hey this is just
81     an example! */
82  hd_src = fopen(file, "rb");
83
84  /* In windows, this will init the winsock stuff */
85  curl_global_init(CURL_GLOBAL_ALL);
86
87  /* get a curl handle */
88  curl = curl_easy_init();
89  if(curl) {
90    /* we want to use our own read function */
91    curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
92
93    /* enable uploading */
94    curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
95
96    /* HTTP PUT please */
97    curl_easy_setopt(curl, CURLOPT_PUT, 1L);
98
99    /* specify target URL, and note that this URL should include a file
100       name, not only a directory */
101    curl_easy_setopt(curl, CURLOPT_URL, url);
102
103    /* now specify which file to upload */
104    curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
105
106    /* provide the size of the upload, we specicially typecast the value
107       to curl_off_t since we must be sure to use the correct data size */
108    curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
109                     (curl_off_t)file_info.st_size);
110
111    /* Now run off and do what you've been told! */
112    res = curl_easy_perform(curl);
113    /* Check for errors */
114    if(res != CURLE_OK)
115      fprintf(stderr, "curl_easy_perform() failed: %s\n",
116              curl_easy_strerror(res));
117
118    /* always cleanup */
119    curl_easy_cleanup(curl);
120  }
121  fclose(hd_src); /* close the local file */
122
123  curl_global_cleanup();
124  return 0;
125}
126