1
From f393ff40ef49c582f614ff44107d1132753693a1 Mon Sep 17 00:00:00 2001
2
From: Herbert Xu <herbert@gondor.apana.org.au>
3
Date: Sat, 12 May 2007 18:00:57 +1000
4
Subject: [PATCH] [REDIR] Replace copyfd by savefd and use dup2 elsewhere
6
There are two kinds of users to copyfd, those that want to copy an fd to
7
an exact value and those that want to move an fd to a value >= 10. The
8
former can simply use dup2 directly while the latter share a lot of common
9
code that now constitutes savefd.
12
src/input.c | 8 +-----
13
src/jobs.c | 14 ++++--------
14
src/redir.c | 65 +++++++++++++++++++++++++++++++---------------------------
16
5 files changed, 44 insertions(+), 46 deletions(-)
18
diff --git a/ChangeLog b/ChangeLog
19
index 20c55d1..a7635fb 100644
23
* Removed unnecessary inclusion of redir.h from parser.c.
24
* Invoke sh_error on error in copyfd.
25
* Use dup2 instead of copyfd in evalbackcmd.
26
+ * Replace copyfd by savefd and dup2.
28
2007-05-05 Herbert Xu <herbert@gondor.apana.org.au>
30
diff --git a/src/input.c b/src/input.c
31
index 49a2972..7f99d4a 100644
34
@@ -428,7 +428,6 @@ int
35
setinputfile(const char *fname, int flags)
41
if ((fd = open(fname, O_RDONLY)) < 0) {
42
@@ -436,11 +435,8 @@ setinputfile(const char *fname, int flags)
44
sh_error("Can't open %s", fname);
47
- fd2 = copyfd(fd, 10);
53
setinputfd(fd, flags & INPUT_PUSH_FILE);
56
diff --git a/src/jobs.c b/src/jobs.c
57
index 9e28adb..7285d0d 100644
60
@@ -188,18 +188,14 @@ setjobctl(int on)
61
if (on == jobctl || rootshell == 0)
65
- ofd = fd = open(_PATH_TTY, O_RDWR);
66
+ fd = open(_PATH_TTY, O_RDWR);
69
- while (!isatty(fd) && --fd >= 0)
75
- fd = fcntl(fd, F_DUPFD, 10);
79
- fcntl(fd, F_SETFD, FD_CLOEXEC);
81
do { /* while we are in the background */
82
if ((pgrp = tcgetpgrp(fd)) < 0) {
84
diff --git a/src/redir.c b/src/redir.c
85
index aab1585..9e0fae5 100644
88
@@ -138,21 +138,10 @@ redirect(union node *redir, int flags)
91
if (sv && *(p = &sv->renamed[fd]) == EMPTY) {
92
- int i = fcntl(fd, F_DUPFD, 10);
96
- const char *m = strerror(i);
98
- sh_error("%d: %s", fd, m);
102
+ int i = savefd(fd);
112
dupredirect(n, newfd, memory);
113
@@ -244,6 +233,7 @@ dupredirect(redir, f)
116
int fd = redir->nfile.fd;
121
@@ -255,16 +245,24 @@ dupredirect(redir, f)
125
- copyfd(redir->ndup.dupfd, fd);
126
+ if (dup2(f, fd) < 0) {
135
+ } else if (dup2(f, fd) < 0)
149
+ sh_error("%d: %s", f, strerror(err));
153
@@ -327,10 +325,8 @@ popredir(int drop)
155
for (i = 0 ; i < 10 ; i++) {
156
if (rp->renamed[i] != EMPTY) {
159
- copyfd(rp->renamed[i], i);
162
+ dup2(rp->renamed[i], i);
163
close(rp->renamed[i]);
166
@@ -372,17 +368,26 @@ clearredir(int drop)
170
- * Copy a file descriptor to be >= to. Invokes sh_error on error.
171
+ * Move a file descriptor to > 10. Invokes sh_error on error unless
172
+ * the original file dscriptor is not open.
176
-copyfd(int from, int to)
182
+ newfd = fcntl(from, F_DUPFD, 10);
183
+ err = newfd < 0 ? errno : 0;
184
+ if (err != EBADF) {
187
+ sh_error("%d: %s", from, strerror(err));
189
+ fcntl(newfd, F_SETFD, FD_CLOEXEC);
192
- newfd = fcntl(from, F_DUPFD, to);
194
- sh_error("%d: %s", from, strerror(errno));
198
diff --git a/src/redir.h b/src/redir.h
199
index f4347ce..2c43511 100644
202
@@ -45,6 +45,6 @@ union node;
203
void redirect(union node *, int);
205
void clearredir(int);
206
-int copyfd(int, int);
208
int redirectsafe(union node *, int);