Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: File descriptor madness

On Thu, 9 May 2002, Peter Stephenson wrote:

> Apart from 0, which I suppose we need to test with open(), I had in mind
> just using dup() to dup 0 and see where the fd ended up.  This would in
> one go show open fd's (avoided) and open new fd's to block them.  Or
> doesn't that work?

Just one dup() will only show you where the first hole in the sequence of
fd numbers is, not whether there are any more descriptors open above that.
So you'd have to do (psuedo-code)

	if (!is_a_valid_fd(0)) { was_closed[0] = open(...); }
	while ((i = dup(0)) < 10) { was_closed[i] = 1; }
	/* ... initialize the shell ... */
	for (i = 0; i < 10; ++i) { if (was_closed[i]) close(i); }

I was merely wondering aloud whether it would be faster to do

	for (i = 0; !pipe(&just_opened[i]); ++i) {
	  if (just_opened[++i]) >= 9) break;
	/* ... initialize the shell ... */
	while (i >= 0) { close(just_opened[i--]); }

Replace pipe() with something else if there's a faster way to create new
descriptors.  It's possible/probable that dup() is a lot faster, but it'd
be nice not to need to hit the filesystem for that initial open(...) in
the first example.

Messages sorted by: Reverse Date, Date, Thread, Author