2
0

tool_stderr.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #include "tool_setup.h"
  25. #include "tool_stderr.h"
  26. #include "tool_msgs.h"
  27. #include "memdebug.h" /* keep this as LAST include */
  28. FILE *tool_stderr;
  29. void tool_init_stderr(void)
  30. {
  31. /* !checksrc! disable STDERR 1 */
  32. tool_stderr = stderr;
  33. }
  34. void tool_set_stderr_file(struct GlobalConfig *global, char *filename)
  35. {
  36. FILE *fp;
  37. if(!filename)
  38. return;
  39. if(!strcmp(filename, "-")) {
  40. tool_stderr = stdout;
  41. return;
  42. }
  43. /* precheck that filename is accessible to lessen the chance that the
  44. subsequent freopen will fail. */
  45. fp = fopen(filename, FOPEN_WRITETEXT);
  46. if(!fp) {
  47. warnf(global, "Warning: Failed to open %s", filename);
  48. return;
  49. }
  50. fclose(fp);
  51. /* freopen the actual stderr (stdio.h stderr) instead of tool_stderr since
  52. the latter may be set to stdout. */
  53. /* !checksrc! disable STDERR 1 */
  54. fp = freopen(filename, FOPEN_WRITETEXT, stderr);
  55. if(!fp) {
  56. /* stderr may have been closed by freopen. there is nothing to be done. */
  57. DEBUGASSERT(0);
  58. return;
  59. }
  60. /* !checksrc! disable STDERR 1 */
  61. tool_stderr = stderr;
  62. }