1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, 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 https://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 ***************************************************************************/
24 * POP3 example using the multi interface
30 #include <curl/curl.h>
32 /* This is a simple example showing how to retrieve mail using libcurl's POP3
33 * capabilities. It builds on the pop3-retr.c example to demonstrate how to use
34 * libcurl's multi interface.
36 * Note that this example requires libcurl 7.20.0 or above.
39 #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
41 static struct timeval tvnow(void)
45 /* time() returns the value of time in seconds since the epoch */
46 now.tv_sec = (long)time(NULL);
52 static long tvdiff(struct timeval newer, struct timeval older)
54 return (newer.tv_sec - older.tv_sec) * 1000 +
55 (newer.tv_usec - older.tv_usec) / 1000;
62 int still_running = 1;
63 struct timeval mp_start;
65 curl_global_init(CURL_GLOBAL_DEFAULT);
67 curl = curl_easy_init();
71 mcurl = curl_multi_init();
75 /* Set username and password */
76 curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
77 curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
79 /* This will retrieve message 1 from the user's mailbox */
80 curl_easy_setopt(curl, CURLOPT_URL, "pop3://pop.example.com/1");
82 /* Tell the multi stack about our easy handle */
83 curl_multi_add_handle(mcurl, curl);
85 /* Record the start time which we can use later */
88 /* We start some action by calling perform right away */
89 curl_multi_perform(mcurl, &still_running);
91 while(still_running) {
92 struct timeval timeout;
98 CURLMcode mc; /* curl_multi_fdset() return code */
100 long curl_timeo = -1;
102 /* Initialise the file descriptors */
107 /* Set a suitable timeout to play around with */
111 curl_multi_timeout(mcurl, &curl_timeo);
112 if(curl_timeo >= 0) {
113 timeout.tv_sec = curl_timeo / 1000;
114 if(timeout.tv_sec > 1)
117 timeout.tv_usec = (curl_timeo % 1000) * 1000;
120 /* get file descriptors from the transfers */
121 mc = curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
124 fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
128 /* On success the value of maxfd is guaranteed to be >= -1. We call
129 select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
130 no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
131 to sleep 100ms, which is the minimum suggested value in the
132 curl_multi_fdset() doc. */
139 /* Portable sleep for platforms other than Windows. */
140 struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
141 rc = select(0, NULL, NULL, NULL, &wait);
145 /* Note that on some platforms 'timeout' may be modified by select().
146 If you need access to the original value save a copy beforehand. */
147 rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
150 if(tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
152 "ABORTING: Since it seems that we would have run forever.\n");
157 case -1: /* select error */
159 case 0: /* timeout */
160 default: /* action */
161 curl_multi_perform(mcurl, &still_running);
167 curl_multi_remove_handle(mcurl, curl);
168 curl_multi_cleanup(mcurl);
169 curl_easy_cleanup(curl);
170 curl_global_cleanup();