diff mbox

builtin: Fix 'echo --' handling

Message ID 20161218201606.GA19607@localhost (mailing list archive)
State Rejected
Delegated to: Herbert Xu
Headers show

Commit Message

Mihail Konev Dec. 18, 2016, 8:16 p.m. UTC
Ignore first "--", as the end of options.

Breaks uses that expect 'echo' to output arguments verbatim, but
fixes those that expect 'echo --' to do so (such as 'echo -- -n').

Signed-off-by: Mihail Konev <k.mvc@ya.ru>
---
 src/bltin/printf.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)

Comments

Harald van Dijk Dec. 18, 2016, 8:28 p.m. UTC | #1
On 18/12/2016 21:16, Mihail Konev wrote:
> Ignore first "--", as the end of options.

echo -- is required by POSIX to print --. To quote:

 > The echo utility shall not recognize the "--" argument in the manner 
specified by Guideline 10 of XBD Utility Syntax Guidelines; "--" shall 
be recognized as a string operand.

Scripts wishing to print the text "-n" without anything else must find 
some other way, for example using the printf command.

Cheers,
Harald van Dijk
--
To unsubscribe from this list: send the line "unsubscribe dash" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/src/bltin/printf.c b/src/bltin/printf.c
index a626cee2c60e..1b3df313f02d 100644
--- a/src/bltin/printf.c
+++ b/src/bltin/printf.c
@@ -450,20 +450,29 @@  int
 echocmd(int argc, char **argv)
 {
 	int nonl;
+	int dashdash_met = 0;
 
-	nonl = *++argv ? equal(*argv, "-n") : 0;
+	nonl = (*++argv && !dashdash_met) ? equal(*argv, "-n") : 0;
 	argv += nonl;
 
 	do {
-		int c;
-
-		if (likely(*argv))
-			nonl += print_escape_str("%s", NULL, NULL, *argv++);
+		int c = 1;
+
+		if (likely(*argv)) {
+			if (unlikely(equal(*argv, "--") && !dashdash_met)) {
+				dashdash_met = 1;
+				c = 0;
+			} else
+				nonl += print_escape_str("%s", NULL, NULL, *argv);
+			argv++;
+		}
 		if (likely((nonl + !*argv) > 1))
 			break;
 
-		c = *argv ? ' ' : '\n';
-		out1c(c);
+		if (c) {
+			c = *argv ? ' ' : '\n';
+			out1c(c);
+		}
 	} while (*argv);
 	return 0;
 }