X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=echo.c;h=31c0315289790aad1840a324e048d494302186be;hb=4391a16c92cef744fc715cb7347b7b71fc8f312e;hp=387ea3fefc5673427a194051800085dd0e78f8f2;hpb=add09fd558c8a336554fbf8b381ab0f8e180382a;p=oweals%2Fbusybox.git diff --git a/echo.c b/echo.c index 387ea3fef..31c031528 100644 --- a/echo.c +++ b/echo.c @@ -22,64 +22,94 @@ * Original copyright notice is retained at the end of this file. */ -#include "internal.h" #include - -static const char uname_usage[] = - "echo [-neE] [ARG ...]\n" -#ifndef BB_FEATURE_TRIVIAL_HELP - "\nPrints the specified ARGs to stdout\n\n" - "Options:\n" - "\t-n\tsuppress trailing newline\n" - "\t-e\tinterpret backslash-escaped characters (i.e. \\t=tab etc)\n" - "\t-E\tdisable interpretation of backslash-escaped characters\n" -#endif - ; +#include +#include +#include "busybox.h" extern int echo_main(int argc, char** argv) { - register char **ap; - char *p; - register char c; int nflag = 0; int eflag = 0; + /* Skip argv[0]. */ + argc--; + argv++; + + while (argc > 0 && *argv[0] == '-') + { + register char *temp; + register int ix; + + /* + * 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. + */ + temp = argv[0] + 1; + + for (ix = 0; temp[ix]; ix++) + { + if (strrchr("neE", temp[ix]) == 0) + goto just_echo; + } + + if (!*temp) + goto just_echo; - while ((c = getopt(argc, argv, "neE")) != EOF) { - switch (c) { - case 'n': - nflag = 1; - break; - case 'e': - eflag = 1; - break; - case 'E': - eflag = 0; - break; - default: - usage(uname_usage); + /* + * All of the options in temp are valid options to echo. + * Handle them. + */ + while (*temp) + { + if (*temp == 'n') + nflag = 1; + else if (*temp == 'e') + eflag = 1; + else if (*temp == 'E') + eflag = 0; + else + goto just_echo; + + temp++; } + argc--; + argv++; } - ap = &argv[optind]; - while ((p = *ap++) != NULL) { - while ((c = *p++) != '\0') { - if (c == '\\' && eflag) { - if (*p == 'c') - exit(0); - else - c = process_escape_sequence(&p); +just_echo: + while (argc > 0) { + const char *arg = argv[0]; + register int c; + + while ((c = *arg++)) { + + /* Check for escape sequence. */ + if (c == '\\' && eflag && *arg) { + if (*arg == 'c') { + /* '\c' means cancel newline. */ + nflag = 1; + arg++; + continue; + } else { + c = process_escape_sequence(&arg); + } } + putchar(c); } - if (*ap) + argc--; + argv++; + if (argc > 0) putchar(' '); } - if (! nflag) + if (!nflag) putchar('\n'); fflush(stdout); - return( 0); + + return EXIT_SUCCESS; } /*- @@ -97,9 +127,10 @@ echo_main(int argc, char** argv) * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of + * + * 3. + * * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software @@ -119,5 +150,3 @@ echo_main(int argc, char** argv) * * @(#)echo.c 8.1 (Berkeley) 5/31/93 */ - -