Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <stdio.h>
00028 #include <fcntl.h>
00029 #ifdef WIN32
00030 # include <io.h>
00031 #else
00032 # ifdef __VMS
00033 typedef int intptr_t;
00034 # endif
00035 # if !defined(_AIX) && !defined(__sgi) && !defined(__osf__)
00036 # include <stdint.h>
00037 # endif
00038 # include <unistd.h>
00039 #endif
00040 #include <sys/types.h>
00041 #include <sys/stat.h>
00042
00043 #ifdef _MSC_VER
00044 # ifdef _WIN64
00045 typedef __int64 intptr_t;
00046 # else
00047 typedef int intptr_t;
00048 # endif
00049 #endif
00050
00051 #include <curl/curl.h>
00052
00053 #if LIBCURL_VERSION_NUM < 0x070c03
00054 #error "upgrade your libcurl to no less than 7.12.3"
00055 #endif
00056
00057 #ifndef TRUE
00058 #define TRUE 1
00059 #endif
00060
00061 #if defined(_AIX) || defined(__sgi) || defined(__osf__)
00062 #ifndef intptr_t
00063 #define intptr_t long
00064 #endif
00065 #endif
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 static curlioerr my_ioctl(CURL *handle, curliocmd cmd, void *userp)
00080 {
00081 int *fdp = (int *)userp;
00082 int fd = *fdp;
00083
00084 (void)handle;
00085
00086 switch(cmd) {
00087 case CURLIOCMD_RESTARTREAD:
00088
00089 if(-1 == lseek(fd, 0, SEEK_SET))
00090
00091 return CURLIOE_FAILRESTART;
00092
00093 break;
00094
00095 default:
00096 return CURLIOE_UNKNOWNCMD;
00097 }
00098 return CURLIOE_OK;
00099 }
00100
00101
00102 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
00103 {
00104 ssize_t retcode;
00105 curl_off_t nread;
00106
00107 int *fdp = (int *)stream;
00108 int fd = *fdp;
00109
00110 retcode = read(fd, ptr, size * nmemb);
00111
00112 nread = (curl_off_t)retcode;
00113
00114 fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
00115 " bytes from file\n", nread);
00116
00117 return retcode;
00118 }
00119
00120 int main(int argc, char **argv)
00121 {
00122 CURL *curl;
00123 CURLcode res;
00124 int hd;
00125 struct stat file_info;
00126
00127 char *file;
00128 char *url;
00129
00130 if(argc < 3)
00131 return 1;
00132
00133 file= argv[1];
00134 url = argv[2];
00135
00136
00137 hd = open(file, O_RDONLY);
00138 fstat(hd, &file_info);
00139
00140
00141 curl_global_init(CURL_GLOBAL_ALL);
00142
00143
00144 curl = curl_easy_init();
00145 if(curl) {
00146
00147 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
00148
00149
00150 curl_easy_setopt(curl, CURLOPT_READDATA, (void *)&hd);
00151
00152
00153 curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, my_ioctl);
00154
00155
00156 curl_easy_setopt(curl, CURLOPT_IOCTLDATA, (void *)&hd);
00157
00158
00159 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
00160
00161
00162
00163 curl_easy_setopt(curl, CURLOPT_URL, url);
00164
00165
00166
00167 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
00168 (curl_off_t)file_info.st_size);
00169
00170
00171
00172
00173 curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
00174
00175
00176 curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
00177
00178
00179 res = curl_easy_perform(curl);
00180
00181 if(res != CURLE_OK)
00182 fprintf(stderr, "curl_easy_perform() failed: %s\n",
00183 curl_easy_strerror(res));
00184
00185
00186 curl_easy_cleanup(curl);
00187 }
00188 close(hd);
00189
00190 curl_global_cleanup();
00191 return 0;
00192 }