Busybox echo prints error messages
authorBarret Rhoden <brho@cs.berkeley.edu>
Mon, 29 Sep 2014 03:52:04 +0000 (20:52 -0700)
committerBarret Rhoden <brho@cs.berkeley.edu>
Mon, 29 Sep 2014 03:56:43 +0000 (20:56 -0700)
It was ignoring any errors, depending on which version of echo_main was
being built.  It's a minor pain to detect errors too, due to how glibc
implements fputs, I think.  Clearing and checking errno suffices.

Now, when you echo garbage > /some/9ns/ctlfile, you'll hear about what
went wrong.

If we start building with writev, or whatever, then we may need another
patch.

You'll need to patch busybox, etc.

tools/patches/busybox/bb-echo-perror.patch [new file with mode: 0644]

diff --git a/tools/patches/busybox/bb-echo-perror.patch b/tools/patches/busybox/bb-echo-perror.patch
new file mode 100644 (file)
index 0000000..5320d4f
--- /dev/null
@@ -0,0 +1,36 @@
+--- a/coreutils/echo.c 2014-09-28 20:44:19.024670523 -0700
++++ b/coreutils/echo.c 2014-09-28 20:46:14.278666374 -0700
+@@ -31,6 +31,7 @@
+ int echo_main(int argc UNUSED_PARAM, char **argv)
+ {
++      int ret;
+       const char *arg;
+ #if !ENABLE_FEATURE_FANCY_ECHO
+       enum {
+@@ -101,7 +102,11 @@
+               if (!eflag) {
+                       /* optimization for very common case */
+-                      fputs(arg, stdout);
++                      errno = 0;
++                      fputs(arg, stdout);
++                      if (errno)
++                              perror("echo failed");
++
+               } else while ((c = *arg++)) {
+                       if (c == eflag) {       /* Check for escape seq. */
+                               if (*arg == 'c') {
+@@ -141,7 +146,11 @@
+               bb_putchar('\n');
+       }
+  ret:
+-      return fflush_all();
++      errno = 0;
++      ret = fflush_all();
++      if (errno)
++              perror("echo failed");
++      return ret;
+ }
+ /*-