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