dash.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] builtin: Fix 'echo --' handling
@ 2016-12-18 20:16 Mihail Konev
  2016-12-18 20:28 ` Harald van Dijk
  0 siblings, 1 reply; 2+ messages in thread
From: Mihail Konev @ 2016-12-18 20:16 UTC (permalink / raw)
  To: dash

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(-)

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;
 }
-- 
2.9.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] builtin: Fix 'echo --' handling
  2016-12-18 20:16 [PATCH] builtin: Fix 'echo --' handling Mihail Konev
@ 2016-12-18 20:28 ` Harald van Dijk
  0 siblings, 0 replies; 2+ messages in thread
From: Harald van Dijk @ 2016-12-18 20:28 UTC (permalink / raw)
  To: Mihail Konev, dash

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-12-18 20:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-18 20:16 [PATCH] builtin: Fix 'echo --' handling Mihail Konev
2016-12-18 20:28 ` Harald van Dijk

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).