bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: fflush & close behavior not well-defined


From: Wolfgang Laun
Subject: Re: fflush & close behavior not well-defined
Date: Mon, 14 Sep 2020 19:13:58 +0200

I've just read section 5.9, Closing Input and Output Redirections and noted
that it contains: * It is best to close each [open file]  when the program
has finished writing it.* Perhaps this could be put into a box to make this
stand out. As there is no open function people might forget that there *is*
a close.

The example containing the variable (!) pipe
   "grep " $1 " /some/file | my_prog -q " $3
is fine, but even a constant pipe requires a close call, because reading
beyond the end of a file or pipe does not result in a program termination:
... { ...
   command = "date"
   while ((command | getline) > 0) {
       print $0
    }
   # need here: close( command )
}
This might alert the ones who think that they don't have something like the
cited complex example.

A disambiguation for close might be done by permitting an optional pipe or
redirection sign preceding or following the file name or pipe command
string.
   close("| md5sum") # close pipe
   close(">md5sum" ) # close file
Unless, of course, this creates another ambiguity :-)

Wolfgang







On Mon, 14 Sep 2020 at 17:37, Andrew J. Schorr <
aschorr@telemetry-investments.com> wrote:

> On Mon, Sep 14, 2020 at 05:27:13PM +0200, Manuel Collado wrote:
> > El 14/09/2020 a las 15:07, Andrew J. Schorr escribió:
> > >Hi,
> > >
> > >On Sun, Sep 13, 2020 at 11:50:17PM -0600, arnold@skeeve.com wrote:
> > >>Thanks for the report. Gawk's close already takes an optional second
> > >>argument ("from" or "to") for closing one end of a two-way pipe.
> > >
> > >True, but if you look at the code in io.c:do_close, you'll see that the
> from/to
> > >"how" argument does not come into play until after the open file is
> identified
> > >by scanning the red_head list. So the presence of that argument does
> not limit
> > >the search to the subset of open pipes. It merely triggers a warning
> from
> > >io.c:close_redir if used with a file that's not a pipe. So in the case
> of
> > >close, the fix would require up to 3 arguments:
> > >    close(file, [, filetype [, how]])
> > >(with the code being smart enough to detect when "how" is located in the
> > >2nd position).
> >
> > Another possibility would be to add two specific close() wrappers, say
> >
> > close_file(file)
> > close_pipe(pipe, how)
> >
> > To be used if it is necessary to disambiguate.
> >
> > Just an idea.
>
> And you'd also need close_coprocess(pipe, how). In my view, it's simpler to
> enhance the existing close and fflush functions, but I grant that these
> problems are very unlikely to occur in real life.  It's just sloppiness in
> the
> awk POSIX spec that has failed to disambiguate.
>
> Regards,
> Andy
>
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]