51#ifdef FOPEN_STANDALONE
69static CURLM *multi_handle;
72static size_t write_callback(
char *buffer,
73 size_t size,
size_t nitems,
void *userp ) {
82 if ( size > rembuff ) {
85 if ( newbuff == NULL ) {
86 fprintf( stderr,
"callback buffer grow failed\n" );
102static int fill_buffer(
URL_FILE *file,
size_t want ) {
106 struct timeval timeout;
119 long curl_timeo = -1;
129 curl_multi_timeout( multi_handle, &curl_timeo );
130 if ( curl_timeo >= 0 ) {
131 timeout.tv_sec = curl_timeo / 1000;
132 if ( timeout.tv_sec > 1 )
135 timeout.tv_usec = ( curl_timeo % 1000 ) * 1000;
139 mc = curl_multi_fdset( multi_handle, &fdread, &fdwrite, &fdexcep,
142 if ( mc != CURLM_OK ) {
143 fprintf( stderr,
"curl_multi_fdset() failed, code %d.\n", mc );
159 struct timeval wait = { 0, 100 * 1000 };
160 rc = select( 0, NULL, NULL, NULL, &wait );
165 rc = select( maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout );
184static int use_buffer(
URL_FILE *file,
size_t want ) {
209 file = calloc( 1,
sizeof(
URL_FILE ) );
213 file->
handle.file = fopen( url, operation );
214 if ( file->
handle.file ) {
216 }
else if (
index_of(
':', url ) > -1 ) {
218 file->
handle.curl = curl_easy_init( );
220 curl_easy_setopt( file->
handle.curl, CURLOPT_URL, url );
221 curl_easy_setopt( file->
handle.curl, CURLOPT_WRITEDATA, file );
222 curl_easy_setopt( file->
handle.curl, CURLOPT_VERBOSE, 0L );
223 curl_easy_setopt( file->
handle.curl, CURLOPT_WRITEFUNCTION,
230 multi_handle = curl_multi_init( );
232 curl_multi_add_handle( multi_handle, file->
handle.curl );
241 curl_multi_remove_handle( multi_handle, file->
handle.curl );
244 curl_easy_cleanup( file->
handle.curl );
261 switch ( file->
type ) {
263 ret = fclose( file->
handle.file );
268 curl_multi_remove_handle( multi_handle, file->
handle.curl );
271 curl_easy_cleanup( file->
handle.curl );
289 switch ( file->
type ) {
291 ret = feof( file->
handle.file );
310 switch ( file->
type ) {
312 want = fread( ptr, size, nmemb, file->
handle.file );
318 fill_buffer( file, want );
330 memcpy( ptr, file->
buffer, want );
332 use_buffer( file, want );
347 size_t want = size - 1;
350 switch ( file->
type ) {
352 ptr = fgets( ptr, (
int ) size, file->
handle.file );
356 fill_buffer( file, want );
369 for ( loop = 0; loop < want; loop++ ) {
370 if ( file->
buffer[loop] ==
'\n' ) {
377 memcpy( ptr, file->
buffer, want );
380 use_buffer( file, want );
394 switch ( file->
type ) {
396 rewind( file->
handle.file );
401 curl_multi_remove_handle( multi_handle, file->
handle.curl );
404 curl_multi_add_handle( multi_handle, file->
handle.curl );
419#ifdef FOPEN_STANDALONE
420#define FGETSFILE "fgets.test"
421#define FREADFILE "fread.test"
422#define REWINDFILE "rewind.test"
427int main(
int argc,
char *argv[] ) {
438 curl_global_init( CURL_GLOBAL_DEFAULT );
440 curl = curl_easy_init( );
444 url =
"http://192.168.7.3/testfile";
449 outf = fopen( FGETSFILE,
"wb+" );
451 perror(
"couldn't open fgets output file\n" );
457 printf(
"couldn't url_fopen() %s\n", url );
463 url_fgets( buffer,
sizeof( buffer ), handle );
464 fwrite( buffer, 1, strlen( buffer ), outf );
473 outf = fopen( FREADFILE,
"wb+" );
475 perror(
"couldn't open fread output file\n" );
481 printf(
"couldn't url_fopen() testfile\n" );
487 nread =
url_fread( buffer, 1,
sizeof( buffer ), handle );
488 fwrite( buffer, 1, nread, outf );
497 outf = fopen( REWINDFILE,
"wb+" );
499 perror(
"couldn't open fread output file\n" );
505 printf(
"couldn't url_fopen() testfile\n" );
510 nread =
url_fread( buffer, 1,
sizeof( buffer ), handle );
511 fwrite( buffer, 1, nread, outf );
515 fwrite( buffer, 1, 1, outf );
517 nread =
url_fread( buffer, 1,
sizeof( buffer ), handle );
518 fwrite( buffer, 1, nread, outf );
void url_rewind(URL_FILE *file)
int url_feof(URL_FILE *file)
URL_FILE * url_fopen(const char *url, const char *operation)
char * url_fgets(char *ptr, size_t size, URL_FILE *file)
size_t url_fread(void *ptr, size_t size, size_t nmemb, URL_FILE *file)
int url_fclose(URL_FILE *file)
union fcurl_data::@0 handle
int main(int argc, char *argv[])
main entry point; parse command line arguments, initialise the environment, and enter the read-eval-p...
CURLSH * io_share
The sharing hub for all connections.
int index_of(char c, const char *s)