summaryrefslogtreecommitdiff
path: root/bin/ksh
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2006-03-17 16:30:14 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2006-03-17 16:30:14 +0000
commit545662306a341986faab81d8a2adb54b559c6830 (patch)
tree3c5e3664174a1c48bbb24f80a3810b68c133026f /bin/ksh
parent655efa37b5c8313ced96c25d2ed9b4346d8837fa (diff)
Simplify savefd() by removing the "noclose" flag and make noclose
behavior the default. Almost all uses of savefd() are followed by an implicit or explicit close. OK otto@
Diffstat (limited to 'bin/ksh')
-rw-r--r--bin/ksh/eval.c4
-rw-r--r--bin/ksh/exec.c13
-rw-r--r--bin/ksh/history.c6
-rw-r--r--bin/ksh/io.c18
-rw-r--r--bin/ksh/proto.h4
5 files changed, 25 insertions, 20 deletions
diff --git a/bin/ksh/eval.c b/bin/ksh/eval.c
index 393363f6dde..e2fc7fee64b 100644
--- a/bin/ksh/eval.c
+++ b/bin/ksh/eval.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: eval.c,v 1.28 2005/12/11 20:31:21 otto Exp $ */
+/* $OpenBSD: eval.c,v 1.29 2006/03/17 16:30:13 millert Exp $ */
/*
* Expansion - quoting, separation, substitution, globbing
@@ -862,7 +862,7 @@ comsub(Expand *xp, char *cp)
int ofd1, pv[2];
openpipe(pv);
shf = shf_fdopen(pv[0], SHF_RD, (struct shf *) 0);
- ofd1 = savefd(1, 0); /* fd 1 may be closed... */
+ ofd1 = savefd(1);
if (pv[1] != 1) {
ksh_dup2(pv[1], 1, false);
close(pv[1]);
diff --git a/bin/ksh/exec.c b/bin/ksh/exec.c
index f96781ce66f..3ef3103ab87 100644
--- a/bin/ksh/exec.c
+++ b/bin/ksh/exec.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: exec.c,v 1.44 2005/12/11 20:31:21 otto Exp $ */
+/* $OpenBSD: exec.c,v 1.45 2006/03/17 16:30:13 millert Exp $ */
/*
* execute command tree
@@ -121,9 +121,8 @@ execute(struct op *volatile t,
case TPIPE:
flags |= XFORK;
flags &= ~XEXEC;
- e->savefd[0] = savefd(0, 0);
- (void) ksh_dup2(e->savefd[0], 0, false); /* stdin of first */
- e->savefd[1] = savefd(1, 0);
+ e->savefd[0] = savefd(0);
+ e->savefd[1] = savefd(1);
while (t->type == TPIPE) {
openpipe(pv);
(void) ksh_dup2(pv[1], 1, false); /* stdout of curr */
@@ -178,8 +177,8 @@ execute(struct op *volatile t,
coproc_cleanup(true);
/* do this before opening pipes, in case these fail */
- e->savefd[0] = savefd(0, 0);
- e->savefd[1] = savefd(1, 0);
+ e->savefd[0] = savefd(0);
+ e->savefd[1] = savefd(1);
openpipe(pv);
if (pv[0] != 0) {
@@ -1109,7 +1108,7 @@ iosetup(struct ioword *iop, struct tbl *tp)
* is 2; also means we can't lose the fd (eg, both
* dup2 below and dup2 in restfd() failing).
*/
- e->savefd[iop->unit] = savefd(iop->unit, 1);
+ e->savefd[iop->unit] = savefd(iop->unit);
}
if (do_close)
diff --git a/bin/ksh/history.c b/bin/ksh/history.c
index 7b959455f95..bcd7ee6fbf3 100644
--- a/bin/ksh/history.c
+++ b/bin/ksh/history.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: history.c,v 1.33 2006/03/12 00:26:58 deraadt Exp $ */
+/* $OpenBSD: history.c,v 1.34 2006/03/17 16:30:13 millert Exp $ */
/*
* command history
@@ -646,7 +646,9 @@ hist_init(Source *s)
if ((fd = open(hname, O_RDWR|O_CREAT|O_APPEND, 0600)) < 0)
return;
- histfd = savefd(fd, 0);
+ histfd = savefd(fd);
+ if (histfd != fd)
+ close(fd);
(void) flock(histfd, LOCK_EX);
diff --git a/bin/ksh/io.c b/bin/ksh/io.c
index 7012d78773d..ea2925cb8aa 100644
--- a/bin/ksh/io.c
+++ b/bin/ksh/io.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: io.c,v 1.21 2005/03/30 17:16:37 deraadt Exp $ */
+/* $OpenBSD: io.c,v 1.22 2006/03/17 16:30:13 millert Exp $ */
/*
* shell buffered IO and formatted output
@@ -232,7 +232,7 @@ ksh_dup2(int ofd, int nfd, int errok)
* set close-on-exec flag.
*/
int
-savefd(int fd, int noclose)
+savefd(int fd)
{
int nfd;
@@ -244,8 +244,6 @@ savefd(int fd, int noclose)
else
errorf("too many files open in shell");
}
- if (!noclose)
- close(fd);
} else
nfd = fd;
fcntl(nfd, F_SETFD, FD_CLOEXEC);
@@ -268,10 +266,16 @@ restfd(int fd, int ofd)
void
openpipe(int *pv)
{
- if (pipe(pv) < 0)
+ int lpv[2];
+
+ if (pipe(lpv) < 0)
errorf("can't create pipe - try again");
- pv[0] = savefd(pv[0], 0);
- pv[1] = savefd(pv[1], 0);
+ pv[0] = savefd(lpv[0]);
+ if (pv[0] != lpv[0])
+ close(lpv[0]);
+ pv[1] = savefd(lpv[1]);
+ if (pv[1] != lpv[1])
+ close(lpv[1]);
}
void
diff --git a/bin/ksh/proto.h b/bin/ksh/proto.h
index 26f59f5e520..2547e2122d3 100644
--- a/bin/ksh/proto.h
+++ b/bin/ksh/proto.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: proto.h,v 1.29 2005/12/11 20:31:21 otto Exp $ */
+/* $OpenBSD: proto.h,v 1.30 2006/03/17 16:30:13 millert Exp $ */
/*
* prototypes for PD-KSH
@@ -118,7 +118,7 @@ void kshdebug_dump_(const char *, const void *, int);
int can_seek(int);
void initio(void);
int ksh_dup2(int, int, int);
-int savefd(int, int);
+int savefd(int);
void restfd(int, int);
void openpipe(int *);
void closepipe(int *);