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