Revert "Update to 7.40.1"
[platform/upstream/curl.git] / docs / examples / multi-app.c
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 an example application source code 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  * Download a HTTP file and upload an FTP file simultaneously.
36  */
37
38 #define HANDLECOUNT 2   /* Number of simultaneous transfers */
39 #define HTTP_HANDLE 0   /* Index for the HTTP transfer */
40 #define FTP_HANDLE 1    /* Index for the FTP transfer */
41
42 int main(void)
43 {
44   CURL *handles[HANDLECOUNT];
45   CURLM *multi_handle;
46
47   int still_running; /* keep number of running handles */
48   int i;
49
50   CURLMsg *msg; /* for picking up messages with the transfer status */
51   int msgs_left; /* how many messages are left */
52
53   /* Allocate one CURL handle per transfer */
54   for (i=0; i<HANDLECOUNT; i++)
55       handles[i] = curl_easy_init();
56
57   /* set the options (I left out a few, you'll get the point anyway) */
58   curl_easy_setopt(handles[HTTP_HANDLE], CURLOPT_URL, "http://example.com");
59
60   curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_URL, "ftp://example.com");
61   curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_UPLOAD, 1L);
62
63   /* init a multi stack */
64   multi_handle = curl_multi_init();
65
66   /* add the individual transfers */
67   for (i=0; i<HANDLECOUNT; i++)
68       curl_multi_add_handle(multi_handle, handles[i]);
69
70   /* we start some action by calling perform right away */
71   curl_multi_perform(multi_handle, &still_running);
72
73   do {
74     struct timeval timeout;
75     int rc; /* select() return code */
76
77     fd_set fdread;
78     fd_set fdwrite;
79     fd_set fdexcep;
80     int maxfd = -1;
81
82     long curl_timeo = -1;
83
84     FD_ZERO(&fdread);
85     FD_ZERO(&fdwrite);
86     FD_ZERO(&fdexcep);
87
88     /* set a suitable timeout to play around with */
89     timeout.tv_sec = 1;
90     timeout.tv_usec = 0;
91
92     curl_multi_timeout(multi_handle, &curl_timeo);
93     if(curl_timeo >= 0) {
94       timeout.tv_sec = curl_timeo / 1000;
95       if(timeout.tv_sec > 1)
96         timeout.tv_sec = 1;
97       else
98         timeout.tv_usec = (curl_timeo % 1000) * 1000;
99     }
100
101     /* get file descriptors from the transfers */
102     curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
103
104     /* In a real-world program you OF COURSE check the return code of the
105        function calls.  On success, the value of maxfd is guaranteed to be
106        greater or equal than -1.  We call select(maxfd + 1, ...), specially in
107        case of (maxfd == -1), we call select(0, ...), which is basically equal
108        to sleep. */
109
110     rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
111
112     switch(rc) {
113     case -1:
114       /* select error */
115       break;
116     case 0: /* timeout */
117     default: /* action */
118       curl_multi_perform(multi_handle, &still_running);
119       break;
120     }
121   } while(still_running);
122
123   /* See how the transfers went */
124   while ((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
125     if (msg->msg == CURLMSG_DONE) {
126       int idx, found = 0;
127
128       /* Find out which handle this message is about */
129       for (idx=0; idx<HANDLECOUNT; idx++) {
130         found = (msg->easy_handle == handles[idx]);
131         if(found)
132           break;
133       }
134
135       switch (idx) {
136       case HTTP_HANDLE:
137         printf("HTTP transfer completed with status %d\n", msg->data.result);
138         break;
139       case FTP_HANDLE:
140         printf("FTP transfer completed with status %d\n", msg->data.result);
141         break;
142       }
143     }
144   }
145
146   curl_multi_cleanup(multi_handle);
147
148   /* Free the CURL handles */
149   for (i=0; i<HANDLECOUNT; i++)
150       curl_easy_cleanup(handles[i]);
151
152   return 0;
153 }