Index: gcc/fortran/error.c
===================================================================
--- gcc/fortran/error.c	(revision 199530)
+++ gcc/fortran/error.c	(working copy)
@@ -30,6 +30,13 @@ along with GCC; see the file COPYING3.  If not see
 #include "flags.h"
 #include "gfortran.h"
 
+#if !(defined (_WIN32) || defined (VMS) || defined (__vxworks) || \
+      defined (__Lynx__) || defined (__ANDROID__))
+/* UNIX-like systems */
+#include <sys/ioctl.h>
+#endif
+
+
 static int suppress_errors = 0;
 
 static int warnings_not_errors = 0; 
@@ -59,9 +66,24 @@ gfc_pop_suppress_errors (void)
 }
 
 
+/* Determine terminal width (for trimming source lines in output).  */
+
 static int
 get_terminal_width (void)
 {
+  /* Only limit the width if we're outputting to a terminal.  */
+  if (!isatty (STDERR_FILENO))
+    return INT_MAX;
+  
+  /* Method #1: Use ioctl (not available on all systems).  */
+#ifdef TIOCGWINSZ
+  struct winsize w;
+  ioctl (0, TIOCGWINSZ, &w);
+  if (w.ws_col > 0)
+    return w.ws_col;
+#endif
+
+  /* Method #2: Query environment variable $COLUMNS.  */
   const char *p = getenv ("COLUMNS");
   if (p)
     {
@@ -69,7 +91,8 @@ get_terminal_width (void)
       if (value > 0)
 	return value;
     }
-  /* Use a reasonable default.  */
+
+  /* If both fail, use reasonable default.  */
   return 80;
 }
 
