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