docs/example patches for VMS
[platform/upstream/curl.git] / docs / examples / multi-debugcallback.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * $Id$
9  *
10  * This is a very simple example using the multi interface and the debug
11  * callback.
12  */
13
14 #include <stdio.h>
15 #include <string.h>
16
17 /* somewhat unix-specific */
18 #include <sys/time.h>
19 #include <unistd.h>
20
21 /* curl stuff */
22 #include <curl/curl.h>
23
24 typedef char bool;
25 #define TRUE 1
26
27 static
28 void dump(const char *text,
29           FILE *stream, unsigned char *ptr, size_t size,
30           bool nohex)
31 {
32   size_t i;
33   size_t c;
34
35   unsigned int width=0x10;
36
37   if(nohex)
38     /* without the hex output, we can fit more on screen */
39     width = 0x40;
40
41   fprintf(stream, "%s, %010.10ld bytes (0x%08.8lx)\n",
42           text, (long)size, (long)size);
43
44   for(i=0; i<size; i+= width) {
45
46     fprintf(stream, "%04.4lx: ", (long)i);
47
48     if(!nohex) {
49       /* hex not disabled, show it */
50       for(c = 0; c < width; c++)
51         if(i+c < size)
52           fprintf(stream, "%02x ", ptr[i+c]);
53         else
54           fputs("   ", stream);
55     }
56
57     for(c = 0; (c < width) && (i+c < size); c++) {
58       /* check for 0D0A; if found, skip past and start a new line of output */
59       if (nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
60         i+=(c+2-width);
61         break;
62       }
63       fprintf(stream, "%c",
64               (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
65       /* check again for 0D0A, to avoid an extra \n if it's at width */
66       if (nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
67         i+=(c+3-width);
68         break;
69       }
70     }
71     fputc('\n', stream); /* newline */
72   }
73   fflush(stream);
74 }
75
76 static
77 int my_trace(CURL *handle, curl_infotype type,
78              unsigned char *data, size_t size,
79              void *userp)
80 {
81   const char *text;
82
83   (void)handle; /* prevent compiler warning */
84
85   switch (type) {
86   case CURLINFO_TEXT:
87     fprintf(stderr, "== Info: %s", data);
88   default: /* in case a new one is introduced to shock us */
89     return 0;
90
91   case CURLINFO_HEADER_OUT:
92     text = "=> Send header";
93     break;
94   case CURLINFO_DATA_OUT:
95     text = "=> Send data";
96     break;
97   case CURLINFO_HEADER_IN:
98     text = "<= Recv header";
99     break;
100   case CURLINFO_DATA_IN:
101     text = "<= Recv data";
102     break;
103   }
104
105   dump(text, stderr, data, size, TRUE);
106   return 0;
107 }
108
109 /*
110  * Simply download a HTTP file.
111  */
112 int main(int argc, char **argv)
113 {
114   CURL *http_handle;
115   CURLM *multi_handle;
116
117   int still_running; /* keep number of running handles */
118
119   http_handle = curl_easy_init();
120
121   /* set the options (I left out a few, you'll get the point anyway) */
122   curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.haxx.se/");
123
124   curl_easy_setopt(http_handle, CURLOPT_DEBUGFUNCTION, my_trace);
125   curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L);
126
127   /* init a multi stack */
128   multi_handle = curl_multi_init();
129
130   /* add the individual transfers */
131   curl_multi_add_handle(multi_handle, http_handle);
132
133   /* we start some action by calling perform right away */
134   while(CURLM_CALL_MULTI_PERFORM ==
135         curl_multi_perform(multi_handle, &still_running));
136
137   while(still_running) {
138     struct timeval timeout;
139     int rc; /* select() return code */
140
141     fd_set fdread;
142     fd_set fdwrite;
143     fd_set fdexcep;
144     int maxfd;
145
146     FD_ZERO(&fdread);
147     FD_ZERO(&fdwrite);
148     FD_ZERO(&fdexcep);
149
150     /* set a suitable timeout to play around with */
151     timeout.tv_sec = 1;
152     timeout.tv_usec = 0;
153
154     /* get file descriptors from the transfers */
155     curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
156
157     /* In a real-world program you OF COURSE check the return code of the
158        function calls, *and* you make sure that maxfd is bigger than -1
159        so that the call to select() below makes sense! */
160
161     rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
162
163     switch(rc) {
164     case -1:
165       /* select error */
166       still_running = 0;
167       printf("select() returns error, this is badness\n");
168       break;
169     case 0:
170     default:
171       /* timeout or readable/writable sockets */
172       while(CURLM_CALL_MULTI_PERFORM ==
173             curl_multi_perform(multi_handle, &still_running));
174       break;
175     }
176   }
177
178   curl_multi_cleanup(multi_handle);
179
180   curl_easy_cleanup(http_handle);
181
182   return 0;
183 }