*: do not assign to stdout/stderr, it's not portable.
authorDenys Vlasenko <vda.linux@googlemail.com>
Sat, 2 Jan 2010 17:49:22 +0000 (18:49 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sat, 2 Jan 2010 17:49:22 +0000 (18:49 +0100)
Based on patch by Aaron Carroll <xaaronc@gmail.com>

function                                             old     new   delta
time_main                                           1062    1052     -10
cpio_main                                            563     549     -14

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
archival/cpio.c
libbb/xfuncs_printf.c
miscutils/time.c

index 41aeef1715edc24e43bbd457f42b93e520dc4965..f139f313046661e60dc4a36e9e4f5a539281d153 100644 (file)
@@ -354,10 +354,7 @@ int cpio_main(int argc UNUSED_PARAM, char **argv)
                if (*cpio_fmt != 'n') /* we _require_ "-H newc" */
                        bb_show_usage();
                if (opt & CPIO_OPT_FILE) {
-                       fclose(stdout);
-                       stdout = fopen_for_write(cpio_filename);
-                       /* Paranoia: I don't trust libc that much */
-                       xdup2(fileno(stdout), STDOUT_FILENO);
+                       xmove_fd(xopen3(cpio_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666), STDOUT_FILENO);
                }
  dump:
                return cpio_o();
index d362841318d2fb385d1caf5d1cc3cbb2d0c59cd6..7207ec58a45eb7577e8444b88bf6871ca4e6f23e 100644 (file)
@@ -263,10 +263,7 @@ int FAST_FUNC fflush_all(void)
 
 int FAST_FUNC bb_putchar(int ch)
 {
-       /* time.c needs putc(ch, stdout), not putchar(ch).
-        * it does "stdout = stderr;", but then glibc's putchar()
-        * doesn't work as expected. bad glibc, bad */
-       return putc(ch, stdout);
+       return putchar(ch);
 }
 
 /* Die with an error message if we can't copy an entire FILE* to stdout,
index 42c812a422fc1062fdd72a653c2b0993615ce775..3421736093235cea839691a8a61e5c5f8d8682a5 100644 (file)
@@ -414,9 +414,7 @@ int time_main(int argc UNUSED_PARAM, char **argv)
        run_command(argv, &res);
 
        /* Cheat. printf's are shorter :) */
-       /* (but see bb_putchar() body for additional wrinkle!) */
-       xdup2(2, 1); /* just in case libc does something silly :( */
-       stdout = stderr;
+       xdup2(STDERR_FILENO, STDOUT_FILENO);
        summarize(output_format, argv, &res);
 
        if (WIFSTOPPED(res.waitstatus))