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