From 5bed8122fd9ad3746f9f6139eac42ce5041f08f4 Mon Sep 17 00:00:00 2001 From: des Date: Wed, 21 Jan 2004 12:50:01 +0000 Subject: Replace home-grown dup2() implementation with actual dup2() calls. This should slightly reduce the number of system calls in critical portions of the shell, and select a more efficient path through the fdalloc code. Reviewed by: bde --- bin/sh/redir.c | 40 ++++++++-------------------------------- 1 file changed, 8 insertions(+), 32 deletions(-) (limited to 'bin/sh/redir.c') diff --git a/bin/sh/redir.c b/bin/sh/redir.c index 547cdcc..6c4f0b8 100644 --- a/bin/sh/redir.c +++ b/bin/sh/redir.c @@ -182,8 +182,7 @@ openredirect(union node *redir, char memory[10]) error("cannot open %s: %s", fname, strerror(errno)); movefd: if (f != fd) { - close(fd); - copyfd(f, fd); + dup2(f, fd); close(f); } break; @@ -215,12 +214,11 @@ movefd: if (redir->ndup.dupfd >= 0) { /* if not ">&-" */ if (memory[redir->ndup.dupfd]) memory[fd] = 1; - else { - close(fd); - copyfd(redir->ndup.dupfd, fd); - } - } else + else + dup2(redir->ndup.dupfd, fd); + } else { close(fd); + } break; case NHERE: case NXHERE: @@ -288,10 +286,11 @@ popredir(void) if (rp->renamed[i] != EMPTY) { if (i == 0) fd0_redirected--; - close(i); if (rp->renamed[i] >= 0) { - copyfd(rp->renamed[i], i); + dup2(rp->renamed[i], i); close(rp->renamed[i]); + } else { + close(i); } } } @@ -346,26 +345,3 @@ clearredir(void) } } } - - - -/* - * Copy a file descriptor to be >= to. Returns -1 - * if the source file descriptor is closed, EMPTY if there are no unused - * file descriptors left. - */ - -int -copyfd(int from, int to) -{ - int newfd; - - newfd = fcntl(from, F_DUPFD, to); - if (newfd < 0) { - if (errno == EMFILE) - return EMPTY; - else - error("%d: %s", from, strerror(errno)); - } - return newfd; -} -- cgit v1.1