From: Theodore Ts'o Date: Sat, 15 May 2010 11:48:25 +0000 (-0400) Subject: libcom_err: Only output ^M when tty is in raw mode X-Git-Tag: v1.41.12~10 X-Git-Url: https://git.whamcloud.com/?a=commitdiff_plain;h=822c10e84e5f57394bcad921adc560f5b032c03e;p=tools%2Fe2fsprogs.git libcom_err: Only output ^M when tty is in raw mode 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" --- diff --git a/lib/et/com_err.c b/lib/et/com_err.c index d83d6a9..e5af13c 100644 --- a/lib/et/com_err.c +++ b/lib/et/com_err.c @@ -12,6 +12,12 @@ */ #include +#ifdef HAVE_TERMIOS_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#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); } diff --git a/lib/ss/test_script_expected b/lib/ss/test_script_expected index 3f61d9a..543f828 100644 --- a/lib/ss/test_script_expected +++ b/lib/ss/test_script_expected @@ -10,9 +10,9 @@ test_icount: test bar quux Hello, world! Args: 'bar', 'quux' test_icount: quux bar -test_ss: Command not found quux +test_ss: Command not found quux test_icount: quux -test_ss: Command not found quux +test_ss: Command not found quux test_icount: test quux Hello, world! Args: 'quux'