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