1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * $Id: writeout.c,v 1.21 2004/02/25 15:41:36 bagder Exp $
22 ***************************************************************************/
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
32 #ifdef HAVE_SYS_SELECT_H
33 #include <sys/select.h>
36 #include <curl/curl.h>
38 #define _MPRINTF_REPLACE /* we want curl-functions instead of native ones */
39 #include <curl/mprintf.h>
44 VAR_NONE, /* must be the first */
49 VAR_STARTTRANSFER_TIME,
59 VAR_NUM_OF_VARS /* must be the last */
68 static struct variable replacements[]={
69 {"url_effective", VAR_EFFECTIVE_URL},
70 {"http_code", VAR_HTTP_CODE},
71 {"time_total", VAR_TOTAL_TIME},
72 {"time_namelookup", VAR_NAMELOOKUP_TIME},
73 {"time_connect", VAR_CONNECT_TIME},
74 {"time_pretransfer", VAR_PRETRANSFER_TIME},
75 {"time_starttransfer", VAR_STARTTRANSFER_TIME},
76 {"size_header", VAR_HEADER_SIZE},
77 {"size_request", VAR_REQUEST_SIZE},
78 {"size_download", VAR_SIZE_DOWNLOAD},
79 {"size_upload", VAR_SIZE_UPLOAD},
80 {"speed_download", VAR_SPEED_DOWNLOAD},
81 {"speed_upload", VAR_SPEED_UPLOAD},
82 {"content_type", VAR_CONTENT_TYPE},
86 void ourWriteOut(CURL *curl, char *writeinfo)
88 FILE *stream = stdout;
97 /* an escaped %-letter */
102 /* this is meant as a variable to output */
106 if(('{' == ptr[1]) && (end=strchr(ptr, '}'))) {
107 ptr+=2; /* pass the % and the { */
109 *end=0; /* zero terminate */
110 for(i=0; replacements[i].name; i++) {
111 if(curl_strequal(ptr, replacements[i].name)) {
112 switch(replacements[i].id) {
113 case VAR_EFFECTIVE_URL:
115 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &stringp))
117 fputs(stringp, stream);
121 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &longinfo))
122 fprintf(stream, "%03ld", longinfo);
124 case VAR_HEADER_SIZE:
126 curl_easy_getinfo(curl, CURLINFO_HEADER_SIZE, &longinfo))
127 fprintf(stream, "%ld", longinfo);
129 case VAR_REQUEST_SIZE:
131 curl_easy_getinfo(curl, CURLINFO_REQUEST_SIZE, &longinfo))
132 fprintf(stream, "%ld", longinfo);
136 curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &doubleinfo))
137 fprintf(stream, "%.3f", doubleinfo);
139 case VAR_NAMELOOKUP_TIME:
141 curl_easy_getinfo(curl, CURLINFO_NAMELOOKUP_TIME,
143 fprintf(stream, "%.3f", doubleinfo);
145 case VAR_CONNECT_TIME:
147 curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME, &doubleinfo))
148 fprintf(stream, "%.3f", doubleinfo);
150 case VAR_PRETRANSFER_TIME:
152 curl_easy_getinfo(curl, CURLINFO_PRETRANSFER_TIME, &doubleinfo))
153 fprintf(stream, "%.3f", doubleinfo);
155 case VAR_STARTTRANSFER_TIME:
157 curl_easy_getinfo(curl, CURLINFO_STARTTRANSFER_TIME, &doubleinfo))
158 fprintf(stream, "%.3f", doubleinfo);
160 case VAR_SIZE_UPLOAD:
162 curl_easy_getinfo(curl, CURLINFO_SIZE_UPLOAD, &doubleinfo))
163 fprintf(stream, "%.0f", doubleinfo);
165 case VAR_SIZE_DOWNLOAD:
167 curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &doubleinfo))
168 fprintf(stream, "%.0f", doubleinfo);
170 case VAR_SPEED_DOWNLOAD:
172 curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD, &doubleinfo))
173 fprintf(stream, "%.3f", doubleinfo);
175 case VAR_SPEED_UPLOAD:
177 curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &doubleinfo))
178 fprintf(stream, "%.3f", doubleinfo);
180 case VAR_CONTENT_TYPE:
182 curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &stringp))
184 fputs(stringp, stream);
187 /* -Wunreachable-code wrongly complains on this */
193 ptr=end+1; /* pass the end */
197 /* illegal syntax, then just output the characters that are used */
199 fputc(ptr[1], stream);
204 else if('\\' == *ptr) {
216 /* unknown, just output this */
218 fputc(ptr[1], stream);