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