123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- #include <stdio.h>
- #include <fcntl.h>
- #ifdef WIN32
- # include <io.h>
- #else
- # ifdef __VMS
- typedef int intptr_t;
- # endif
- # if !defined(_AIX) && !defined(__sgi) && !defined(__osf__)
- # include <stdint.h>
- # endif
- # include <unistd.h>
- #endif
- #include <sys/types.h>
- #include <sys/stat.h>
- #ifdef _MSC_VER
- # ifdef _WIN64
- typedef __int64 intptr_t;
- # else
- typedef int intptr_t;
- # endif
- #endif
- #include <curl/curl.h>
- #if LIBCURL_VERSION_NUM < 0x070c03
- #error "upgrade your libcurl to no less than 7.12.3"
- #endif
- #ifndef TRUE
- #define TRUE 1
- #endif
- #if defined(_AIX) || defined(__sgi) || defined(__osf__)
- #ifndef intptr_t
- #define intptr_t long
- #endif
- #endif
- static curlioerr my_ioctl(CURL *handle, curliocmd cmd, void *userp)
- {
- intptr_t fd = (intptr_t)userp;
- (void)handle;
- switch(cmd) {
- case CURLIOCMD_RESTARTREAD:
-
- if(-1 == lseek(fd, 0, SEEK_SET))
-
- return CURLIOE_FAILRESTART;
- break;
- default:
- return CURLIOE_UNKNOWNCMD;
- }
- return CURLIOE_OK;
- }
- static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
- {
- size_t retcode;
- curl_off_t nread;
- intptr_t fd = (intptr_t)stream;
- retcode = read(fd, ptr, size * nmemb);
- nread = (curl_off_t)retcode;
- fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
- " bytes from file\n", nread);
- return retcode;
- }
- int main(int argc, char **argv)
- {
- CURL *curl;
- CURLcode res;
- intptr_t hd ;
- struct stat file_info;
- char *file;
- char *url;
- if(argc < 3)
- return 1;
- file= argv[1];
- url = argv[2];
-
- hd = open(file, O_RDONLY) ;
- fstat(hd, &file_info);
-
- curl_global_init(CURL_GLOBAL_ALL);
-
- curl = curl_easy_init();
- if(curl) {
-
- curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
-
- curl_easy_setopt(curl, CURLOPT_READDATA, (void*)hd);
-
- curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, my_ioctl);
-
- curl_easy_setopt(curl, CURLOPT_IOCTLDATA, (void*)hd);
-
- curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L) ;
-
- curl_easy_setopt(curl,CURLOPT_URL, url);
-
- curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
- (curl_off_t)file_info.st_size);
-
- curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
-
- curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
-
- res = curl_easy_perform(curl);
-
- if(res != CURLE_OK)
- fprintf(stderr, "curl_easy_perform() failed: %s\n",
- curl_easy_strerror(res));
-
- curl_easy_cleanup(curl);
- }
- close(hd);
- curl_global_cleanup();
- return 0;
- }
|