lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 1998 - 2015, 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.haxx.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 | ***************************************************************************/ |
| 22 | #include "tool_setup.h" |
| 23 | |
| 24 | #define ENABLE_CURLX_PRINTF |
| 25 | /* use our own printf() functions */ |
| 26 | #include "curlx.h" |
| 27 | |
| 28 | #include "tool_cfgable.h" |
| 29 | #include "tool_msgs.h" |
| 30 | |
| 31 | #include "memdebug.h" /* keep this as LAST include */ |
| 32 | |
| 33 | #define WARN_PREFIX "Warning: " |
| 34 | #define NOTE_PREFIX "Note: " |
| 35 | |
| 36 | static void voutf(struct GlobalConfig *config, |
| 37 | const char *prefix, |
| 38 | const char *fmt, |
| 39 | va_list ap) |
| 40 | { |
| 41 | size_t width = (79 - strlen(prefix)); |
| 42 | if(!config->mute) { |
| 43 | size_t len; |
| 44 | char *ptr; |
| 45 | char print_buffer[256]; |
| 46 | |
| 47 | len = vsnprintf(print_buffer, sizeof(print_buffer), fmt, ap); |
| 48 | |
| 49 | ptr = print_buffer; |
| 50 | while(len > 0) { |
| 51 | fputs(prefix, config->errors); |
| 52 | |
| 53 | if(len > width) { |
| 54 | size_t cut = width-1; |
| 55 | |
| 56 | while(!ISSPACE(ptr[cut]) && cut) { |
| 57 | cut--; |
| 58 | } |
| 59 | if(0 == cut) |
| 60 | /* not a single cutting position was found, just cut it at the |
| 61 | max text width then! */ |
| 62 | cut = width-1; |
| 63 | |
| 64 | (void)fwrite(ptr, cut + 1, 1, config->errors); |
| 65 | fputs("\n", config->errors); |
| 66 | ptr += cut+1; /* skip the space too */ |
| 67 | |
| 68 | // changed for CVE-2018-16842 1/1-files-1/1-changes |
| 69 | // len -= cut; |
| 70 | len -= cut + 1; |
| 71 | |
| 72 | } |
| 73 | else { |
| 74 | fputs(ptr, config->errors); |
| 75 | len = 0; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Emit 'note' formatted message on configured 'errors' stream, if verbose was |
| 83 | * selected. |
| 84 | */ |
| 85 | void notef(struct GlobalConfig *config, const char *fmt, ...) |
| 86 | { |
| 87 | va_list ap; |
| 88 | va_start(ap, fmt); |
| 89 | if(config->tracetype) |
| 90 | voutf(config, NOTE_PREFIX, fmt, ap); |
| 91 | va_end(ap); |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Emit warning formatted message on configured 'errors' stream unless |
| 96 | * mute (--silent) was selected. |
| 97 | */ |
| 98 | |
| 99 | void warnf(struct GlobalConfig *config, const char *fmt, ...) |
| 100 | { |
| 101 | va_list ap; |
| 102 | va_start(ap, fmt); |
| 103 | voutf(config, WARN_PREFIX, fmt, ap); |
| 104 | va_end(ap); |
| 105 | } |
| 106 | /* |
| 107 | * Emit help formatted message on given stream. |
| 108 | */ |
| 109 | |
| 110 | void helpf(FILE *errors, const char *fmt, ...) |
| 111 | { |
| 112 | va_list ap; |
| 113 | if(fmt) { |
| 114 | va_start(ap, fmt); |
| 115 | fputs("curl: ", errors); /* prefix it */ |
| 116 | vfprintf(errors, fmt, ap); |
| 117 | va_end(ap); |
| 118 | } |
| 119 | fprintf(errors, "curl: try 'curl --help' " |
| 120 | #ifdef USE_MANUAL |
| 121 | "or 'curl --manual' " |
| 122 | #endif |
| 123 | "for more information\n"); |
| 124 | } |
| 125 | |