1 /* vi: set sw=4 ts=4: */
3 * echo implementation for busybox
5 * Copyright (c) 1991, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 * Original copyright notice is retained at the end of this file.
13 /* BB_AUDIT SUSv3 compliant -- unless configured as fancy echo. */
14 /* http://www.opengroup.org/onlinepubs/007904975/utilities/echo.html */
16 /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
18 * Because of behavioral differences, implemented configurable SUSv3
19 * or 'fancy' gnu-ish behaviors. Also, reduced size and fixed bugs.
20 * 1) In handling '\c' escape, the previous version only suppressed the
21 * trailing newline. SUSv3 specifies _no_ output after '\c'.
22 * 2) SUSv3 specifies that octal escapes are of the form \0{#{#{#}}}.
23 * The previous version did not allow 4-digit octals.
28 /* This is a NOFORK applet. Be very careful! */
30 /* NB: can be used by shell even if not enabled as applet */
32 int echo_main(int argc UNUSED_PARAM, char **argv)
35 #if !ENABLE_FEATURE_FANCY_ECHO
38 nflag = 1, /* 1 -- print '\n' */
41 /* We must check that stdout is not closed.
42 * The reason for this is highly non-obvious.
43 * echo_main is used from shell. Shell must correctly handle "echo foo"
44 * if stdout is closed. With stdio, output gets shoveled into
45 * stdout buffer, and even fflush cannot clear it out. It seems that
46 * even if libc receives EBADF on write attempts, it feels determined
47 * to output data no matter what. So it will try later,
48 * and possibly will clobber future output. Not good. */
49 // TODO: check fcntl() & O_ACCMODE == O_WRONLY or O_RDWR?
50 if (fcntl(1, F_GETFL) == -1)
51 return 1; /* match coreutils 6.10 (sans error msg to stderr) */
52 //if (dup2(1, 1) != 1) - old way
63 /* We must check that stdout is not closed. */
64 if (fcntl(1, F_GETFL) == -1)
74 /* If it appears that we are handling options, then make sure
75 * that all of the options specified are actually valid.
76 * Otherwise, the string should just be echoed.
79 if (!*p) /* A single '-', so echo it. */
83 if (!strrchr("neE", *p))
87 /* All of the options in this arg are valid, so handle them. */
99 /* arg is already == *argv and isn't NULL */
103 /* optimization for very common case */
105 } else while ((c = *arg++)) {
106 if (c == eflag) { /* Check for escape seq. */
108 /* '\c' means cancel newline and
109 * ignore all subsequent chars. */
112 #if !ENABLE_FEATURE_FANCY_ECHO
113 /* SUSv3 specifies that octal escapes must begin with '0'. */
114 if ( ((int)(unsigned char)(*arg) - '0') >= 8) /* '8' or bigger */
117 /* Since SUSv3 mandates a first digit of 0, 4-digit octals
118 * of the form \0### are accepted. */
120 /* NB: don't turn "...\0" into "...\" */
121 if (arg[1] && ((unsigned char)(arg[1]) - '0') < 8) {
125 /* bb_process_escape_sequence handles NUL correctly
127 c = bb_process_escape_sequence(&arg);
144 return fflush(stdout);
148 * Copyright (c) 1991, 1993
149 * The Regents of the University of California. All rights reserved.
151 * This code is derived from software contributed to Berkeley by
154 * Redistribution and use in source and binary forms, with or without
155 * modification, are permitted provided that the following conditions
157 * 1. Redistributions of source code must retain the above copyright
158 * notice, this list of conditions and the following disclaimer.
159 * 2. Redistributions in binary form must reproduce the above copyright
160 * notice, this list of conditions and the following disclaimer in the
161 * documentation and/or other materials provided with the distribution.
163 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
164 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
166 * California, Berkeley and its contributors.
167 * 4. Neither the name of the University nor the names of its contributors
168 * may be used to endorse or promote products derived from this software
169 * without specific prior written permission.
171 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
172 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
173 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
174 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
175 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
176 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
177 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
178 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
179 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
180 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
183 * @(#)echo.c 8.1 (Berkeley) 5/31/93
186 #ifdef VERSION_WITH_WRITEV
187 /* We can't use stdio.
188 * The reason for this is highly non-obvious.
189 * echo_main is used from shell. Shell must correctly handle "echo foo"
190 * if stdout is closed. With stdio, output gets shoveled into
191 * stdout buffer, and even fflush cannot clear it out. It seems that
192 * even if libc receives EBADF on write attempts, it feels determined
193 * to output data no matter what. So it will try later,
194 * and possibly will clobber future output. Not good.
196 * Using writev instead, with 'direct' conversion of argv vector.
199 int echo_main(int argc, char **argv)
201 struct iovec io[argc];
202 struct iovec *cur_io = io;
205 #if !ENABLE_FEATURE_FANCY_ECHO
208 nflag = 1, /* 1 -- print '\n' */
224 /* If it appears that we are handling options, then make sure
225 * that all of the options specified are actually valid.
226 * Otherwise, the string should just be echoed.
229 if (!*p) /* A single '-', so echo it. */
233 if (!strrchr("neE", *p))
237 /* All of the options in this arg are valid, so handle them. */
250 /* arg is already == *argv and isn't NULL */
253 cur_io->iov_base = p = arg;
256 /* optimization for very common case */
258 } else while ((c = *arg++)) {
259 if (c == eflag) { /* Check for escape seq. */
261 /* '\c' means cancel newline and
262 * ignore all subsequent chars. */
263 cur_io->iov_len = p - (char*)cur_io->iov_base;
267 #if !ENABLE_FEATURE_FANCY_ECHO
268 /* SUSv3 specifies that octal escapes must begin with '0'. */
269 if ( (((unsigned char)*arg) - '1') >= 7)
272 /* Since SUSv3 mandates a first digit of 0, 4-digit octals
273 * of the form \0### are accepted. */
274 if (*arg == '0' && ((unsigned char)(arg[1]) - '0') < 8) {
277 /* bb_process_escape_sequence can handle nul correctly */
278 c = bb_process_escape_sequence( (void*) &arg);
287 cur_io->iov_len = p - (char*)cur_io->iov_base;
295 cur_io->iov_base = (char*)"\n";
300 /* TODO: implement and use full_writev? */
301 return writev(1, io, (cur_io - io)) >= 0;