At Sun, 11 Apr 2021 09:05:57 +0200, tlaro...@polynum.com wrote:
Subject: Re: Possible "new" redirect style for /bin/sh (needs a name)
>
> On Sat, Apr 10, 2021 at 05:04:11PM -0700, Greg A. Woods wrote:
> >
> > So, still, the shell could keep a "high FD watermark" for the range of
> > the FDs allocated by "{var}>file" and open them the normal way then move
> > them with dup2() to the next lower (i.e. unused) FD below this high FD
> > watermark, and reduce it in turn.  If I'm not mistaken the initial value
> > for the high FD watermark could be set to sysconf(_SC_OPEN_MAX)-1.
>
> I thought of this "mapping" too (using big numbers for "named fds" but
> mapping them to real small ones).

not "mapping" -- "moving".

and not moving them to small ones, but using the automatically allocated
next-higher number from open() and moving it up to the top of the
available range

dup2() (and dup3()) lets the caller specify the new FD -- which would be
N-1 in my example where N begins at sysconf(_SC_OPEN_MAX)-1 and moves
down.

/bin/sh already has a function called movefd() to do this (which
basically does the error handling around dup2() or dup3()), and so
"dynamic" redirection could use it like:

        N = sysconf(_SC_OPEN_MAX) - 1;

    ....

        fd = open();
        fd_var = movefd(fd, N-1);
        fd = -1;
        N--;

--
                                        Greg A. Woods <gwo...@acm.org>

Kelowna, BC     +1 250 762-7675           RoboHack <wo...@robohack.ca>
Planix, Inc. <wo...@planix.com>     Avoncote Farms <wo...@avoncote.ca>

Attachment: pgpykCKGPhMGs.pgp
Description: OpenPGP Digital Signature

Reply via email to