Michal Marek's cleanup of how curl_easy_setopt() is used in examples and
[platform/upstream/curl.git] / docs / examples / 10-at-a-time.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * $Id$
9  *
10  * Example application source code using the multi interface to download many
11  * files, but with a capped maximum amount of simultaneous transfers.
12  *
13  * Written by Michael Wallner
14  */
15
16 #include <errno.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <curl/multi.h>
21 #ifdef WIN32
22 #include <windows.h>
23 #endif
24
25 static const char *urls[] = {
26   "http://www.microsoft.com",
27   "http://www.opensource.org",
28   "http://www.google.com",
29   "http://www.yahoo.com",
30   "http://www.ibm.com",
31   "http://www.mysql.com",
32   "http://www.oracle.com",
33   "http://www.ripe.net",
34   "http://www.iana.org",
35   "http://www.amazon.com",
36   "http://www.netcraft.com",
37   "http://www.heise.de",
38   "http://www.chip.de",
39   "http://www.ca.com",
40   "http://www.cnet.com",
41   "http://www.news.com",
42   "http://www.cnn.com",
43   "http://www.wikipedia.org",
44   "http://www.dell.com",
45   "http://www.hp.com",
46   "http://www.cert.org",
47   "http://www.mit.edu",
48   "http://www.nist.gov",
49   "http://www.ebay.com",
50   "http://www.playstation.com",
51   "http://www.uefa.com",
52   "http://www.ieee.org",
53   "http://www.apple.com",
54   "http://www.sony.com",
55   "http://www.symantec.com",
56   "http://www.zdnet.com",
57   "http://www.fujitsu.com",
58   "http://www.supermicro.com",
59   "http://www.hotmail.com",
60   "http://www.ecma.com",
61   "http://www.bbc.co.uk",
62   "http://news.google.com",
63   "http://www.foxnews.com",
64   "http://www.msn.com",
65   "http://www.wired.com",
66   "http://www.sky.com",
67   "http://www.usatoday.com",
68   "http://www.cbs.com",
69   "http://www.nbc.com",
70   "http://slashdot.org",
71   "http://www.bloglines.com",
72   "http://www.techweb.com",
73   "http://www.newslink.org",
74   "http://www.un.org",
75 };
76
77 #define MAX 10 /* number of simultaneous transfers */
78 #define CNT sizeof(urls)/sizeof(char*) /* total number of transfers to do */
79
80 static size_t cb(char *d, size_t n, size_t l, void *p)
81 {
82   /* take care of the data here, ignored in this example */
83   (void)d;
84   (void)p;
85   return n*l;
86 }
87
88 static void init(CURLM *cm, int i)
89 {
90   CURL *eh = curl_easy_init();
91
92   curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, cb);
93   curl_easy_setopt(eh, CURLOPT_HEADER, 0);
94   curl_easy_setopt(eh, CURLOPT_URL, urls[i]);
95   curl_easy_setopt(eh, CURLOPT_PRIVATE, urls[i]);
96   curl_easy_setopt(eh, CURLOPT_VERBOSE, 0);
97
98   curl_multi_add_handle(cm, eh);
99 }
100
101 int main(void)
102 {
103   CURLM *cm;
104   CURLMsg *msg;
105   long L;
106   unsigned int C=0;
107   int M, Q, U = -1;
108   fd_set R, W, E;
109   struct timeval T;
110
111   curl_global_init(CURL_GLOBAL_ALL);
112
113   cm = curl_multi_init();
114
115   /* we can optionally limit the total amount of connections this multi handle
116      uses */
117   curl_multi_setopt(cm, CURLMOPT_MAXCONNECTS, MAX);
118
119   for (C = 0; C < MAX; ++C) {
120     init(cm, C);
121   }
122
123   while (U) {
124     while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(cm, &U));
125
126     if (U) {
127       FD_ZERO(&R);
128       FD_ZERO(&W);
129       FD_ZERO(&E);
130
131       if (curl_multi_fdset(cm, &R, &W, &E, &M)) {
132         fprintf(stderr, "E: curl_multi_fdset\n");
133         return EXIT_FAILURE;
134       }
135
136       if (curl_multi_timeout(cm, &L)) {
137         fprintf(stderr, "E: curl_multi_timeout\n");
138         return EXIT_FAILURE;
139       }
140       if (L == -1)
141         L = 100;
142
143       if (M == -1) {
144 #ifdef WIN32
145         Sleep(L);
146 #else
147         sleep(L / 1000);
148 #endif
149       } else {
150         T.tv_sec = L/1000;
151         T.tv_usec = (L%1000)*1000;
152
153         if (0 > select(M+1, &R, &W, &E, &T)) {
154           fprintf(stderr, "E: select(%i,,,,%li): %i: %s\n",
155               M+1, L, errno, strerror(errno));
156           return EXIT_FAILURE;
157         }
158       }
159     }
160
161     while ((msg = curl_multi_info_read(cm, &Q))) {
162       if (msg->msg == CURLMSG_DONE) {
163         char *url;
164         CURL *e = msg->easy_handle;
165         curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &url);
166         fprintf(stderr, "R: %d - %s <%s>\n",
167                 msg->data.result, curl_easy_strerror(msg->data.result), url);
168         curl_multi_remove_handle(cm, e);
169         curl_easy_cleanup(e);
170       }
171       else {
172         fprintf(stderr, "E: CURLMsg (%d)\n", msg->msg);
173       }
174       if (C < CNT) {
175         init(cm, C++);
176         U++; /* just to prevent it from remaining at 0 if there are more
177                 URLs to get */
178       }
179     }
180   }
181
182   curl_multi_cleanup(cm);
183   curl_global_cleanup();
184
185   return EXIT_SUCCESS;
186 }