diff -uNr gcc-4061.orig/libgfortran/io/io.h gcc-4061/libgfortran/io/io.h --- gcc-4061.orig/libgfortran/io/io.h 2004-09-16 08:46:26.000000000 +0900 +++ gcc-4061/libgfortran/io/io.h 2005-06-22 16:02:58.000000000 +0900 @@ -411,6 +411,9 @@ #define output_stream prefix(output_stream) stream *output_stream (void); +extern stream *error_stream (void); +internal_proto(error_stream); + #define compare_file_filename prefix(compare_file_filename) int compare_file_filename (stream *, const char *, int); diff -uNr gcc-4061.orig/libgfortran/io/open.c gcc-4061/libgfortran/io/open.c --- gcc-4061.orig/libgfortran/io/open.c 2004-09-16 08:46:26.000000000 +0900 +++ gcc-4061/libgfortran/io/open.c 2005-06-22 16:17:00.000000000 +0900 @@ -362,9 +362,14 @@ internal_error ("new_unit(): Bad status"); } - /* Make sure the file isn't already open someplace else. */ + /* Make sure the file isn't already open someplace else. + Do not error if opening file preconnected to stdin, stdout, stderr. */ - if (find_file () != NULL) + u = find_file (); + if (u != NULL + && (options.stdin_unit < 0 || u->unit_number != options.stdin_unit) + && (options.stdout_unit < 0 || u->unit_number != options.stdout_unit) + && (options.stderr_unit < 0 || u->unit_number != options.stderr_unit)) { generate_error (ERROR_ALREADY_OPEN, NULL); goto cleanup; diff -uNr gcc-4061.orig/libgfortran/io/unit.c gcc-4061/libgfortran/io/unit.c --- gcc-4061.orig/libgfortran/io/unit.c 2004-09-16 08:46:26.000000000 +0900 +++ gcc-4061/libgfortran/io/unit.c 2005-06-22 16:04:59.000000000 +0900 @@ -325,6 +325,28 @@ insert_unit (u); } + if (options.stderr_unit >= 0) + { /* STDERR */ + u = get_mem (sizeof (gfc_unit)); + memset (u, '\0', sizeof (gfc_unit)); + + u->unit_number = options.stderr_unit; + u->s = error_stream (); + + u->flags.action = ACTION_WRITE; + + u->flags.access = ACCESS_SEQUENTIAL; + u->flags.form = FORM_FORMATTED; + u->flags.status = STATUS_OLD; + u->flags.blank = BLANK_ZERO; + u->flags.position = POSITION_ASIS; + + u->recl = options.default_recl; + u->endfile = AT_ENDFILE; + + insert_unit (u); + } + /* Calculate the maximum file offset in a portable manner. * max will be the largest signed number for the type gfc_offset. * diff -uNr gcc-4061.orig/libgfortran/io/unix.c gcc-4061/libgfortran/io/unix.c --- gcc-4061.orig/libgfortran/io/unix.c 2004-11-11 10:16:32.000000000 +0900 +++ gcc-4061/libgfortran/io/unix.c 2005-06-22 15:58:41.000000000 +0900 @@ -870,7 +870,7 @@ * around it. */ static stream * -fd_to_stream (int fd, int prot) +fd_to_stream (int fd, int prot, int avoid_mmap) { struct stat statbuf; unix_stream *s; @@ -889,7 +889,10 @@ s->file_length = S_ISREG (statbuf.st_mode) ? statbuf.st_size : -1; #if HAVE_MMAP - mmap_open (s); + if (avoid_mmap) + fd_open (s); + else + mmap_open (s); #else fd_open (s); #endif @@ -1060,7 +1063,7 @@ if (status == STATUS_SCRATCH) unlink (ioparm.file); - return fd_to_stream (fd, prot); + return fd_to_stream (fd, prot, 0); } @@ -1070,8 +1073,7 @@ stream * input_stream (void) { - - return fd_to_stream (STDIN_FILENO, PROT_READ); + return fd_to_stream (STDIN_FILENO, PROT_READ, 1); } @@ -1081,10 +1083,14 @@ stream * output_stream (void) { - - return fd_to_stream (STDOUT_FILENO, PROT_WRITE); + return fd_to_stream (STDOUT_FILENO, PROT_WRITE, 1); } +stream * +error_stream (void) +{ + return fd_to_stream (STDERR_FILENO, PROT_WRITE, 1); +} /* init_error_stream()-- Return a pointer to the error stream. This * subroutine is called when the stream is needed, rather than at diff -uNr gcc-4061.orig/libgfortran/libgfortran.h gcc-4061/libgfortran/libgfortran.h --- gcc-4061.orig/libgfortran/libgfortran.h 2004-10-16 06:05:56.000000000 +0900 +++ gcc-4061/libgfortran/libgfortran.h 2005-06-22 16:07:49.000000000 +0900 @@ -172,7 +172,7 @@ typedef struct { - int stdin_unit, stdout_unit, optional_plus; + int stdin_unit, stdout_unit, stderr_unit, optional_plus; int allocate_init_flag, allocate_init_value; int locus; diff -uNr gcc-4061.orig/libgfortran/runtime/environ.c gcc-4061/libgfortran/runtime/environ.c --- gcc-4061.orig/libgfortran/runtime/environ.c 2003-09-20 04:11:12.000000000 +0900 +++ gcc-4061/libgfortran/runtime/environ.c 2005-06-22 16:21:03.000000000 +0900 @@ -447,6 +447,11 @@ "Unit number that will be preconnected to standard output\n" "(No preconnection if negative)"}, + {"GFORTRAN_STDERR_UNIT", 0, &options.stderr_unit, init_integer, + show_integer, + "Unit number that will be preconnected to standard error\n" + "(No preconnection if negative)", 0}, + {"GFORTRAN_USE_STDERR", 1, &options.use_stderr, init_boolean, show_boolean, "Sends library output to standard error instead of standard output."},