1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
30 # if !defined(_AIX) && !defined(__sgi) && !defined(__osf__)
35 #include <sys/types.h>
40 typedef __int64 intptr_t;
46 #include <curl/curl.h>
48 #if LIBCURL_VERSION_NUM < 0x070c03
49 #error "upgrade your libcurl to no less than 7.12.3"
56 #if defined(_AIX) || defined(__sgi) || defined(__osf__)
63 * This example shows a HTTP PUT operation with authentiction using "any"
64 * type. It PUTs a file given as a command line argument to the URL also given
65 * on the command line.
67 * Since libcurl 7.12.3, using "any" auth and POST/PUT requires a set ioctl
70 * This example also uses its own read callback.
73 /* ioctl callback function */
74 static curlioerr my_ioctl(CURL *handle, curliocmd cmd, void *userp)
76 intptr_t fd = (intptr_t)userp;
78 (void)handle; /* not used in here */
81 case CURLIOCMD_RESTARTREAD:
82 /* mr libcurl kindly asks as to rewind the read data stream to start */
83 if(-1 == lseek(fd, 0, SEEK_SET))
85 return CURLIOE_FAILRESTART;
89 default: /* ignore unknown commands */
90 return CURLIOE_UNKNOWNCMD;
92 return CURLIOE_OK; /* success! */
95 /* read callback function, fread() look alike */
96 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
101 intptr_t fd = (intptr_t)stream;
103 retcode = read(fd, ptr, size * nmemb);
105 nread = (curl_off_t)retcode;
107 fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
108 " bytes from file\n", nread);
113 int main(int argc, char **argv)
118 struct stat file_info;
129 /* get the file size of the local file */
130 hd = open(file, O_RDONLY) ;
131 fstat(hd, &file_info);
133 /* In windows, this will init the winsock stuff */
134 curl_global_init(CURL_GLOBAL_ALL);
136 /* get a curl handle */
137 curl = curl_easy_init();
139 /* we want to use our own read function */
140 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
142 /* which file to upload */
143 curl_easy_setopt(curl, CURLOPT_READDATA, (void*)hd);
145 /* set the ioctl function */
146 curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, my_ioctl);
148 /* pass the file descriptor to the ioctl callback as well */
149 curl_easy_setopt(curl, CURLOPT_IOCTLDATA, (void*)hd);
151 /* enable "uploading" (which means PUT when doing HTTP) */
152 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L) ;
154 /* specify target URL, and note that this URL should also include a file
155 name, not only a directory (as you can do with GTP uploads) */
156 curl_easy_setopt(curl,CURLOPT_URL, url);
158 /* and give the size of the upload, this supports large file sizes
159 on systems that have general support for it */
160 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
161 (curl_off_t)file_info.st_size);
163 /* tell libcurl we can use "any" auth, which lets the lib pick one, but it
164 also costs one extra round-trip and possibly sending of all the PUT
166 curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
168 /* set user name and password for the authentication */
169 curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
171 /* Now run off and do what you've been told! */
172 res = curl_easy_perform(curl);
173 /* Check for errors */
175 fprintf(stderr, "curl_easy_perform() failed: %s\n",
176 curl_easy_strerror(res));
179 curl_easy_cleanup(curl);
181 close(hd); /* close the local file */
183 curl_global_cleanup();