1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include <curl/curl.h>
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.
29 * Note that this example requires libcurl 7.30.0 or above.
32 #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
34 static struct timeval tvnow(void)
38 /* time() returns the value of time in seconds since the epoch */
39 now.tv_sec = (long)time(NULL);
45 static long tvdiff(struct timeval newer, struct timeval older)
47 return (newer.tv_sec - older.tv_sec) * 1000 +
48 (newer.tv_usec - older.tv_usec) / 1000;
55 int still_running = 1;
56 struct timeval mp_start;
58 curl_global_init(CURL_GLOBAL_DEFAULT);
60 curl = curl_easy_init();
64 mcurl = curl_multi_init();
68 /* Set username and password */
69 curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
70 curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
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");
75 /* Tell the multi stack about our easy handle */
76 curl_multi_add_handle(mcurl, curl);
78 /* Record the start time which we can use later */
81 /* We start some action by calling perform right away */
82 curl_multi_perform(mcurl, &still_running);
84 while(still_running) {
85 struct timeval timeout;
91 CURLMcode mc; /* curl_multi_fdset() return code */
95 /* Initialise the file descriptors */
100 /* Set a suitable timeout to play around with */
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)
110 timeout.tv_usec = (curl_timeo % 1000) * 1000;
113 /* get file descriptors from the transfers */
114 mc = curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
118 fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
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. */
133 /* Portable sleep for platforms other than Windows. */
134 struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
135 rc = select(0, NULL, NULL, NULL, &wait);
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);
144 if(tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
146 "ABORTING: Since it seems that we would have run forever.\n");
151 case -1: /* select error */
153 case 0: /* timeout */
154 default: /* action */
155 curl_multi_perform(mcurl, &still_running);
161 curl_multi_remove_handle(mcurl, curl);
162 curl_multi_cleanup(mcurl);
163 curl_easy_cleanup(curl);
164 curl_global_cleanup();