From 7e754f12d304704d44e10fd4d2fdb8710526656e Mon Sep 17 00:00:00 2001
From: Denis Vlasenko <vda.linux@googlemail.com>
Date: Mon, 9 Apr 2007 13:04:50 +0000
Subject: Implement first instance of NOFORK applet - echo find: use
 NOFORK/NOEXEC; small -exec buglet also eliminated vfork_daemon_rexec: honor
 PREFER_APPLETS echo: small size improvements

find -exec echo {} \; with PREFER_APPLETS=y runs 4 times faster
---
 coreutils/echo.c | 75 ++++++++++++++++++++++++++++----------------------------
 1 file changed, 38 insertions(+), 37 deletions(-)

(limited to 'coreutils/echo.c')

diff --git a/coreutils/echo.c b/coreutils/echo.c
index 8270fc006..486245508 100644
--- a/coreutils/echo.c
+++ b/coreutils/echo.c
@@ -20,86 +20,85 @@
  * 1) In handling '\c' escape, the previous version only suppressed the
  *     trailing newline.  SUSv3 specifies _no_ output after '\c'.
  * 2) SUSv3 specifies that octal escapes are of the form \0{#{#{#}}}.
- *    The previous version version did not allow 4-digit octals.
+ *    The previous version did not allow 4-digit octals.
  */
 
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
 #include "busybox.h"
 
 int bb_echo(char **argv)
 {
-#ifndef CONFIG_FEATURE_FANCY_ECHO
+	const char *arg;
+#if !ENABLE_FEATURE_FANCY_ECHO
 #define eflag '\\'
 	++argv;
 #else
 	const char *p;
-	int nflag = 1;
-	int eflag = 0;
+	char nflag = 1;
+	char eflag = 0;
+
+	while (1) {
+		arg = *++argv;
+		if (!arg)
+			goto ret;
+		if (*arg != '-')
+			break;
 
-	while (*++argv && (**argv == '-')) {
 		/* If it appears that we are handling options, then make sure
 		 * that all of the options specified are actually valid.
 		 * Otherwise, the string should just be echoed.
 		 */
-
-		if (!*(p = *argv + 1)) {	/* A single '-', so echo it. */
+		p = arg + 1;
+		if (!*p)	/* A single '-', so echo it. */
 			goto just_echo;
-		}
 
 		do {
-			if (strrchr("neE", *p) == 0) {
+			if (!strrchr("neE", *p))
 				goto just_echo;
-			}
 		} while (*++p);
 
 		/* All of the options in this arg are valid, so handle them. */
-		p = *argv + 1;
+		p = arg + 1;
 		do {
-			if (*p == 'n') {
+			if (*p == 'n')
 				nflag = 0;
-			} else if (*p == 'e') {
+			if (*p == 'e')
 				eflag = '\\';
-			} else {
-				eflag = 0;
-			}
 		} while (*++p);
 	}
-
-just_echo:
+ just_echo:
 #endif
-	while (*argv) {
+	while (1) {
+		/* arg is already = *argv and isn't NULL */
 		int c;
 
-		while ((c = *(*argv)++)) {
+		while ((c = *arg++)) {
 			if (c == eflag) {	/* Check for escape seq. */
-				if (**argv == 'c') {
+				if (*arg == 'c') {
 					/* '\c' means cancel newline and
 					 * ignore all subsequent chars. */
-					return 0;
+					goto ret;
 				}
-#ifndef CONFIG_FEATURE_FANCY_ECHO
+#if !ENABLE_FEATURE_FANCY_ECHO
 				/* SUSv3 specifies that octal escapes must begin with '0'. */
-				if (((unsigned int)(**argv - '1')) >= 7)
+				if ( (((unsigned char)*arg) - '1') >= 7)
 #endif
 				{
 					/* Since SUSv3 mandates a first digit of 0, 4-digit octals
 					* of the form \0### are accepted. */
-					if ((**argv == '0') && (((unsigned int)(argv[0][1] - '0')) < 8)) {
-						(*argv)++;
+					if (*arg == '0' && ((unsigned char)(arg[1]) - '0') < 8) {
+						arg++;
 					}
 					/* bb_process_escape_sequence can handle nul correctly */
-					c = bb_process_escape_sequence((const char **) argv);
+					c = bb_process_escape_sequence(&arg);
 				}
 			}
 			putchar(c);
 		}
 
-		if (*++argv) {
-			putchar(' ');
-		}
+		arg = *++argv;
+		if (!arg)
+			break;
+		putchar(' ');
 	}
 
 #ifdef CONFIG_FEATURE_FANCY_ECHO
@@ -109,14 +108,16 @@ just_echo:
 #else
 	putchar('\n');
 #endif
-	return 0;
+ ret:
+	return fflush(stdout);
 }
 
+/* This is a NOFORK applet. Be very careful! */
+
 int echo_main(int argc, char** argv);
 int echo_main(int argc, char** argv)
 {
-	(void)bb_echo(argv);
-	fflush_stdout_and_exit(EXIT_SUCCESS);
+	return bb_echo(argv);
 }
 
 /*-
-- 
cgit v1.2.3-55-g6feb