• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2021, 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 https://curl.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 /* <DESC>
23  * using the multi interface to do a single download
24  * </DESC>
25  */
26 
27 #include <stdio.h>
28 #include <string.h>
29 
30 /* somewhat unix-specific */
31 #include <sys/time.h>
32 #include <unistd.h>
33 
34 /* curl stuff */
35 #include <curl/curl.h>
36 
37 #ifdef _WIN32
38 #define WAITMS(x) Sleep(x)
39 #else
40 /* Portable sleep for platforms other than Windows. */
41 #define WAITMS(x)                               \
42   struct timeval wait = { 0, (x) * 1000 };      \
43   (void)select(0, NULL, NULL, NULL, &wait)
44 #endif
45 
46 /*
47  * Simply download a HTTP file.
48  */
main(void)49 int main(void)
50 {
51   CURL *http_handle;
52   CURLM *multi_handle;
53   int still_running = 1; /* keep number of running handles */
54 
55   curl_global_init(CURL_GLOBAL_DEFAULT);
56 
57   http_handle = curl_easy_init();
58 
59   /* set the options (I left out a few, you'll get the point anyway) */
60   curl_easy_setopt(http_handle, CURLOPT_URL, "https://www.example.com/");
61 
62   /* init a multi stack */
63   multi_handle = curl_multi_init();
64 
65   /* add the individual transfers */
66   curl_multi_add_handle(multi_handle, http_handle);
67 
68   do {
69     CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
70 
71     if(!mc)
72       /* wait for activity, timeout or "nothing" */
73       mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
74 
75     if(mc) {
76       fprintf(stderr, "curl_multi_poll() failed, code %d.\n", (int)mc);
77       break;
78     }
79 
80   } while(still_running);
81 
82   curl_multi_remove_handle(multi_handle, http_handle);
83 
84   curl_easy_cleanup(http_handle);
85 
86   curl_multi_cleanup(multi_handle);
87 
88   curl_global_cleanup();
89 
90   return 0;
91 }
92