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