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