[PATCH 08/12] echo: add -e option support

Sascha Hauer s.hauer at pengutronix.de
Mon Mar 29 05:36:19 EDT 2010


Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 commands/echo.c |   52 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 51 insertions(+), 1 deletions(-)

diff --git a/commands/echo.c b/commands/echo.c
index d5640a0..6f4f136 100644
--- a/commands/echo.c
+++ b/commands/echo.c
@@ -26,12 +26,59 @@
 #include <fcntl.h>
 #include <errno.h>
 
+static int my_fputs(int fd, const char *s)
+{
+	int c;
+
+	while (*s) {
+		if (*s == '\\') {
+			switch (*(s + 1)) {
+			case 0:
+				return 0;
+			case '\\':
+				c = '\\';
+				break;
+			case 'a':
+				c = '\a';
+				break;
+			case 'b':
+				c = '\b';
+				break;
+			case 'n':
+				c = '\n';
+				break;
+			case 'r':
+				c = '\r';
+				break;
+			case 't':
+				c = '\t';
+				break;
+			case 'f':
+				c = '\f';
+				break;
+			case 'e':
+				c = 0x1b;
+				break;
+			default:
+				fputc(fd, '\\');
+				c = *(s + 1);
+			}
+			s++;
+			fputc(fd, c);
+		} else
+			fputc(fd, *s);
+		s++;
+	}
+	return 0;
+}
+
 static int do_echo(struct command *cmdtp, int argc, char *argv[])
 {
 	int i, optind = 1;
 	int fd = stdout, opt, newline = 1;
 	char *file = NULL;
 	int oflags = O_WRONLY | O_CREAT;
+	int (*fputsfunc)(int, const char *) = fputs;
 
 	/* We can't use getopt() here because we want to
 	 * echo all things we don't understand.
@@ -62,6 +109,9 @@ static int do_echo(struct command *cmdtp, int argc, char *argv[])
 				goto no_optarg_out;
 			optind++;
 			break;
+		case 'e':
+			fputsfunc = my_fputs;
+			break;
 		default:
 			goto exit_parse;
 		}
@@ -80,7 +130,7 @@ exit_parse:
 	for (i = optind; i < argc; i++) {
 		if (i > optind)
 			fputc(fd, ' ');
-		fputs(fd, argv[i]);
+		fputsfunc(fd, argv[i]);
 	}
 
 	if (newline)
-- 
1.7.0




More information about the barebox mailing list