diff options
author | des <des@FreeBSD.org> | 2004-01-21 12:50:01 +0000 |
---|---|---|
committer | des <des@FreeBSD.org> | 2004-01-21 12:50:01 +0000 |
commit | 5bed8122fd9ad3746f9f6139eac42ce5041f08f4 (patch) | |
tree | 45b894f449c0d20308caae0992208804c1312206 /bin | |
parent | c939cff38891929d64b9b5a0d14e2cd42623a789 (diff) | |
download | FreeBSD-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')
-rw-r--r-- | bin/sh/eval.c | 12 | ||||
-rw-r--r-- | bin/sh/input.c | 2 | ||||
-rw-r--r-- | bin/sh/parser.c | 1 | ||||
-rw-r--r-- | bin/sh/redir.c | 40 | ||||
-rw-r--r-- | bin/sh/redir.h | 1 |
5 files changed, 13 insertions, 43 deletions
diff --git a/bin/sh/eval.c b/bin/sh/eval.c index bf96c2f..38282d4 100644 --- a/bin/sh/eval.c +++ b/bin/sh/eval.c @@ -479,16 +479,14 @@ evalpipe(union node *n) if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) { INTON; if (prevfd > 0) { - close(0); - copyfd(prevfd, 0); + dup2(prevfd, 0); close(prevfd); } if (pip[1] >= 0) { if (!(prevfd >= 0 && pip[0] == 0)) close(pip[0]); if (pip[1] != 1) { - close(1); - copyfd(pip[1], 1); + dup2(pip[1], 1); close(pip[1]); } } @@ -545,8 +543,7 @@ evalbackcmd(union node *n, struct backcmd *result) FORCEINTON; close(pip[0]); if (pip[1] != 1) { - close(1); - copyfd(pip[1], 1); + dup2(pip[1], 1); close(pip[1]); } evaltree(n, EV_EXIT); @@ -742,8 +739,7 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd) FORCEINTON; close(pip[0]); if (pip[1] != 1) { - close(1); - copyfd(pip[1], 1); + dup2(pip[1], 1); close(pip[1]); } } diff --git a/bin/sh/input.c b/bin/sh/input.c index 85b94d6..9948881 100644 --- a/bin/sh/input.c +++ b/bin/sh/input.c @@ -388,7 +388,7 @@ setinputfile(char *fname, int push) if ((fd = open(fname, O_RDONLY)) < 0) error("Can't open %s: %s", fname, strerror(errno)); if (fd < 10) { - fd2 = copyfd(fd, 10); + fd2 = fcntl(fd, F_DUPFD, 10); close(fd); if (fd2 < 0) error("Out of file descriptors"); diff --git a/bin/sh/parser.c b/bin/sh/parser.c index da166e6..70ac6ba 100644 --- a/bin/sh/parser.c +++ b/bin/sh/parser.c @@ -48,7 +48,6 @@ __FBSDID("$FreeBSD$"); #include "parser.h" #include "nodes.h" #include "expand.h" /* defines rmescapes() */ -#include "redir.h" /* defines copyfd() */ #include "syntax.h" #include "options.h" #include "input.h" 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; -} diff --git a/bin/sh/redir.h b/bin/sh/redir.h index e786004..d063ecf 100644 --- a/bin/sh/redir.h +++ b/bin/sh/redir.h @@ -46,5 +46,4 @@ void redirect(union node *, int); void popredir(void); int fd0_redirected_p(void); void clearredir(void); -int copyfd(int, int); |