Revert "Update to 7.40.1"
[platform/upstream/curl.git] / docs / examples / imap-multi.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@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
92     long curl_timeo = -1;
93
94     /* Initialise the file descriptors */
95     FD_ZERO(&fdread);
96     FD_ZERO(&fdwrite);
97     FD_ZERO(&fdexcep);
98
99     /* Set a suitable timeout to play around with */
100     timeout.tv_sec = 1;
101     timeout.tv_usec = 0;
102
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)
107         timeout.tv_sec = 1;
108       else
109         timeout.tv_usec = (curl_timeo % 1000) * 1000;
110     }
111
112     /* Get file descriptors from the transfers */
113     curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
114
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
119        to sleep. */
120     rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
121
122     if(tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
123       fprintf(stderr,
124               "ABORTING: Since it seems that we would have run forever.\n");
125       break;
126     }
127
128     switch(rc) {
129     case -1:  /* select error */
130       break;
131     case 0:   /* timeout */
132     default:  /* action */
133       curl_multi_perform(mcurl, &still_running);
134       break;
135     }
136   }
137
138   /* Always cleanup */
139   curl_multi_remove_handle(mcurl, curl);
140   curl_multi_cleanup(mcurl);
141   curl_easy_cleanup(curl);
142   curl_global_cleanup();
143
144   return 0;
145 }