echo.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 source tree.
  9. *
  10. * Original copyright notice is retained at the end of this file.
  11. */
  12. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  13. *
  14. * Because of behavioral differences, implemented configurable SUSv3
  15. * or 'fancy' gnu-ish behaviors. Also, reduced size and fixed bugs.
  16. * 1) In handling '\c' escape, the previous version only suppressed the
  17. * trailing newline. SUSv3 specifies _no_ output after '\c'.
  18. * 2) SUSv3 specifies that octal escapes are of the form \0{#{#{#}}}.
  19. * The previous version did not allow 4-digit octals.
  20. */
  21. //config:config ECHO
  22. //config: bool "echo (1.8 kb)"
  23. //config: default y
  24. //config: help
  25. //config: echo prints a specified string to stdout.
  26. //config:
  27. //config:# this entry also appears in shell/Config.in, next to the echo builtin
  28. //config:config FEATURE_FANCY_ECHO
  29. //config: bool "Enable -n and -e options"
  30. //config: default y
  31. //config: depends on ECHO || ASH_ECHO || HUSH_ECHO
  32. //applet:IF_ECHO(APPLET_NOFORK(echo, echo, BB_DIR_BIN, BB_SUID_DROP, echo))
  33. //kbuild:lib-$(CONFIG_ECHO) += echo.o
  34. //kbuild:lib-$(CONFIG_ASH_ECHO) += echo.o
  35. //kbuild:lib-$(CONFIG_HUSH_ECHO) += echo.o
  36. /* BB_AUDIT SUSv3 compliant -- unless configured as fancy echo. */
  37. /* http://www.opengroup.org/onlinepubs/007904975/utilities/echo.html */
  38. //usage:#define echo_trivial_usage
  39. //usage: IF_FEATURE_FANCY_ECHO("[-neE] ") "[ARG]..."
  40. //usage:#define echo_full_usage "\n\n"
  41. //usage: "Print the specified ARGs to stdout"
  42. //usage: IF_FEATURE_FANCY_ECHO( "\n"
  43. //usage: "\n -n Suppress trailing newline"
  44. //usage: "\n -e Interpret backslash escapes (i.e., \\t=tab)"
  45. //usage: "\n -E Don't interpret backslash escapes (default)"
  46. //usage: )
  47. //usage:
  48. //usage:#define echo_example_usage
  49. //usage: "$ echo \"Erik is cool\"\n"
  50. //usage: "Erik is cool\n"
  51. //usage: IF_FEATURE_FANCY_ECHO("$ echo -e \"Erik\\nis\\ncool\"\n"
  52. //usage: "Erik\n"
  53. //usage: "is\n"
  54. //usage: "cool\n"
  55. //usage: "$ echo \"Erik\\nis\\ncool\"\n"
  56. //usage: "Erik\\nis\\ncool\n")
  57. #include "libbb.h"
  58. /* This is a NOFORK applet. Be very careful! */
  59. /* NB: can be used by shell even if not enabled as applet */
  60. /*
  61. * NB2: we don't use stdio, we need better error handing.
  62. * Examples include writing into non-opened stdout and error on write.
  63. *
  64. * With stdio, output gets shoveled into stdout buffer, and even
  65. * fflush cannot clear it out. It seems that even if libc receives
  66. * EBADF on write attempts, it feels determined to output data no matter what.
  67. * If echo is called by shell, it will try writing again later, and possibly
  68. * will clobber future output. Not good.
  69. *
  70. * Solaris has fpurge which discards buffered input. glibc has __fpurge.
  71. * But this function is not standard.
  72. */
  73. int echo_main(int argc UNUSED_PARAM, char **argv)
  74. {
  75. char **pp;
  76. const char *arg;
  77. char *out;
  78. char *buffer;
  79. unsigned buflen;
  80. #if !ENABLE_FEATURE_FANCY_ECHO
  81. enum {
  82. eflag = 0, /* 0 -- disable escape sequences */
  83. nflag = 1, /* 1 -- print '\n' */
  84. };
  85. argv++;
  86. #else
  87. char nflag = 1;
  88. char eflag = 0;
  89. while ((arg = *++argv) != NULL) {
  90. char n, e;
  91. if (arg[0] != '-')
  92. break; /* not an option arg, echo it */
  93. /* If it appears that we are handling options, then make sure
  94. * that all of the options specified are actually valid.
  95. * Otherwise, the string should just be echoed.
  96. */
  97. arg++;
  98. n = nflag;
  99. e = eflag;
  100. do {
  101. if (*arg == 'n')
  102. n = 0;
  103. else if (*arg == 'e')
  104. e = '\\';
  105. else if (*arg != 'E') {
  106. /* "-ccc" arg with one of c's invalid, echo it */
  107. /* arg consisting from just "-" also handled here */
  108. goto just_echo;
  109. }
  110. } while (*++arg);
  111. nflag = n;
  112. eflag = e;
  113. }
  114. just_echo:
  115. #endif
  116. buflen = 0;
  117. pp = argv;
  118. while ((arg = *pp) != NULL) {
  119. buflen += strlen(arg) + 1;
  120. pp++;
  121. }
  122. out = buffer = xmalloc(buflen + 1); /* +1 is needed for "no args" case */
  123. while ((arg = *argv) != NULL) {
  124. int c;
  125. if (!eflag) {
  126. /* optimization for very common case */
  127. out = stpcpy(out, arg);
  128. } else
  129. while ((c = *arg++) != '\0') {
  130. if (c == eflag) {
  131. /* This is an "\x" sequence */
  132. if (*arg == 'c') {
  133. /* "\c" means cancel newline and
  134. * ignore all subsequent chars. */
  135. goto do_write;
  136. }
  137. /* Since SUSv3 mandates a first digit of 0, 4-digit octals
  138. * of the form \0### are accepted. */
  139. if (*arg == '0') {
  140. if ((unsigned char)(arg[1] - '0') < 8) {
  141. /* 2nd char is 0..7: skip leading '0' */
  142. arg++;
  143. }
  144. }
  145. /* bb_process_escape_sequence handles NUL correctly
  146. * ("...\" case). */
  147. {
  148. /* optimization: don't force arg to be on-stack,
  149. * use another variable for that. ~30 bytes win */
  150. const char *z = arg;
  151. c = bb_process_escape_sequence(&z);
  152. arg = z;
  153. }
  154. }
  155. *out++ = c;
  156. }
  157. if (!*++argv)
  158. break;
  159. *out++ = ' ';
  160. }
  161. if (nflag) {
  162. *out++ = '\n';
  163. }
  164. do_write:
  165. /* Careful to error out on partial writes too (think ENOSPC!) */
  166. errno = 0;
  167. /*r =*/ full_write(STDOUT_FILENO, buffer, out - buffer);
  168. free(buffer);
  169. if (/*WRONG:r < 0*/ errno) {
  170. bb_perror_msg(bb_msg_write_error);
  171. return 1;
  172. }
  173. return 0;
  174. }
  175. /*
  176. * Copyright (c) 1991, 1993
  177. * The Regents of the University of California. All rights reserved.
  178. *
  179. * This code is derived from software contributed to Berkeley by
  180. * Kenneth Almquist.
  181. *
  182. * Redistribution and use in source and binary forms, with or without
  183. * modification, are permitted provided that the following conditions
  184. * are met:
  185. * 1. Redistributions of source code must retain the above copyright
  186. * notice, this list of conditions and the following disclaimer.
  187. * 2. Redistributions in binary form must reproduce the above copyright
  188. * notice, this list of conditions and the following disclaimer in the
  189. * documentation and/or other materials provided with the distribution.
  190. *
  191. * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
  192. * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
  193. *
  194. * California, Berkeley and its contributors.
  195. * 4. Neither the name of the University nor the names of its contributors
  196. * may be used to endorse or promote products derived from this software
  197. * without specific prior written permission.
  198. *
  199. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND
  200. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  201. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  202. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  203. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  204. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  205. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  206. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  207. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  208. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  209. * SUCH DAMAGE.
  210. *
  211. * @(#)echo.c 8.1 (Berkeley) 5/31/93
  212. */
  213. #ifdef VERSION_WITH_WRITEV
  214. /* We can't use stdio.
  215. * The reason for this is highly non-obvious.
  216. * echo_main is used from shell. Shell must correctly handle "echo foo"
  217. * if stdout is closed. With stdio, output gets shoveled into
  218. * stdout buffer, and even fflush cannot clear it out. It seems that
  219. * even if libc receives EBADF on write attempts, it feels determined
  220. * to output data no matter what. So it will try later,
  221. * and possibly will clobber future output. Not good.
  222. *
  223. * Using writev instead, with 'direct' conversion of argv vector.
  224. */
  225. int echo_main(int argc, char **argv)
  226. {
  227. struct iovec io[argc];
  228. struct iovec *cur_io = io;
  229. char *arg;
  230. char *p;
  231. #if !ENABLE_FEATURE_FANCY_ECHO
  232. enum {
  233. eflag = '\\',
  234. nflag = 1, /* 1 -- print '\n' */
  235. };
  236. arg = *++argv;
  237. if (!arg)
  238. goto newline_ret;
  239. #else
  240. char nflag = 1;
  241. char eflag = 0;
  242. while (1) {
  243. arg = *++argv;
  244. if (!arg)
  245. goto newline_ret;
  246. if (*arg != '-')
  247. break;
  248. /* If it appears that we are handling options, then make sure
  249. * that all of the options specified are actually valid.
  250. * Otherwise, the string should just be echoed.
  251. */
  252. p = arg + 1;
  253. if (!*p) /* A single '-', so echo it. */
  254. goto just_echo;
  255. do {
  256. if (!strchr("neE", *p))
  257. goto just_echo;
  258. } while (*++p);
  259. /* All of the options in this arg are valid, so handle them. */
  260. p = arg + 1;
  261. do {
  262. if (*p == 'n')
  263. nflag = 0;
  264. if (*p == 'e')
  265. eflag = '\\';
  266. } while (*++p);
  267. }
  268. just_echo:
  269. #endif
  270. while (1) {
  271. /* arg is already == *argv and isn't NULL */
  272. int c;
  273. cur_io->iov_base = p = arg;
  274. if (!eflag) {
  275. /* optimization for very common case */
  276. p += strlen(arg);
  277. } else while ((c = *arg++)) {
  278. if (c == eflag) {
  279. /* This is an "\x" sequence */
  280. if (*arg == 'c') {
  281. /* "\c" means cancel newline and
  282. * ignore all subsequent chars. */
  283. cur_io->iov_len = p - (char*)cur_io->iov_base;
  284. cur_io++;
  285. goto ret;
  286. }
  287. /* Since SUSv3 mandates a first digit of 0, 4-digit octals
  288. * of the form \0### are accepted. */
  289. if (*arg == '0' && (unsigned char)(arg[1] - '0') < 8) {
  290. arg++;
  291. }
  292. /* bb_process_escape_sequence can handle nul correctly */
  293. c = bb_process_escape_sequence( (void*) &arg);
  294. }
  295. *p++ = c;
  296. }
  297. arg = *++argv;
  298. if (arg)
  299. *p++ = ' ';
  300. cur_io->iov_len = p - (char*)cur_io->iov_base;
  301. cur_io++;
  302. if (!arg)
  303. break;
  304. }
  305. newline_ret:
  306. if (nflag) {
  307. cur_io->iov_base = (char*)"\n";
  308. cur_io->iov_len = 1;
  309. cur_io++;
  310. }
  311. ret:
  312. /* TODO: implement and use full_writev? */
  313. return writev(1, io, (cur_io - io)) >= 0;
  314. }
  315. #endif