lineedit: do not hang on error, but return error indicator.
[oweals/busybox.git] / coreutils / echo.c
index 36cb6b3afc415631d16e269aa2d4d63b8254ba38..3821e594e329ca963dc2f03558ce68f6bfe70825 100644 (file)
@@ -5,7 +5,7 @@
  * Copyright (c) 1991, 1993
  *     The Regents of the University of California.  All rights reserved.
  *
- * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  *
  * Original copyright notice is retained at the end of this file.
  */
@@ -46,8 +46,11 @@ int echo_main(int argc UNUSED_PARAM, char **argv)
         * even if libc receives EBADF on write attempts, it feels determined
         * to output data no matter what. So it will try later,
         * and possibly will clobber future output. Not good. */
-       if (dup2(1, 1) != 1)
-               return -1;
+// TODO: check fcntl() & O_ACCMODE == O_WRONLY or O_RDWR?
+       if (fcntl(1, F_GETFL) == -1)
+               return 1; /* match coreutils 6.10 (sans error msg to stderr) */
+       //if (dup2(1, 1) != 1) - old way
+       //      return 1;
 
        arg = *++argv;
        if (!arg)
@@ -58,8 +61,8 @@ int echo_main(int argc UNUSED_PARAM, char **argv)
        char eflag = 0;
 
        /* We must check that stdout is not closed. */
-       if (dup2(1, 1) != 1)
-               return -1;
+       if (fcntl(1, F_GETFL) == -1)
+               return 1;
 
        while (1) {
                arg = *++argv;
@@ -138,7 +141,7 @@ int echo_main(int argc UNUSED_PARAM, char **argv)
                bb_putchar('\n');
        }
  ret:
-       return fflush(stdout);
+       return fflush_all();
 }
 
 /*-