summaryrefslogtreecommitdiffstats
path: root/bin/sh/redir.c
diff options
context:
space:
mode:
authordes <des@FreeBSD.org>2004-01-21 12:50:01 +0000
committerdes <des@FreeBSD.org>2004-01-21 12:50:01 +0000
commit5bed8122fd9ad3746f9f6139eac42ce5041f08f4 (patch)
tree45b894f449c0d20308caae0992208804c1312206 /bin/sh/redir.c
parentc939cff38891929d64b9b5a0d14e2cd42623a789 (diff)
downloadFreeBSD-src-5bed8122fd9ad3746f9f6139eac42ce5041f08f4.zip
FreeBSD-src-5bed8122fd9ad3746f9f6139eac42ce5041f08f4.tar.gz
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
Diffstat (limited to 'bin/sh/redir.c')
-rw-r--r--bin/sh/redir.c40
1 files changed, 8 insertions, 32 deletions
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;
-}
OpenPOWER on IntegriCloud