• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 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  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 
25 /* In this file, stdio.h's stderr macro is not overridden. */
26 #define CURL_DO_NOT_OVERRIDE_STDERR
27 
28 #include "tool_setup.h"
29 
30 #include "tool_stderr.h"
31 
32 #include "memdebug.h" /* keep this as LAST include */
33 
34 /* In other tool files stderr is defined as tool_stderr by tool_setup.h */
35 FILE *tool_stderr;
36 
tool_init_stderr(void)37 void tool_init_stderr(void)
38 {
39   tool_stderr = stderr;
40 }
41 
tool_set_stderr_file(char * filename)42 void tool_set_stderr_file(char *filename)
43 {
44   FILE *fp;
45 
46   if(!filename)
47     return;
48 
49   if(!strcmp(filename, "-")) {
50     tool_stderr = stdout;
51     return;
52   }
53 
54   /* precheck that filename is accessible to lessen the chance that the
55      subsequent freopen will fail. */
56   fp = fopen(filename, FOPEN_WRITETEXT);
57   if(!fp) {
58     fprintf(tool_stderr, "Warning: Failed to open %s!\n", filename);
59     return;
60   }
61   fclose(fp);
62 
63   /* freopen the actual stderr (stdio.h stderr) instead of tool_stderr since
64      the latter may be set to stdout. */
65   fp = freopen(filename, FOPEN_WRITETEXT, stderr);
66   if(!fp) {
67     /* stderr may have been closed by freopen. there is nothing to be done. */
68     DEBUGASSERT(0);
69     return;
70   }
71   tool_stderr = stderr;
72 }
73