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
This commit is contained in:
Denis Vlasenko
2007-04-09 13:04:50 +00:00
parent 2dfdd44d9d
commit 7e754f12d3
6 changed files with 77 additions and 50 deletions

View File

@ -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);
}
/*-