Home | History | Annotate | Download | only in examples
      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel (at) haxx.se>, et al.
      9  *
     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.
     13  *
     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.
     17  *
     18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
     19  * KIND, either express or implied.
     20  *
     21  ***************************************************************************/
     22 #include <stdio.h>
     23 #include <curl/curl.h>
     24 
     25 /* This is a simple example showing how to fetch mail using libcurl's IMAP
     26  * capabilities. It builds on the imap-fetch.c example to demonstrate how to
     27  * use libcurl's multi interface.
     28  *
     29  * Note that this example requires libcurl 7.30.0 or above.
     30  */
     31 
     32 #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
     33 
     34 static struct timeval tvnow(void)
     35 {
     36   struct timeval now;
     37 
     38   /* time() returns the value of time in seconds since the epoch */
     39   now.tv_sec = (long)time(NULL);
     40   now.tv_usec = 0;
     41 
     42   return now;
     43 }
     44 
     45 static long tvdiff(struct timeval newer, struct timeval older)
     46 {
     47   return (newer.tv_sec - older.tv_sec) * 1000 +
     48     (newer.tv_usec - older.tv_usec) / 1000;
     49 }
     50 
     51 int main(void)
     52 {
     53   CURL *curl;
     54   CURLM *mcurl;
     55   int still_running = 1;
     56   struct timeval mp_start;
     57 
     58   curl_global_init(CURL_GLOBAL_DEFAULT);
     59 
     60   curl = curl_easy_init();
     61   if(!curl)
     62     return 1;
     63 
     64   mcurl = curl_multi_init();
     65   if(!mcurl)
     66     return 2;
     67 
     68   /* Set username and password */
     69   curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
     70   curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
     71 
     72   /* This will fetch message 1 from the user's inbox */
     73   curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com/INBOX/;UID=1");
     74 
     75   /* Tell the multi stack about our easy handle */
     76   curl_multi_add_handle(mcurl, curl);
     77 
     78   /* Record the start time which we can use later */
     79   mp_start = tvnow();
     80 
     81   /* We start some action by calling perform right away */
     82   curl_multi_perform(mcurl, &still_running);
     83 
     84   while(still_running) {
     85     struct timeval timeout;
     86     fd_set fdread;
     87     fd_set fdwrite;
     88     fd_set fdexcep;
     89     int maxfd = -1;
     90     int rc;
     91     CURLMcode mc; /* curl_multi_fdset() return code */
     92 
     93     long curl_timeo = -1;
     94 
     95     /* Initialise the file descriptors */
     96     FD_ZERO(&fdread);
     97     FD_ZERO(&fdwrite);
     98     FD_ZERO(&fdexcep);
     99 
    100     /* Set a suitable timeout to play around with */
    101     timeout.tv_sec = 1;
    102     timeout.tv_usec = 0;
    103 
    104     curl_multi_timeout(mcurl, &curl_timeo);
    105     if(curl_timeo >= 0) {
    106       timeout.tv_sec = curl_timeo / 1000;
    107       if(timeout.tv_sec > 1)
    108         timeout.tv_sec = 1;
    109       else
    110         timeout.tv_usec = (curl_timeo % 1000) * 1000;
    111     }
    112 
    113     /* get file descriptors from the transfers */
    114     mc = curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
    115 
    116     if(mc != CURLM_OK)
    117     {
    118       fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
    119       break;
    120     }
    121 
    122     /* On success the value of maxfd is guaranteed to be >= -1. We call
    123        select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
    124        no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
    125        to sleep 100ms, which is the minimum suggested value in the
    126        curl_multi_fdset() doc. */
    127 
    128     if(maxfd == -1) {
    129 #ifdef _WIN32
    130       Sleep(100);
    131       rc = 0;
    132 #else
    133       /* Portable sleep for platforms other than Windows. */
    134       struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
    135       rc = select(0, NULL, NULL, NULL, &wait);
    136 #endif
    137     }
    138     else {
    139       /* Note that on some platforms 'timeout' may be modified by select().
    140          If you need access to the original value save a copy beforehand. */
    141       rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
    142     }
    143 
    144     if(tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
    145       fprintf(stderr,
    146               "ABORTING: Since it seems that we would have run forever.\n");
    147       break;
    148     }
    149 
    150     switch(rc) {
    151     case -1:  /* select error */
    152       break;
    153     case 0:   /* timeout */
    154     default:  /* action */
    155       curl_multi_perform(mcurl, &still_running);
    156       break;
    157     }
    158   }
    159 
    160   /* Always cleanup */
    161   curl_multi_remove_handle(mcurl, curl);
    162   curl_multi_cleanup(mcurl);
    163   curl_easy_cleanup(curl);
    164   curl_global_cleanup();
    165 
    166   return 0;
    167 }
    168