From bug-request at octave dot org Tue Dec 13 12:44:12 2005 Subject: puts(1) closes stdout stream From: "John W. Eaton" To: David dot Bateman at motorola dot com Cc: bug at octave dot org Date: Tue, 13 Dec 2005 13:44:06 -0500 On 9-Dec-2005, David Bateman wrote: | It seems that puts(1) closes the stdout stream. I'm not quite sure how | it does this, but it seems to be due to the stdout_stream::fail variable | being set when octave_stream_list::insert is called. | The result is that the next call to fopen returns fid = 1 and this | results in a file that can't be closed with fclose.. The test for this is | | puts(1);nm=tmpnam();fid=fopen(nm,'w'),fclose(fid); I'm not sure that this is a bug in puts. If you pass something other than a character string to puts, it returns -1 and makes the text of the error available from ferror for the stream. You can clear the error with fclear (stdout). octave:1> puts (1) ans = -1 octave:2> ferror (stdout) ans = puts: argument must be a string octave:3> fclear (stdout) octave:4> puts ("foo\n") foo ans = 0 octave:5> fid = fopen (tmpnam (), "w") fid = 3 So it seems the bug is that when there is an error condition for stdout, it becomes available again as a file that can be opened. I think the following patch fixes the problem. Thanks, jwe src/ChangeLog: 2005-12-13 John W. Eaton * oct-stream.cc (octave_stream_list::do_insert): Check open state of stream in list instead of whether stream state is OK. Index: src/oct-stream.cc =================================================================== RCS file: /cvs/octave/src/oct-stream.cc,v retrieving revision 1.124 diff -u -r1.124 oct-stream.cc --- src/oct-stream.cc 29 Sep 2005 22:49:43 -0000 1.124 +++ src/oct-stream.cc 13 Dec 2005 18:42:10 -0000 at @ -3941,7 +3941,7 @@ { octave_stream tmp = list(i); - if (! tmp) + if (! tmp.is_open ()) { list(i) = os; stream_number = i; ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------