'fopen' successfully opens directories
Sergei Steshenko
sergstesh at yahoo.com
Thu Apr 24 02:30:55 CDT 2008
--- "John W. Eaton" <jwe at bevo.che.wisc.edu> wrote:
> On 23-Apr-2008, Sergei Steshenko wrote:
>
> | --- "John W. Eaton" <jwe at bevo.che.wisc.edu> wrote:
> |
> | > Instead of a warning, maybe it would be better to just be compatible
> | > with the other brand and return -1?
>
> | What about 'matlab' compatibility ? I do not have 'matlab' to try.
>
> I thought that from what I wrote above it was clear that the Matlab
> behavior is to return -1 for directories. I checked in the following
> patch. It is really just a two line change plus changes in
> indentation.
>
> jwe
>
>
> > # HG changeset patch
> # User John W. Eaton <jwe at octave.org>
> # Date 1209019909 14400
> # Node ID b68e44c90afed78ea53353ca51c17e4138382158
> # Parent 6f2b2cc4b957669d4f7b80622cc600b5cd10a361
> file-io.cc (do_stream_open): return -1 for directories
>
> diff --git a/src/ChangeLog b/src/ChangeLog
> --- a/src/ChangeLog
> +++ b/src/ChangeLog
> @@ -1,3 +1,7 @@ 2008-04-23 John W. Eaton <jwe at octave.o
> +2008-04-24 John W. Eaton <jwe at octave.org>
> +
> + * file-io.cc (do_stream_open): Return -1 for directories.
> +
> 2008-04-23 John W. Eaton <jwe at octave.org>
>
> * DLD-FUNCTIONS/__qp__.cc (qp): Avoid bounds error when removing
> diff --git a/src/file-io.cc b/src/file-io.cc
> --- a/src/file-io.cc
> +++ b/src/file-io.cc
> @@ -415,12 +415,12 @@ do_stream_open (const std::string& name,
> {
> std::string fname = file_ops::tilde_expand (name);
>
> + file_stat fs (fname);
> +
> if (! (md & std::ios::out
> || octave_env::absolute_pathname (fname)
> || octave_env::rooted_relative_pathname (fname)))
> {
> - file_stat fs (fname);
> -
> if (! fs.exists ())
> {
> std::string tmp = octave_env::make_absolute
> @@ -434,49 +434,52 @@ do_stream_open (const std::string& name,
> }
> }
> }
> +
> + if (! fs.is_dir ())
> + {
> + std::string tmode = mode;
>
> - std::string tmode = mode;
> + // Use binary mode if 't' is not specified, but don't add
> + // 'b' if it is already present.
>
> - // Use binary mode if 't' is not specified, but don't add
> - // 'b' if it is already present.
> + size_t bpos = tmode.find ('b');
> + size_t tpos = tmode.find ('t');
>
> - size_t bpos = tmode.find ('b');
> - size_t tpos = tmode.find ('t');
> -
> - if (bpos == NPOS && tpos == NPOS)
> - tmode += 'b';
> + if (bpos == NPOS && tpos == NPOS)
> + tmode += 'b';
>
> #if defined (HAVE_ZLIB)
> - size_t pos = tmode.find ('z');
> + size_t pos = tmode.find ('z');
>
> - if (pos != NPOS)
> - {
> - tmode.erase (pos, 1);
> + if (pos != NPOS)
> + {
> + tmode.erase (pos, 1);
>
> - gzFile fptr = ::gzopen (fname.c_str (), tmode.c_str ());
> + gzFile fptr = ::gzopen (fname.c_str (), tmode.c_str ());
>
> - if (fptr)
> - retval = octave_zstdiostream::create (fname, fptr, md, flt_fmt);
> + if (fptr)
> + retval = octave_zstdiostream::create (fname, fptr, md, flt_fmt);
> + else
> + {
> + using namespace std;
> + retval.error (::strerror (errno));
> + }
> + }
> else
> +#endif
> {
> - using namespace std;
> - retval.error (::strerror (errno));
> + FILE *fptr = ::fopen (fname.c_str (), tmode.c_str ());
> +
> + retval = octave_stdiostream::create (fname, fptr, md, flt_fmt);
> +
> + if (! fptr)
> + {
> + using namespace std;
> + retval.error (::strerror (errno));
> + }
> }
> +
> }
> - else
> -#endif
> - {
> - FILE *fptr = ::fopen (fname.c_str (), tmode.c_str ());
> -
> - retval = octave_stdiostream::create (fname, fptr, md, flt_fmt);
> -
> - if (! fptr)
> - {
> - using namespace std;
> - retval.error (::strerror (errno));
> - }
> - }
> -
> }
> }
>
>
Oh, I didn't quite understand that "the other brand" was "matlab". I think you meant other
functions, but looking at all this back, it probably wasn't very logical.
I'm glad the directory instead of file user error will finally be caught.
Thanks,
Sergei.
Applications From Scratch: http://appsfromscratch.berlios.de/
____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
More information about the Bug-octave
mailing list