/* 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"
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;
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
}
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)
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,
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)
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':
{
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;
}
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;
}
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;
f--;
break;
default:
- bb_putchar(*f);
+ putchar(*f);
}
}
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();