From 56f24f08d76e1fdd965ea0fe08a2ce51cd9a7018 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Mon, 18 May 2020 19:34:53 +0200 Subject: [PATCH] printf: Don't print an error if not given an argument --- CHANGELOG.md | 1 + doc_src/cmds/printf.rst | 4 +++- src/builtin_printf.cpp | 1 - tests/checks/printf.fish | 5 +++++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05aa65556..84ad3119f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ - Range limits in index range expansions like `$x[$start..$end]` may be omitted: `$start` and `$end` default to 1 and -1 (the last item) respectively. - `string sub` has a new `--end` option to specify the end index of a substring (#6765). - `string split` has a new `--fields` option to specify fields to output, similar to `cut -f` (#6770). +- `printf` no longer prints an error if not given an argument (not even a format string) ### Interactive improvements diff --git a/doc_src/cmds/printf.rst b/doc_src/cmds/printf.rst index ca56c4bb3..1fab5ff4a 100644 --- a/doc_src/cmds/printf.rst +++ b/doc_src/cmds/printf.rst @@ -59,7 +59,9 @@ printf also knows a number of backslash escapes: - ``\uhhhh`` 16-bit Unicode character (hhhh is 4 digits) - ``\Uhhhhhhhh`` 32-bit Unicode character (hhhhhhhh is 8 digits) -The ``format`` argument is re-used as many times as necessary to convert all of the given arguments. If a format specifier is not appropriate for the given argument, an error is printed. For example, ``printf '%d' "102.234"`` produces an error, as "102.234" cannot be formatted as an integer. +The ``format`` argument is re-used as many times as necessary to convert all of the given arguments. If the given argument doesn't work for the given format, an error is printed. For example, ``printf '%d' "102.234"`` produces an error, as "102.234" cannot be formatted as an integer. printf will then also return non-zero. + +It will also return non-zero if no argument at all was given, in which case it will print nothing. This file has been imported from the printf in GNU Coreutils version 6.9. If you would like to use a newer version of printf, for example the one shipped with your OS, try ``command printf``. diff --git a/src/builtin_printf.cpp b/src/builtin_printf.cpp index 662d24a58..b57896712 100644 --- a/src/builtin_printf.cpp +++ b/src/builtin_printf.cpp @@ -749,7 +749,6 @@ int builtin_printf(parser_t &parser, io_streams_t &streams, wchar_t **argv) { argc -= optind; argv += optind; if (argc < 1) { - streams.err.append_format(BUILTIN_ERR_MIN_ARG_COUNT1, cmd, 1, argc); return STATUS_INVALID_ARGS; } diff --git a/tests/checks/printf.fish b/tests/checks/printf.fish index 4b0fec975..f342bee7d 100644 --- a/tests/checks/printf.fish +++ b/tests/checks/printf.fish @@ -111,6 +111,11 @@ printf 'long signed %d\n' -498216206595 printf 'long signed to unsigned %u\n' -498216206596 # CHECK: long signed to unsigned 18446743575493345020 +# Just check that we print no error for no arguments +printf +echo $status +# CHECK: 2 + # Verify numeric conversion still happens even if it couldn't be fully converted printf '%d\n' 15.1 # CHECK: 15