[phobos] Removing std.stdio.File.popen()

Lars Tandle Kyllingstad lars at kyllingen.net
Wed Aug 18 04:38:10 PDT 2010


On Wed, 2010-08-18 at 04:44 -0500, Andrei Alexandrescu wrote:
> On 08/18/2010 03:10 AM, Lars Tandle Kyllingstad wrote:
> > On Tue, 2010-08-17 at 22:03 -0500, Andrei Alexandrescu wrote:
> >> I understand that, and the motivation has been thoroughly explained in
> >> the Unix lore. My question is, why define a separate type? Wouldn't
> >> creating a file and calling setbuf(null, 0) suffice?
> >
> >
> > Well, for one thing, I didn't know about setbuf() until just now. :)
> >
> > I still think I prefer the UnbufferedFile solution, though.  setbuf()
> > should be called before anything is read or written to the stream.  This
> > means it will be up to the user to do so, and this is easily forgotten.
> > Compare
> >
> >          auto f = UnbufferedFile("myfile.txt");
> >          ...
> >          auto p = spawnProcess("myapp", f);
> >
> > to
> >
> >          auto f = File("myfile.txt");
> >          setbuf(f.getFP(), null);      // DON'T FORGET THIS!
> >          ...
> >          auto p = spawnProcess("myapp", f);
> 
> spawnProcess could call setbuf itself and fail if that doesn't go through.


On my computer, setvbuf() doesn't fail when you turn off the buffer
after reading something.  In particular, the following program runs
fine:

        import core.stdc.stdio, std.string;
        
        void main(string[] args)
        {
            auto file = fopen(toStringz(args[1]), "r");
        
            auto data = new char[10];
            fread(data.ptr, 1, data.length, file);
        
            // This should fail, but doesn't:
            assert (setvbuf(file, null, _IONBF, 0) == 0);
        
            fclose(file);
        }

This means that it either silently ignores the request to turn off
buffering, or it ditches a whole lot of data. :(

-Lars



More information about the phobos mailing list