pop3-multi.c: Corrected copy/paste typo
[platform/upstream/curl.git] / docs / examples / pop3-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 retrieve mail using libcurl's POP3
26  * capabilities. It builds on the pop3-retr.c example to demonstrate how to use
27  * libcurl's multi interface.
28  *
29  * Note that this example requires libcurl 7.20.0 or above.
30  */
31
32 static struct timeval tvnow(void)
33 {
34   struct timeval now;
35
36   /* time() returns the value of time in seconds since the epoch */
37   now.tv_sec = (long)time(NULL);
38   now.tv_usec = 0;
39
40   return now;
41 }
42
43 static long tvdiff(struct timeval newer, struct timeval older)
44 {
45   return (newer.tv_sec - older.tv_sec) * 1000 +
46     (newer.tv_usec - older.tv_usec) / 1000;
47 }
48
49 int main(void)
50 {
51   CURL *curl;
52   CURLM *mcurl;
53   int still_running = 1;
54   struct timeval mp_start;
55
56   curl_global_init(CURL_GLOBAL_DEFAULT);
57
58   curl = curl_easy_init();
59   if(!curl)
60     return 1;
61
62   mcurl = curl_multi_init();
63   if(!mcurl)
64     return 2;
65
66   /* Set username and password */
67   curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
68   curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
69
70   /* This will retreive message 1 from the user's mailbox */
71   curl_easy_setopt(curl, CURLOPT_URL, "pop3://pop.example.com/1");
72
73   /* Tell the multi stack about our easy handle */
74   curl_multi_add_handle(mcurl, curl);
75
76   /* Record the start time which we can use later */
77   mp_start = tvnow();
78
79   /* We start some action by calling perform right away */
80   curl_multi_perform(mcurl, &still_running);
81
82   while(still_running) {
83     struct timeval timeout;
84     fd_set fdread;
85     fd_set fdwrite;
86     fd_set fdexcep;
87     int maxfd = -1;
88     int rc;
89
90     long curl_timeo = -1;
91
92     /* Initialise the file descriptors */
93     FD_ZERO(&fdread);
94     FD_ZERO(&fdwrite);
95     FD_ZERO(&fdexcep);
96
97     /* Set a suitable timeout to play around with */
98     timeout.tv_sec = 1;
99     timeout.tv_usec = 0;
100
101     curl_multi_timeout(mcurl, &curl_timeo);
102     if(curl_timeo >= 0) {
103       timeout.tv_sec = curl_timeo / 1000;
104       if(timeout.tv_sec > 1)
105         timeout.tv_sec = 1;
106       else
107         timeout.tv_usec = (curl_timeo % 1000) * 1000;
108     }
109
110     /* Get file descriptors from the transfers */
111     curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
112
113     /* In a real-world program you OF COURSE check the return code of the
114        function calls. On success, the value of maxfd is guaranteed to be
115        greater or equal than -1. We call select(maxfd + 1, ...), specially in
116        case of (maxfd == -1), we call select(0, ...), which is basically equal
117        to sleep. */
118     rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
119
120     if(tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
121       fprintf(stderr,
122               "ABORTING: Since it seems that we would have run forever.\n");
123       break;
124     }
125
126     switch(rc) {
127     case -1:  /* select error */
128       break;
129     case 0:   /* timeout */
130     default:  /* action */
131       curl_multi_perform(mcurl, &still_running);
132       break;
133     }
134   }
135
136   /* Always cleanup */
137   curl_multi_remove_handle(mcurl, curl);
138   curl_multi_cleanup(mcurl);
139   curl_easy_cleanup(curl);
140   curl_global_cleanup();
141
142   return 0;
143 }