unzip: -d should create the dir
[oweals/busybox.git] / coreutils / printf.c
index 5b326c643d3c863ac14ccbcfa6af86381952b4a6..a20fc33013b849414c8c0c95aaa2da009001972e 100644 (file)
@@ -1,42 +1,63 @@
 /* vi: set sw=4 ts=4: */
-/* printf - format and print data
-
-   Copyright 1999 Dave Cinege
-   Portions copyright (C) 1990-1996 Free Software Foundation, Inc.
-
-   Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
-*/
-
+/*
+ * printf - format and print data
+ *
+ * Copyright 1999 Dave Cinege
+ * Portions copyright (C) 1990-1996 Free Software Foundation, Inc.
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
+ */
 /* Usage: printf format [argument...]
-
-   A front end to the printf function that lets it be used from the shell.
-
-   Backslash escapes:
-
-   \" = double quote
-   \\ = backslash
-   \a = alert (bell)
-   \b = backspace
-   \c = produce no further output
-   \f = form feed
-   \n = new line
-   \r = carriage return
-   \t = horizontal tab
-   \v = vertical tab
-   \0ooo = octal number (ooo is 0 to 3 digits)
-   \xhhh = hexadecimal number (hhh is 1 to 3 digits)
-
-   Additional directive:
-
-   %b = print an argument string, interpreting backslash escapes
-
-   The 'format' argument is re-used as many times as necessary
-   to convert all of the given arguments.
-
-   David MacKenzie <djm@gnu.ai.mit.edu>
-*/
-
-//   19990508 Busy Boxed! Dave Cinege
+ *
+ * A front end to the printf function that lets it be used from the shell.
+ *
+ * Backslash escapes:
+ *
+ * \" = double quote
+ * \\ = backslash
+ * \a = alert (bell)
+ * \b = backspace
+ * \c = produce no further output
+ * \f = form feed
+ * \n = new line
+ * \r = carriage return
+ * \t = horizontal tab
+ * \v = vertical tab
+ * \0ooo = octal number (ooo is 0 to 3 digits)
+ * \xhhh = hexadecimal number (hhh is 1 to 3 digits)
+ *
+ * Additional directive:
+ *
+ * %b = print an argument string, interpreting backslash escapes
+ *
+ * The 'format' argument is re-used as many times as necessary
+ * to convert all of the given arguments.
+ *
+ * David MacKenzie <djm@gnu.ai.mit.edu>
+ */
+/* 19990508 Busy Boxed! Dave Cinege */
+
+//config:config PRINTF
+//config:      bool "printf (3.8 kb)"
+//config:      default y
+//config:      help
+//config:      printf is used to format and print specified strings.
+//config:      It's similar to 'echo' except it has more options.
+
+//applet:IF_PRINTF(APPLET_NOFORK(printf, printf, BB_DIR_USR_BIN, BB_SUID_DROP, printf))
+
+//kbuild:lib-$(CONFIG_PRINTF) += printf.o
+//kbuild:lib-$(CONFIG_ASH_PRINTF)  += printf.o
+//kbuild:lib-$(CONFIG_HUSH_PRINTF) += printf.o
+
+//usage:#define printf_trivial_usage
+//usage:       "FORMAT [ARG]..."
+//usage:#define printf_full_usage "\n\n"
+//usage:       "Format and print ARG(s) according to FORMAT (a-la C printf)"
+//usage:
+//usage:#define printf_example_usage
+//usage:       "$ printf \"Val=%d\\n\" 5\n"
+//usage:       "Val=5\n"
 
 #include "libbb.h"
 
@@ -66,7 +87,7 @@ static int multiconvert(const char *arg, void *result, converter convert)
        errno = 0;
        convert(arg, result);
        if (errno) {
-               bb_error_msg("%s: invalid number", arg);
+               bb_error_msg("invalid number '%s'", arg);
                return 1;
        }
        return 0;
@@ -74,6 +95,12 @@ static int multiconvert(const char *arg, void *result, converter convert)
 
 static void FAST_FUNC conv_strtoull(const char *arg, void *result)
 {
+       /* Allow leading '+' - bb_strtoull() by itself does not allow it,
+        * and probably shouldn't (other callers might require purely numeric
+        * inputs to be allowed.
+        */
+       if (arg[0] == '+')
+               arg++;
        *(unsigned long long*)result = bb_strtoull(arg, NULL, 0);
        /* both coreutils 6.10 and bash 3.2:
         * $ printf '%x\n' -2
@@ -86,6 +113,8 @@ static void FAST_FUNC conv_strtoull(const char *arg, void *result)
 }
 static void FAST_FUNC conv_strtoll(const char *arg, void *result)
 {
+       if (arg[0] == '+')
+               arg++;
        *(long long*)result = bb_strtoll(arg, NULL, 0);
 }
 static void FAST_FUNC conv_strtod(const char *arg, void *result)
@@ -122,17 +151,35 @@ static double my_xstrtod(const char *arg)
        return result;
 }
 
-static void print_esc_string(char *str)
+/* Handles %b; return 1 if output is to be short-circuited by \c */
+static int print_esc_string(const char *str)
 {
-       while (*str) {
-               if (*str == '\\') {
-                       str++;
-                       bb_putchar(bb_process_escape_sequence((const char **)&str));
-               } else {
-                       bb_putchar(*str);
-                       str++;
+       char c;
+       while ((c = *str) != '\0') {
+               str++;
+               if (c == '\\') {
+                       /* %b also accepts 4-digit octals of the form \0### */
+                       if (*str == '0') {
+                               if ((unsigned char)(str[1] - '0') < 8) {
+                                       /* 2nd char is 0..7: skip leading '0' */
+                                       str++;
+                               }
+                       }
+                       else if (*str == 'c') {
+                               return 1;
+                       }
+                       {
+                               /* optimization: don't force arg to be on-stack,
+                                * use another variable for that. */
+                               const char *z = str;
+                               c = bb_process_escape_sequence(&z);
+                               str = z;
+                       }
                }
+               putchar(c);
        }
+
+       return 0;
 }
 
 static void print_direc(char *format, unsigned fmt_length,
@@ -152,13 +199,16 @@ static void print_direc(char *format, unsigned fmt_length,
        if (have_width - 1 == have_prec)
                have_width = NULL;
 
+       /* multiconvert sets errno = 0, but %s needs it cleared */
+       errno = 0;
+
        switch (format[fmt_length - 1]) {
        case 'c':
                printf(format, *argument);
                break;
        case 'd':
        case 'i':
-               llv = my_xstrtoll(argument);
+               llv = my_xstrtoll(skip_whitespace(argument));
  print_long:
                if (!have_width) {
                        if (!have_prec)
@@ -176,7 +226,7 @@ static void print_direc(char *format, unsigned fmt_length,
        case 'u':
        case 'x':
        case 'X':
-               llv = my_xstrtoull(argument);
+               llv = my_xstrtoull(skip_whitespace(argument));
                /* cheat: unsigned long and long have same width, so... */
                goto print_long;
        case 's':
@@ -228,7 +278,7 @@ static int get_width_prec(const char *str)
 {
        int v = bb_strtoi(str, NULL, 10);
        if (errno) {
-               bb_error_msg("%s: invalid number", str);
+               bb_error_msg("invalid number '%s'", str);
                v = 0;
        }
        return v;
@@ -256,12 +306,13 @@ static char **print_formatted(char *f, char **argv, int *conv_err)
                        }
                        if (*f == 'b') {
                                if (*argv) {
-                                       print_esc_string(*argv);
+                                       if (print_esc_string(*argv))
+                                               return saved_argv; /* causes main() to exit */
                                        ++argv;
                                }
                                break;
                        }
-                       if (strchr("-+ #", *f)) {
+                       if (*f && strchr("-+ #", *f)) {
                                ++f;
                                ++direc_length;
                        }
@@ -304,7 +355,7 @@ static char **print_formatted(char *f, char **argv, int *conv_err)
                                static const char format_chars[] ALIGN1 = "diouxXfeEgGcs";
                                char *p = strchr(format_chars, *f);
                                /* needed - try "printf %" without it */
-                               if (p == NULL) {
+                               if (p == NULL || *f == '\0') {
                                        bb_error_msg("%s: invalid format", direc_start);
                                        /* causes main() to exit with error */
                                        return saved_argv - 1;
@@ -342,7 +393,7 @@ static char **print_formatted(char *f, char **argv, int *conv_err)
                        f--;
                        break;
                default:
-                       bb_putchar(*f);
+                       putchar(*f);
                }
        }
 
@@ -376,10 +427,10 @@ int printf_main(int argc UNUSED_PARAM, char **argv)
        if (argv[1] && argv[1][0] == '-' && argv[1][1] == '-' && !argv[1][2])
                argv++;
        if (!argv[1]) {
-               if (ENABLE_ASH_BUILTIN_PRINTF
+               if (ENABLE_ASH_PRINTF
                 && applet_name[0] != 'p'
                ) {
-                       bb_error_msg("usage: printf FORMAT [ARGUMENT...]");
+                       bb_simple_error_msg("usage: printf FORMAT [ARGUMENT...]");
                        return 2; /* bash compat */
                }
                bb_show_usage();