1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, 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 https://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 ***************************************************************************/
22 #include "tool_setup.h"
24 #define ENABLE_CURLX_PRINTF
25 /* use our own printf() functions */
28 #include "tool_cfgable.h"
29 #include "tool_msgs.h"
30 #include "tool_cb_wrt.h"
32 #include "memdebug.h" /* keep this as LAST include */
34 /* create a local file for writing, return TRUE on success */
35 bool tool_create_output_file(struct OutStruct *outs)
37 struct GlobalConfig *global = outs->config->global;
40 if(!outs->filename || !*outs->filename) {
41 warnf(global, "Remote filename has no length!\n");
45 if(outs->is_cd_filename) {
46 /* don't overwrite existing files */
47 file = fopen(outs->filename, "rb");
50 warnf(global, "Refusing to overwrite %s: %s\n", outs->filename,
56 /* open file for writing */
57 file = fopen(outs->filename, "wb");
59 warnf(global, "Failed to create the file %s: %s\n", outs->filename,
72 ** callback for CURLOPT_WRITEFUNCTION
75 size_t tool_write_cb(char *buffer, size_t sz, size_t nmemb, void *userdata)
78 struct OutStruct *outs = userdata;
79 struct OperationConfig *config = outs->config;
80 size_t bytes = sz * nmemb;
81 bool is_tty = config->global->isatty;
84 * Once that libcurl has called back tool_write_cb() the returned value
85 * is checked against the amount that was intended to be written, if
86 * it does not match then it fails with CURLE_WRITE_ERROR. So at this
87 * point returning a value different from sz*nmemb indicates failure.
89 const size_t failure = bytes ? 0 : 1;
93 char *tty = curlx_getenv("CURL_ISATTY");
100 if(config->include_headers) {
101 if(bytes > (size_t)CURL_MAX_HTTP_HEADER) {
102 warnf(config->global, "Header data size exceeds single call write "
108 if(bytes > (size_t)CURL_MAX_WRITE_SIZE) {
109 warnf(config->global, "Data size exceeds single call write limit!\n");
115 /* Some internal congruency checks on received OutStruct */
116 bool check_fails = FALSE;
123 if(outs->fopened && !outs->stream)
125 if(!outs->fopened && outs->stream)
127 if(!outs->fopened && outs->bytes)
131 /* standard stream */
132 if(!outs->stream || outs->s_isreg || outs->fopened)
134 if(outs->alloc_filename || outs->is_cd_filename || outs->init)
138 warnf(config->global, "Invalid output struct data for write callback\n");
144 if(!outs->stream && !tool_create_output_file(outs))
147 if(is_tty && (outs->bytes < 2000) && !config->terminal_binary_ok) {
148 /* binary output to terminal? */
149 if(memchr(buffer, 0, bytes)) {
150 warnf(config->global, "Binary output can mess up your terminal. "
151 "Use \"--output -\" to tell curl to output it to your terminal "
152 "anyway, or consider \"--output <FILE>\" to save to a file.\n");
153 config->synthetic_error = ERR_BINARY_TERMINAL;
158 rc = fwrite(buffer, sz, nmemb, outs->stream);
161 /* we added this amount of data to the output */
162 outs->bytes += bytes;
164 if(config->readbusy) {
165 config->readbusy = FALSE;
166 curl_easy_pause(config->easy, CURLPAUSE_CONT);
169 if(config->nobuffer) {
170 /* output buffering disabled */
171 int res = fflush(outs->stream);