From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 8086 invoked from network); 9 May 2002 20:37:05 -0000 Received: from sunsite.dk (130.225.247.90) by ns1.primenet.com.au with SMTP; 9 May 2002 20:37:05 -0000 Received: (qmail 10442 invoked by alias); 9 May 2002 20:36:56 -0000 Mailing-List: contact zsh-workers-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 17119 Received: (qmail 10430 invoked from network); 9 May 2002 20:36:55 -0000 Date: Thu, 9 May 2002 13:36:49 -0700 (PDT) From: Bart Schaefer Sender: schaefer@ns1.sodaware.com To: Peter Stephenson cc: Zsh hackers list Subject: Re: File descriptor madness In-Reply-To: <20020509185715.C4F301C0AB@pwstephenson.fsnet.co.uk> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII 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.