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>
*/
#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"
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);
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);
}
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'