Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denys Vlasenko | a02a4e9 | 2017-10-05 15:19:25 +0200 | [diff] [blame] | 2 | /* |
| 3 | * printf - format and print data |
Denys Vlasenko | ebe6d9d | 2017-10-05 14:40:24 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright 1999 Dave Cinege |
| 6 | * Portions copyright (C) 1990-1996 Free Software Foundation, Inc. |
| 7 | * |
| 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
| 9 | */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 10 | /* Usage: printf format [argument...] |
Denys Vlasenko | ebe6d9d | 2017-10-05 14:40:24 +0200 | [diff] [blame] | 11 | * |
| 12 | * A front end to the printf function that lets it be used from the shell. |
| 13 | * |
| 14 | * Backslash escapes: |
| 15 | * |
| 16 | * \" = double quote |
| 17 | * \\ = backslash |
| 18 | * \a = alert (bell) |
| 19 | * \b = backspace |
| 20 | * \c = produce no further output |
| 21 | * \f = form feed |
| 22 | * \n = new line |
| 23 | * \r = carriage return |
| 24 | * \t = horizontal tab |
| 25 | * \v = vertical tab |
| 26 | * \0ooo = octal number (ooo is 0 to 3 digits) |
| 27 | * \xhhh = hexadecimal number (hhh is 1 to 3 digits) |
| 28 | * |
| 29 | * Additional directive: |
| 30 | * |
| 31 | * %b = print an argument string, interpreting backslash escapes |
| 32 | * |
| 33 | * The 'format' argument is re-used as many times as necessary |
| 34 | * to convert all of the given arguments. |
| 35 | * |
| 36 | * David MacKenzie <djm@gnu.ai.mit.edu> |
| 37 | */ |
Denys Vlasenko | d4acaf7 | 2012-03-07 11:44:15 +0100 | [diff] [blame] | 38 | /* 19990508 Busy Boxed! Dave Cinege */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 39 | |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 40 | //config:config PRINTF |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 41 | //config: bool "printf (3.8 kb)" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 42 | //config: default y |
| 43 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 44 | //config: printf is used to format and print specified strings. |
Denys Vlasenko | 18e781d | 2017-08-02 14:12:48 +0200 | [diff] [blame] | 45 | //config: It's similar to 'echo' except it has more options. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 46 | |
| 47 | //applet:IF_PRINTF(APPLET_NOFORK(printf, printf, BB_DIR_USR_BIN, BB_SUID_DROP, printf)) |
| 48 | |
| 49 | //kbuild:lib-$(CONFIG_PRINTF) += printf.o |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 50 | //kbuild:lib-$(CONFIG_ASH_PRINTF) += printf.o |
| 51 | //kbuild:lib-$(CONFIG_HUSH_PRINTF) += printf.o |
| 52 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 53 | //usage:#define printf_trivial_usage |
Denys Vlasenko | d4acaf7 | 2012-03-07 11:44:15 +0100 | [diff] [blame] | 54 | //usage: "FORMAT [ARG]..." |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 55 | //usage:#define printf_full_usage "\n\n" |
Denys Vlasenko | d4acaf7 | 2012-03-07 11:44:15 +0100 | [diff] [blame] | 56 | //usage: "Format and print ARG(s) according to FORMAT (a-la C printf)" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 57 | //usage: |
| 58 | //usage:#define printf_example_usage |
| 59 | //usage: "$ printf \"Val=%d\\n\" 5\n" |
| 60 | //usage: "Val=5\n" |
| 61 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 62 | #include "libbb.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 63 | |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 64 | /* A note on bad input: neither bash 3.2 nor coreutils 6.10 stop on it. |
| 65 | * They report it: |
| 66 | * bash: printf: XXX: invalid number |
| 67 | * printf: XXX: expected a numeric value |
| 68 | * bash: printf: 123XXX: invalid number |
| 69 | * printf: 123XXX: value not completely converted |
| 70 | * but then they use 0 (or partially converted numeric prefix) as a value |
| 71 | * and continue. They exit with 1 in this case. |
| 72 | * Both accept insane field width/precision (e.g. %9999999999.9999999999d). |
| 73 | * Both print error message and assume 0 if %*.*f width/precision is "bad" |
| 74 | * (but negative numbers are not "bad"). |
| 75 | * Both accept negative numbers for %u specifier. |
| 76 | * |
Denys Vlasenko | bf4aeed | 2009-06-18 22:22:04 +0200 | [diff] [blame] | 77 | * We try to be compatible. |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 78 | */ |
Denis Vlasenko | c290563 | 2006-09-23 16:01:09 +0000 | [diff] [blame] | 79 | |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 80 | typedef void FAST_FUNC (*converter)(const char *arg, void *result); |
| 81 | |
| 82 | static int multiconvert(const char *arg, void *result, converter convert) |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 83 | { |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 84 | if (*arg == '"' || *arg == '\'') { |
Denis Vlasenko | 5f11662 | 2008-07-18 18:41:55 +0000 | [diff] [blame] | 85 | arg = utoa((unsigned char)arg[1]); |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 86 | } |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 87 | errno = 0; |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 88 | convert(arg, result); |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 89 | if (errno) { |
Denys Vlasenko | b32a543 | 2010-08-29 13:29:02 +0200 | [diff] [blame] | 90 | bb_error_msg("invalid number '%s'", arg); |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 91 | return 1; |
| 92 | } |
| 93 | return 0; |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 94 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 95 | |
Denis Vlasenko | cb39a7c | 2009-01-04 02:58:58 +0000 | [diff] [blame] | 96 | static void FAST_FUNC conv_strtoull(const char *arg, void *result) |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 97 | { |
Denys Vlasenko | 93ef5dd | 2018-10-30 23:24:18 +0100 | [diff] [blame] | 98 | /* Allow leading '+' - bb_strtoull() by itself does not allow it, |
| 99 | * and probably shouldn't (other callers might require purely numeric |
| 100 | * inputs to be allowed. |
| 101 | */ |
| 102 | if (arg[0] == '+') |
| 103 | arg++; |
Denis Vlasenko | cb39a7c | 2009-01-04 02:58:58 +0000 | [diff] [blame] | 104 | *(unsigned long long*)result = bb_strtoull(arg, NULL, 0); |
Denys Vlasenko | 71016ba | 2009-06-05 16:24:29 +0200 | [diff] [blame] | 105 | /* both coreutils 6.10 and bash 3.2: |
| 106 | * $ printf '%x\n' -2 |
| 107 | * fffffffffffffffe |
| 108 | * Mimic that: |
| 109 | */ |
| 110 | if (errno) { |
| 111 | *(unsigned long long*)result = bb_strtoll(arg, NULL, 0); |
| 112 | } |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 113 | } |
Denis Vlasenko | cb39a7c | 2009-01-04 02:58:58 +0000 | [diff] [blame] | 114 | static void FAST_FUNC conv_strtoll(const char *arg, void *result) |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 115 | { |
Denys Vlasenko | 93ef5dd | 2018-10-30 23:24:18 +0100 | [diff] [blame] | 116 | if (arg[0] == '+') |
| 117 | arg++; |
Denis Vlasenko | cb39a7c | 2009-01-04 02:58:58 +0000 | [diff] [blame] | 118 | *(long long*)result = bb_strtoll(arg, NULL, 0); |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 119 | } |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 120 | static void FAST_FUNC conv_strtod(const char *arg, void *result) |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 121 | { |
| 122 | char *end; |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 123 | /* Well, this one allows leading whitespace... so what? */ |
| 124 | /* What I like much less is that "-" accepted too! :( */ |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 125 | *(double*)result = strtod(arg, &end); |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 126 | if (end[0]) { |
| 127 | errno = ERANGE; |
| 128 | *(double*)result = 0; |
| 129 | } |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 132 | /* Callers should check errno to detect errors */ |
Denis Vlasenko | cb39a7c | 2009-01-04 02:58:58 +0000 | [diff] [blame] | 133 | static unsigned long long my_xstrtoull(const char *arg) |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 134 | { |
Denis Vlasenko | cb39a7c | 2009-01-04 02:58:58 +0000 | [diff] [blame] | 135 | unsigned long long result; |
| 136 | if (multiconvert(arg, &result, conv_strtoull)) |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 137 | result = 0; |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 138 | return result; |
| 139 | } |
Denis Vlasenko | cb39a7c | 2009-01-04 02:58:58 +0000 | [diff] [blame] | 140 | static long long my_xstrtoll(const char *arg) |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 141 | { |
Denis Vlasenko | cb39a7c | 2009-01-04 02:58:58 +0000 | [diff] [blame] | 142 | long long result; |
| 143 | if (multiconvert(arg, &result, conv_strtoll)) |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 144 | result = 0; |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 145 | return result; |
| 146 | } |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 147 | static double my_xstrtod(const char *arg) |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 148 | { |
| 149 | double result; |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 150 | multiconvert(arg, &result, conv_strtod); |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 151 | return result; |
| 152 | } |
| 153 | |
Ron Yorston | 4a79224 | 2016-01-31 22:22:25 +0100 | [diff] [blame] | 154 | /* Handles %b; return 1 if output is to be short-circuited by \c */ |
| 155 | static int print_esc_string(const char *str) |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 156 | { |
Denys Vlasenko | 5360059 | 2010-10-23 21:06:06 +0200 | [diff] [blame] | 157 | char c; |
| 158 | while ((c = *str) != '\0') { |
| 159 | str++; |
Denys Vlasenko | 69d81a1 | 2012-03-07 11:57:47 +0100 | [diff] [blame] | 160 | if (c == '\\') { |
| 161 | /* %b also accepts 4-digit octals of the form \0### */ |
| 162 | if (*str == '0') { |
| 163 | if ((unsigned char)(str[1] - '0') < 8) { |
| 164 | /* 2nd char is 0..7: skip leading '0' */ |
| 165 | str++; |
| 166 | } |
| 167 | } |
Ron Yorston | 4a79224 | 2016-01-31 22:22:25 +0100 | [diff] [blame] | 168 | else if (*str == 'c') { |
| 169 | return 1; |
| 170 | } |
Denys Vlasenko | 69d81a1 | 2012-03-07 11:57:47 +0100 | [diff] [blame] | 171 | { |
| 172 | /* optimization: don't force arg to be on-stack, |
| 173 | * use another variable for that. */ |
| 174 | const char *z = str; |
| 175 | c = bb_process_escape_sequence(&z); |
| 176 | str = z; |
| 177 | } |
| 178 | } |
Denys Vlasenko | 5360059 | 2010-10-23 21:06:06 +0200 | [diff] [blame] | 179 | putchar(c); |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 180 | } |
Ron Yorston | 4a79224 | 2016-01-31 22:22:25 +0100 | [diff] [blame] | 181 | |
| 182 | return 0; |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 185 | static void print_direc(char *format, unsigned fmt_length, |
| 186 | int field_width, int precision, |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 187 | const char *argument) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 188 | { |
Denis Vlasenko | cb39a7c | 2009-01-04 02:58:58 +0000 | [diff] [blame] | 189 | long long llv; |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 190 | double dv; |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 191 | char saved; |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 192 | char *have_prec, *have_width; |
| 193 | |
Denis Vlasenko | 6852eff | 2009-03-03 14:14:44 +0000 | [diff] [blame] | 194 | saved = format[fmt_length]; |
| 195 | format[fmt_length] = '\0'; |
| 196 | |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 197 | have_prec = strstr(format, ".*"); |
| 198 | have_width = strchr(format, '*'); |
| 199 | if (have_width - 1 == have_prec) |
| 200 | have_width = NULL; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 201 | |
Bernhard Reutner-Fischer | 3db4e7f | 2018-10-19 15:25:41 +0200 | [diff] [blame] | 202 | /* multiconvert sets errno = 0, but %s needs it cleared */ |
Denys Vlasenko | 7e60343 | 2009-06-27 00:07:23 +0200 | [diff] [blame] | 203 | errno = 0; |
| 204 | |
Denis Vlasenko | a76669c | 2008-05-31 18:32:56 +0000 | [diff] [blame] | 205 | switch (format[fmt_length - 1]) { |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 206 | case 'c': |
| 207 | printf(format, *argument); |
| 208 | break; |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 209 | case 'd': |
| 210 | case 'i': |
Bernhard Reutner-Fischer | 3db4e7f | 2018-10-19 15:25:41 +0200 | [diff] [blame] | 211 | llv = my_xstrtoll(skip_whitespace(argument)); |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 212 | print_long: |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 213 | if (!have_width) { |
| 214 | if (!have_prec) |
Denis Vlasenko | cb39a7c | 2009-01-04 02:58:58 +0000 | [diff] [blame] | 215 | printf(format, llv); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 216 | else |
Denis Vlasenko | cb39a7c | 2009-01-04 02:58:58 +0000 | [diff] [blame] | 217 | printf(format, precision, llv); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 218 | } else { |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 219 | if (!have_prec) |
Denis Vlasenko | cb39a7c | 2009-01-04 02:58:58 +0000 | [diff] [blame] | 220 | printf(format, field_width, llv); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 221 | else |
Denis Vlasenko | cb39a7c | 2009-01-04 02:58:58 +0000 | [diff] [blame] | 222 | printf(format, field_width, precision, llv); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 223 | } |
| 224 | break; |
| 225 | case 'o': |
| 226 | case 'u': |
| 227 | case 'x': |
| 228 | case 'X': |
Bernhard Reutner-Fischer | 3db4e7f | 2018-10-19 15:25:41 +0200 | [diff] [blame] | 229 | llv = my_xstrtoull(skip_whitespace(argument)); |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 230 | /* cheat: unsigned long and long have same width, so... */ |
| 231 | goto print_long; |
| 232 | case 's': |
Denis Vlasenko | cb39a7c | 2009-01-04 02:58:58 +0000 | [diff] [blame] | 233 | /* Are char* and long long the same? */ |
| 234 | if (sizeof(argument) == sizeof(llv)) { |
| 235 | llv = (long long)(ptrdiff_t)argument; |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 236 | goto print_long; |
Denis Vlasenko | cb39a7c | 2009-01-04 02:58:58 +0000 | [diff] [blame] | 237 | } else { |
| 238 | /* Hope compiler will optimize it out by moving call |
| 239 | * instruction after the ifs... */ |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 240 | if (!have_width) { |
| 241 | if (!have_prec) |
Denis Vlasenko | cb39a7c | 2009-01-04 02:58:58 +0000 | [diff] [blame] | 242 | printf(format, argument, /*unused:*/ argument, argument); |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 243 | else |
Denis Vlasenko | cb39a7c | 2009-01-04 02:58:58 +0000 | [diff] [blame] | 244 | printf(format, precision, argument, /*unused:*/ argument); |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 245 | } else { |
| 246 | if (!have_prec) |
Denis Vlasenko | cb39a7c | 2009-01-04 02:58:58 +0000 | [diff] [blame] | 247 | printf(format, field_width, argument, /*unused:*/ argument); |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 248 | else |
| 249 | printf(format, field_width, precision, argument); |
| 250 | } |
| 251 | break; |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 252 | } |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 253 | case 'f': |
| 254 | case 'e': |
| 255 | case 'E': |
| 256 | case 'g': |
| 257 | case 'G': |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 258 | dv = my_xstrtod(argument); |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 259 | if (!have_width) { |
| 260 | if (!have_prec) |
| 261 | printf(format, dv); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 262 | else |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 263 | printf(format, precision, dv); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 264 | } else { |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 265 | if (!have_prec) |
| 266 | printf(format, field_width, dv); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 267 | else |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 268 | printf(format, field_width, precision, dv); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 269 | } |
| 270 | break; |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 271 | } /* switch */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 272 | |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 273 | format[fmt_length] = saved; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 276 | /* Handle params for "%*.*f". Negative numbers are ok (compat). */ |
| 277 | static int get_width_prec(const char *str) |
| 278 | { |
| 279 | int v = bb_strtoi(str, NULL, 10); |
| 280 | if (errno) { |
Denys Vlasenko | b32a543 | 2010-08-29 13:29:02 +0200 | [diff] [blame] | 281 | bb_error_msg("invalid number '%s'", str); |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 282 | v = 0; |
| 283 | } |
| 284 | return v; |
| 285 | } |
| 286 | |
Denis Vlasenko | b6c4855 | 2008-05-18 14:28:40 +0000 | [diff] [blame] | 287 | /* Print the text in FORMAT, using ARGV for arguments to any '%' directives. |
| 288 | Return advanced ARGV. */ |
Denys Vlasenko | bf4aeed | 2009-06-18 22:22:04 +0200 | [diff] [blame] | 289 | static char **print_formatted(char *f, char **argv, int *conv_err) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 290 | { |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 291 | char *direc_start; /* Start of % directive. */ |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 292 | unsigned direc_length; /* Length of % directive. */ |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 293 | int field_width; /* Arg to first '*' */ |
| 294 | int precision; /* Arg to second '*' */ |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 295 | char **saved_argv = argv; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 296 | |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 297 | for (; *f; ++f) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 298 | switch (*f) { |
| 299 | case '%': |
| 300 | direc_start = f++; |
| 301 | direc_length = 1; |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 302 | field_width = precision = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 303 | if (*f == '%') { |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 304 | bb_putchar('%'); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 305 | break; |
| 306 | } |
| 307 | if (*f == 'b') { |
Denis Vlasenko | b6c4855 | 2008-05-18 14:28:40 +0000 | [diff] [blame] | 308 | if (*argv) { |
Ron Yorston | 4a79224 | 2016-01-31 22:22:25 +0100 | [diff] [blame] | 309 | if (print_esc_string(*argv)) |
| 310 | return saved_argv; /* causes main() to exit */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 311 | ++argv; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 312 | } |
| 313 | break; |
| 314 | } |
Ron Yorston | 513a245 | 2017-07-18 09:33:22 +0100 | [diff] [blame] | 315 | if (*f && strchr("-+ #", *f)) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 316 | ++f; |
| 317 | ++direc_length; |
| 318 | } |
| 319 | if (*f == '*') { |
| 320 | ++f; |
| 321 | ++direc_length; |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 322 | if (*argv) |
| 323 | field_width = get_width_prec(*argv++); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 324 | } else { |
Glenn L McGrath | 240a91d | 2004-09-15 02:05:23 +0000 | [diff] [blame] | 325 | while (isdigit(*f)) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 326 | ++f; |
| 327 | ++direc_length; |
| 328 | } |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 329 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 330 | if (*f == '.') { |
| 331 | ++f; |
| 332 | ++direc_length; |
| 333 | if (*f == '*') { |
| 334 | ++f; |
| 335 | ++direc_length; |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 336 | if (*argv) |
| 337 | precision = get_width_prec(*argv++); |
| 338 | } else { |
Glenn L McGrath | 240a91d | 2004-09-15 02:05:23 +0000 | [diff] [blame] | 339 | while (isdigit(*f)) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 340 | ++f; |
| 341 | ++direc_length; |
| 342 | } |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 343 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 344 | } |
Denis Vlasenko | cb39a7c | 2009-01-04 02:58:58 +0000 | [diff] [blame] | 345 | |
| 346 | /* Remove "lLhz" size modifiers, repeatedly. |
| 347 | * bash does not like "%lld", but coreutils |
Denys Vlasenko | bf4aeed | 2009-06-18 22:22:04 +0200 | [diff] [blame] | 348 | * happily takes even "%Llllhhzhhzd"! |
| 349 | * We are permissive like coreutils */ |
Denis Vlasenko | cb39a7c | 2009-01-04 02:58:58 +0000 | [diff] [blame] | 350 | while ((*f | 0x20) == 'l' || *f == 'h' || *f == 'z') { |
Denis Vlasenko | 0f293b9 | 2008-07-22 20:16:55 +0000 | [diff] [blame] | 351 | overlapping_strcpy(f, f + 1); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 352 | } |
Denis Vlasenko | cb39a7c | 2009-01-04 02:58:58 +0000 | [diff] [blame] | 353 | /* Add "ll" if integer modifier, then print */ |
| 354 | { |
| 355 | static const char format_chars[] ALIGN1 = "diouxXfeEgGcs"; |
| 356 | char *p = strchr(format_chars, *f); |
| 357 | /* needed - try "printf %" without it */ |
Ron Yorston | 513a245 | 2017-07-18 09:33:22 +0100 | [diff] [blame] | 358 | if (p == NULL || *f == '\0') { |
Denis Vlasenko | cb39a7c | 2009-01-04 02:58:58 +0000 | [diff] [blame] | 359 | bb_error_msg("%s: invalid format", direc_start); |
| 360 | /* causes main() to exit with error */ |
| 361 | return saved_argv - 1; |
| 362 | } |
| 363 | ++direc_length; |
| 364 | if (p - format_chars <= 5) { |
| 365 | /* it is one of "diouxX" */ |
| 366 | p = xmalloc(direc_length + 3); |
| 367 | memcpy(p, direc_start, direc_length); |
| 368 | p[direc_length + 1] = p[direc_length - 1]; |
| 369 | p[direc_length - 1] = 'l'; |
| 370 | p[direc_length] = 'l'; |
| 371 | //bb_error_msg("<%s>", p); |
| 372 | direc_length += 2; |
| 373 | direc_start = p; |
| 374 | } else { |
| 375 | p = NULL; |
| 376 | } |
| 377 | if (*argv) { |
| 378 | print_direc(direc_start, direc_length, field_width, |
Denys Vlasenko | bf4aeed | 2009-06-18 22:22:04 +0200 | [diff] [blame] | 379 | precision, *argv++); |
Denis Vlasenko | cb39a7c | 2009-01-04 02:58:58 +0000 | [diff] [blame] | 380 | } else { |
| 381 | print_direc(direc_start, direc_length, field_width, |
| 382 | precision, ""); |
| 383 | } |
Denys Vlasenko | bf4aeed | 2009-06-18 22:22:04 +0200 | [diff] [blame] | 384 | *conv_err |= errno; |
Denis Vlasenko | cb39a7c | 2009-01-04 02:58:58 +0000 | [diff] [blame] | 385 | free(p); |
Denis Vlasenko | 0f683f8 | 2008-07-17 09:17:51 +0000 | [diff] [blame] | 386 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 387 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 388 | case '\\': |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 389 | if (*++f == 'c') { |
| 390 | return saved_argv; /* causes main() to exit */ |
| 391 | } |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 392 | bb_putchar(bb_process_escape_sequence((const char **)&f)); |
Glenn L McGrath | 240a91d | 2004-09-15 02:05:23 +0000 | [diff] [blame] | 393 | f--; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 394 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 395 | default: |
Denys Vlasenko | 5360059 | 2010-10-23 21:06:06 +0200 | [diff] [blame] | 396 | putchar(*f); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 397 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 398 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 399 | |
Denis Vlasenko | b6c4855 | 2008-05-18 14:28:40 +0000 | [diff] [blame] | 400 | return argv; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 403 | int printf_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 404 | { |
Denys Vlasenko | bf4aeed | 2009-06-18 22:22:04 +0200 | [diff] [blame] | 405 | int conv_err; |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 406 | char *format; |
Denis Vlasenko | b6c4855 | 2008-05-18 14:28:40 +0000 | [diff] [blame] | 407 | char **argv2; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 408 | |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 409 | /* We must check that stdout is not closed. |
Denis Vlasenko | 0f683f8 | 2008-07-17 09:17:51 +0000 | [diff] [blame] | 410 | * The reason for this is highly non-obvious. |
| 411 | * printf_main is used from shell. |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 412 | * Shell must correctly handle 'printf "%s" foo' |
| 413 | * if stdout is closed. With stdio, output gets shoveled into |
| 414 | * stdout buffer, and even fflush cannot clear it out. It seems that |
| 415 | * even if libc receives EBADF on write attempts, it feels determined |
| 416 | * to output data no matter what. So it will try later, |
| 417 | * and possibly will clobber future output. Not good. */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 418 | // TODO: check fcntl() & O_ACCMODE == O_WRONLY or O_RDWR? |
| 419 | if (fcntl(1, F_GETFL) == -1) |
| 420 | return 1; /* match coreutils 6.10 (sans error msg to stderr) */ |
| 421 | //if (dup2(1, 1) != 1) - old way |
| 422 | // return 1; |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 423 | |
Denis Vlasenko | b6c4855 | 2008-05-18 14:28:40 +0000 | [diff] [blame] | 424 | /* bash builtin errors out on "printf '-%s-\n' foo", |
| 425 | * coreutils-6.9 works. Both work with "printf -- '-%s-\n' foo". |
| 426 | * We will mimic coreutils. */ |
Denis Vlasenko | 0f683f8 | 2008-07-17 09:17:51 +0000 | [diff] [blame] | 427 | if (argv[1] && argv[1][0] == '-' && argv[1][1] == '-' && !argv[1][2]) |
Denis Vlasenko | b6c4855 | 2008-05-18 14:28:40 +0000 | [diff] [blame] | 428 | argv++; |
Denis Vlasenko | 311c19a | 2008-12-10 11:51:45 +0000 | [diff] [blame] | 429 | if (!argv[1]) { |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 430 | if (ENABLE_ASH_PRINTF |
Denis Vlasenko | 311c19a | 2008-12-10 11:51:45 +0000 | [diff] [blame] | 431 | && applet_name[0] != 'p' |
| 432 | ) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 433 | bb_simple_error_msg("usage: printf FORMAT [ARGUMENT...]"); |
Denis Vlasenko | 311c19a | 2008-12-10 11:51:45 +0000 | [diff] [blame] | 434 | return 2; /* bash compat */ |
| 435 | } |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 436 | bb_show_usage(); |
Denis Vlasenko | 311c19a | 2008-12-10 11:51:45 +0000 | [diff] [blame] | 437 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 438 | |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 439 | format = argv[1]; |
Denis Vlasenko | b6c4855 | 2008-05-18 14:28:40 +0000 | [diff] [blame] | 440 | argv2 = argv + 2; |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 441 | |
Denys Vlasenko | bf4aeed | 2009-06-18 22:22:04 +0200 | [diff] [blame] | 442 | conv_err = 0; |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 443 | do { |
Denis Vlasenko | b6c4855 | 2008-05-18 14:28:40 +0000 | [diff] [blame] | 444 | argv = argv2; |
Denys Vlasenko | bf4aeed | 2009-06-18 22:22:04 +0200 | [diff] [blame] | 445 | argv2 = print_formatted(format, argv, &conv_err); |
Denis Vlasenko | 0f683f8 | 2008-07-17 09:17:51 +0000 | [diff] [blame] | 446 | } while (argv2 > argv && *argv2); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 447 | |
Denis Vlasenko | b6c4855 | 2008-05-18 14:28:40 +0000 | [diff] [blame] | 448 | /* coreutils compat (bash doesn't do this): |
| 449 | if (*argv) |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 450 | fprintf(stderr, "excess args ignored"); |
Denis Vlasenko | b6c4855 | 2008-05-18 14:28:40 +0000 | [diff] [blame] | 451 | */ |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 452 | |
Denys Vlasenko | bf4aeed | 2009-06-18 22:22:04 +0200 | [diff] [blame] | 453 | return (argv2 < argv) /* if true, print_formatted errored out */ |
| 454 | || conv_err; /* print_formatted saw invalid number */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 455 | } |