1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2011, 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/* This is a very simple example using the multi interface. */
23
24#include <stdio.h>
25#include <string.h>
26
27/* somewhat unix-specific */
28#include <sys/time.h>
29#include <unistd.h>
30
31/* curl stuff */
32#include <curl/curl.h>
33
34/*
35 * Simply download a HTTP file.
36 */
37int main(void)
38{
39  CURL *http_handle;
40  CURLM *multi_handle;
41
42  int still_running; /* keep number of running handles */
43
44  curl_global_init(CURL_GLOBAL_DEFAULT);
45
46  http_handle = curl_easy_init();
47
48  /* set the options (I left out a few, you'll get the point anyway) */
49  curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
50
51  /* init a multi stack */
52  multi_handle = curl_multi_init();
53
54  /* add the individual transfers */
55  curl_multi_add_handle(multi_handle, http_handle);
56
57  /* we start some action by calling perform right away */
58  curl_multi_perform(multi_handle, &still_running);
59
60  do {
61    struct timeval timeout;
62    int rc; /* select() return code */
63
64    fd_set fdread;
65    fd_set fdwrite;
66    fd_set fdexcep;
67    int maxfd = -1;
68
69    long curl_timeo = -1;
70
71    FD_ZERO(&fdread);
72    FD_ZERO(&fdwrite);
73    FD_ZERO(&fdexcep);
74
75    /* set a suitable timeout to play around with */
76    timeout.tv_sec = 1;
77    timeout.tv_usec = 0;
78
79    curl_multi_timeout(multi_handle, &curl_timeo);
80    if(curl_timeo >= 0) {
81      timeout.tv_sec = curl_timeo / 1000;
82      if(timeout.tv_sec > 1)
83        timeout.tv_sec = 1;
84      else
85        timeout.tv_usec = (curl_timeo % 1000) * 1000;
86    }
87
88    /* get file descriptors from the transfers */
89    curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
90
91    /* In a real-world program you OF COURSE check the return code of the
92       function calls.  On success, the value of maxfd is guaranteed to be
93       greater or equal than -1.  We call select(maxfd + 1, ...), specially in
94       case of (maxfd == -1), we call select(0, ...), which is basically equal
95       to sleep. */
96
97    rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
98
99    switch(rc) {
100    case -1:
101      /* select error */
102      still_running = 0;
103      printf("select() returns error, this is badness\n");
104      break;
105    case 0:
106    default:
107      /* timeout or readable/writable sockets */
108      curl_multi_perform(multi_handle, &still_running);
109      break;
110    }
111  } while(still_running);
112
113  curl_multi_remove_handle(multi_handle, http_handle);
114
115  curl_easy_cleanup(http_handle);
116
117  curl_multi_cleanup(multi_handle);
118
119  curl_global_cleanup();
120
121  return 0;
122}
123