OSDN Git Service

libcom_err: Only output ^M when tty is in raw mode
authorTheodore Ts'o <tytso@mit.edu>
Sat, 15 May 2010 11:48:25 +0000 (07:48 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Sat, 15 May 2010 11:48:25 +0000 (07:48 -0400)
This fixes a long-standing botch in the com_err library, and solves a
regression test problem for libss that gets tickled by source code
management systems (like Perforce) that don't preserve CRLF line
endings with fidelity.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
lib/et/com_err.c
lib/ss/test_script_expected

index d83d6a9..e5af13c 100644 (file)
  */
 
 #include <stdio.h>
+#ifdef HAVE_TERMIOS_H
+#include <termios.h>
+#endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
 #include "com_err.h"
 #include "error_table.h"
 #include "internal.h"
@@ -25,6 +31,8 @@ static void
 default_com_err_proc (const char *whoami, errcode_t code, const
                      char *fmt, va_list args)
 {
+    int do_cr = 1, fd = fileno(stderr);
+
     if (whoami) {
        fputs(whoami, stderr);
        fputs(": ", stderr);
@@ -36,8 +44,20 @@ default_com_err_proc (const char *whoami, errcode_t code, const
     if (fmt) {
         vfprintf (stderr, fmt, args);
     }
-    /* should output \r only if using a tty in raw mode */
-    fputs("\r\n", stderr);
+    if (!isatty(fd))
+       do_cr = 0;
+#ifdef HAVE_TERMIOS_H
+    else {
+       struct termios t;
+
+       if ((tcgetattr(fd, &t)) == 0 &&
+           (t.c_oflag & OPOST) && (t.c_oflag & ONLCR))
+       do_cr = 0;
+    }
+#endif
+    if (do_cr)
+       fputc('\r', stderr);
+    fputc('\n', stderr);
     fflush(stderr);
 }
 
index 3f61d9a..543f828 100644 (file)
@@ -10,9 +10,9 @@ test_icount: test bar quux
 Hello, world!
 Args: 'bar', 'quux'
 test_icount: quux bar
-test_ss: Command not found quux\r
+test_ss: Command not found quux
 test_icount: quux
-test_ss: Command not found quux\r
+test_ss: Command not found quux
 test_icount: test quux
 Hello, world!
 Args: 'quux'