echo.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * echo implementation for busybox
  4. *
  5. * Copyright (c) 1991, 1993
  6. * The Regents of the University of California. All rights reserved.
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  9. *
  10. * Original copyright notice is retained at the end of this file.
  11. */
  12. /* BB_AUDIT SUSv3 compliant -- unless configured as fancy echo. */
  13. /* http://www.opengroup.org/onlinepubs/007904975/utilities/echo.html */
  14. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  15. *
  16. * Because of behavioral differences, implemented configurable SUSv3
  17. * or 'fancy' gnu-ish behaviors. Also, reduced size and fixed bugs.
  18. * 1) In handling '\c' escape, the previous version only suppressed the
  19. * trailing newline. SUSv3 specifies _no_ output after '\c'.
  20. * 2) SUSv3 specifies that octal escapes are of the form \0{#{#{#}}}.
  21. * The previous version version did not allow 4-digit octals.
  22. */
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include "busybox.h"
  27. int bb_echo(int ATTRIBUTE_UNUSED argc, char **argv)
  28. {
  29. #ifndef CONFIG_FEATURE_FANCY_ECHO
  30. #define eflag '\\'
  31. ++argv;
  32. #else
  33. const char *p;
  34. int nflag = 1;
  35. int eflag = 0;
  36. while (*++argv && (**argv == '-')) {
  37. /* If it appears that we are handling options, then make sure
  38. * that all of the options specified are actually valid.
  39. * Otherwise, the string should just be echoed.
  40. */
  41. if (!*(p = *argv + 1)) { /* A single '-', so echo it. */
  42. goto just_echo;
  43. }
  44. do {
  45. if (strrchr("neE", *p) == 0) {
  46. goto just_echo;
  47. }
  48. } while (*++p);
  49. /* All of the options in this arg are valid, so handle them. */
  50. p = *argv + 1;
  51. do {
  52. if (*p == 'n') {
  53. nflag = 0;
  54. } else if (*p == 'e') {
  55. eflag = '\\';
  56. } else {
  57. eflag = 0;
  58. }
  59. } while (*++p);
  60. }
  61. just_echo:
  62. #endif
  63. while (*argv) {
  64. int c;
  65. while ((c = *(*argv)++)) {
  66. if (c == eflag) { /* Check for escape seq. */
  67. if (**argv == 'c') {
  68. /* '\c' means cancel newline and
  69. * ignore all subsequent chars. */
  70. return 0;
  71. }
  72. #ifndef CONFIG_FEATURE_FANCY_ECHO
  73. /* SUSv3 specifies that octal escapes must begin with '0'. */
  74. if (((unsigned int)(**argv - '1')) >= 7)
  75. #endif
  76. {
  77. /* Since SUSv3 mandates a first digit of 0, 4-digit octals
  78. * of the form \0### are accepted. */
  79. if ((**argv == '0') && (((unsigned int)(argv[0][1] - '0')) < 8)) {
  80. (*argv)++;
  81. }
  82. /* bb_process_escape_sequence can handle nul correctly */
  83. c = bb_process_escape_sequence((const char **) argv);
  84. }
  85. }
  86. putchar(c);
  87. }
  88. if (*++argv) {
  89. putchar(' ');
  90. }
  91. }
  92. #ifdef CONFIG_FEATURE_FANCY_ECHO
  93. if (nflag) {
  94. putchar('\n');
  95. }
  96. #else
  97. putchar('\n');
  98. #endif
  99. return 0;
  100. }
  101. int echo_main(int argc, char** argv)
  102. {
  103. (void)bb_echo(argc, argv);
  104. fflush_stdout_and_exit(EXIT_SUCCESS);
  105. }
  106. /*-
  107. * Copyright (c) 1991, 1993
  108. * The Regents of the University of California. All rights reserved.
  109. *
  110. * This code is derived from software contributed to Berkeley by
  111. * Kenneth Almquist.
  112. *
  113. * Redistribution and use in source and binary forms, with or without
  114. * modification, are permitted provided that the following conditions
  115. * are met:
  116. * 1. Redistributions of source code must retain the above copyright
  117. * notice, this list of conditions and the following disclaimer.
  118. * 2. Redistributions in binary form must reproduce the above copyright
  119. * notice, this list of conditions and the following disclaimer in the
  120. * documentation and/or other materials provided with the distribution.
  121. *
  122. * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
  123. * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
  124. *
  125. * California, Berkeley and its contributors.
  126. * 4. Neither the name of the University nor the names of its contributors
  127. * may be used to endorse or promote products derived from this software
  128. * without specific prior written permission.
  129. *
  130. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  131. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  132. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  133. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  134. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  135. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  136. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  137. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  138. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  139. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  140. * SUCH DAMAGE.
  141. *
  142. * @(#)echo.c 8.1 (Berkeley) 5/31/93
  143. */