tizen 2.3.1 release
[external/curl.git] / docs / examples / fopen.c
index e4505bf..3d2a817 100644 (file)
@@ -68,8 +68,8 @@ struct fcurl_data
   } handle;                   /* handle */
 
   char *buffer;               /* buffer to store cached data*/
-  int buffer_len;             /* currently allocated buffers length */
-  int buffer_pos;             /* end of data in buffer*/
+  size_t buffer_len;          /* currently allocated buffers length */
+  size_t buffer_pos;          /* end of data in buffer*/
   int still_running;          /* Is background url fetch still in progress */
 };
 
@@ -80,7 +80,7 @@ URL_FILE *url_fopen(const char *url,const char *operation);
 int url_fclose(URL_FILE *file);
 int url_feof(URL_FILE *file);
 size_t url_fread(void *ptr, size_t size, size_t nmemb, URL_FILE *file);
-char * url_fgets(char *ptr, int size, URL_FILE *file);
+char * url_fgets(char *ptr, size_t size, URL_FILE *file);
 void url_rewind(URL_FILE *file);
 
 /* we use a global one for convenience */
@@ -93,7 +93,7 @@ static size_t write_callback(char *buffer,
                              void *userp)
 {
   char *newbuff;
-  int rembuff;
+  size_t rembuff;
 
   URL_FILE *url = (URL_FILE *)userp;
   size *= nitems;
@@ -121,13 +121,14 @@ static size_t write_callback(char *buffer,
 }
 
 /* use to attempt to fill the read buffer up to requested number of bytes */
-static int fill_buffer(URL_FILE *file,int want,int waittime)
+static int fill_buffer(URL_FILE *file, size_t want)
 {
   fd_set fdread;
   fd_set fdwrite;
   fd_set fdexcep;
   struct timeval timeout;
   int rc;
+  CURLMcode mc; /* curl_multi_fdset() return code */
 
   /* only attempt to fill buffer if transactions still running and buffer
    * doesnt exceed required size already
@@ -158,15 +159,35 @@ static int fill_buffer(URL_FILE *file,int want,int waittime)
     }
 
     /* get file descriptors from the transfers */
-    curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
+    mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
 
-    /* In a real-world program you OF COURSE check the return code of the
-       function calls.  On success, the value of maxfd is guaranteed to be
-       greater or equal than -1.  We call select(maxfd + 1, ...), specially
-       in case of (maxfd == -1), we call select(0, ...), which is basically
-       equal to sleep. */
+    if(mc != CURLM_OK)
+    {
+      fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
+      break;
+    }
 
-    rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
+    /* On success the value of maxfd is guaranteed to be >= -1. We call
+       select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
+       no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
+       to sleep 100ms, which is the minimum suggested value in the
+       curl_multi_fdset() doc. */
+
+    if(maxfd == -1) {
+#ifdef _WIN32
+      Sleep(100);
+      rc = 0;
+#else
+      /* Portable sleep for platforms other than Windows. */
+      struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
+      rc = select(0, NULL, NULL, NULL, &wait);
+#endif
+    }
+    else {
+      /* Note that on some platforms 'timeout' may be modified by select().
+         If you need access to the original value save a copy beforehand. */
+      rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
+    }
 
     switch(rc) {
     case -1:
@@ -323,7 +344,7 @@ size_t url_fread(void *ptr, size_t size, size_t nmemb, URL_FILE *file)
   case CFTYPE_CURL:
     want = nmemb * size;
 
-    fill_buffer(file,want,1);
+    fill_buffer(file,want);
 
     /* check if theres data in the buffer - if not fill_buffer()
      * either errored or EOF */
@@ -351,10 +372,10 @@ size_t url_fread(void *ptr, size_t size, size_t nmemb, URL_FILE *file)
   return want;
 }
 
-char *url_fgets(char *ptr, int size, URL_FILE *file)
+char *url_fgets(char *ptr, size_t size, URL_FILE *file)
 {
-  int want = size - 1;/* always need to leave room for zero termination */
-  int loop;
+  size_t want = size - 1;/* always need to leave room for zero termination */
+  size_t loop;
 
   switch(file->type) {
   case CFTYPE_FILE:
@@ -362,7 +383,7 @@ char *url_fgets(char *ptr, int size, URL_FILE *file)
     break;
 
   case CFTYPE_CURL:
-    fill_buffer(file,want,1);
+    fill_buffer(file,want);
 
     /* check if theres data in the buffer - if not fill either errored or
      * EOF */