* Original copyright notice is retained at the end of this file.
*/
-#include "internal.h"
#include <stdio.h>
-
-static const char uname_usage[] =
- "echo [-neE] [ARG ...]\n\n"
- "Prints 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";
+#include <string.h>
+#include <stdlib.h>
+#include "busybox.h"
extern int
echo_main(int argc, char** argv)
{
- register char **ap;
- register char *p;
- register char c;
int nflag = 0;
int eflag = 0;
- ap = argv;
- if (argc)
- ap++;
- while ((p = *ap) != NULL && *p == '-') {
- if (strcmp(p, "-n")==0) {
- nflag = 1;
- } else if (strcmp(p, "-e")==0) {
- eflag = 1;
- } else if (strcmp(p, "-E")==0) {
- 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;
}
- else if (strncmp(p, "--", 2)==0) {
- usage( uname_usage);
- }
- else break;
- ap++;
+
+ if (!*temp)
+ goto just_echo;
+
+ /*
+ * 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++;
}
- while ((p = *ap++) != NULL) {
- while ((c = *p++) != '\0') {
- if (c == '\\' && eflag) {
- switch (c = *p++) {
- case 'a': c = '\007'; break;
- case 'b': c = '\b'; break;
- case 'c': exit( 0); /* exit */
- case 'f': c = '\f'; break;
- case 'n': c = '\n'; break;
- case 'r': c = '\r'; break;
- case 't': c = '\t'; break;
- case 'v': c = '\v'; break;
- case '\\': break; /* c = '\\' */
- case '0': case '1': case '2': case '3':
- case '4': case '5': case '6': case '7':
- c -= '0';
- if (*p >= '0' && *p <= '7')
- c = c * 8 + (*p++ - '0');
- if (*p >= '0' && *p <= '7')
- c = c * 8 + (*p++ - '0');
- break;
- default:
- p--;
- break;
+
+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);
- exit( 0);
+
+ return EXIT_SUCCESS;
}
/*-
* 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. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
+ * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
+ *
* 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
*
* @(#)echo.c 8.1 (Berkeley) 5/31/93
*/
-
-