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;
94 /* Initialise the file descriptors */
99 /* Set a suitable timeout to play around with */
103 curl_multi_timeout(mcurl, &curl_timeo);
104 if(curl_timeo >= 0) {
105 timeout.tv_sec = curl_timeo / 1000;
106 if(timeout.tv_sec > 1)
109 timeout.tv_usec = (curl_timeo % 1000) * 1000;
112 /* Get file descriptors from the transfers */
113 curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
115 /* In a real-world program you OF COURSE check the return code of the
116 function calls. On success, the value of maxfd is guaranteed to be
117 greater or equal than -1. We call select(maxfd + 1, ...), specially in
118 case of (maxfd == -1), we call select(0, ...), which is basically equal
120 rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
122 if(tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
124 "ABORTING: Since it seems that we would have run forever.\n");
129 case -1: /* select error */
131 case 0: /* timeout */
132 default: /* action */
133 curl_multi_perform(mcurl, &still_running);
139 curl_multi_remove_handle(mcurl, curl);
140 curl_multi_cleanup(mcurl);
141 curl_easy_cleanup(curl);
142 curl_global_cleanup();