sh cleanup

Craig Dooley craig at xlnx-x.net
Mon Mar 22 10:46:09 PST 2004


The shell cannot handle some code created by the latest autotools I think,
so bring in changes from FreeBSD5.  First change removes it's own function 
copyfd and instead uses dup2.

-Craig
-- 
------------------------------------------------------------------------
Craig Dooley                                            craig at xxxxxxxxxx
------------------------------------------------------------------------
Index: bin/sh/eval.c
===================================================================
RCS file: /home/dcvs/src/bin/sh/eval.c,v
retrieving revision 1.4
diff -u -r1.4 eval.c
--- bin/sh/eval.c	19 Mar 2004 18:39:41 -0000	1.4
+++ bin/sh/eval.c	22 Mar 2004 17:19:38 -0000
@@ -499,16 +499,14 @@
 		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]);
 				}
 			}
@@ -565,8 +563,7 @@
 			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);
@@ -762,8 +759,7 @@
 			FORCEINTON;
 			close(pip[0]);
 			if (pip[1] != 1) {
-				close(1);
-				copyfd(pip[1], 1);
+				dup2(pip[1], 1);
 				close(pip[1]);
 			}
 		}
Index: bin/sh/input.c
===================================================================
RCS file: /home/dcvs/src/bin/sh/input.c,v
retrieving revision 1.4
diff -u -r1.4 input.c
--- bin/sh/input.c	19 Mar 2004 18:39:41 -0000	1.4
+++ bin/sh/input.c	22 Mar 2004 17:20:15 -0000
@@ -382,7 +382,7 @@
 	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");
Index: bin/sh/redir.c
===================================================================
RCS file: /home/dcvs/src/bin/sh/redir.c,v
retrieving revision 1.3
diff -u -r1.3 redir.c
--- bin/sh/redir.c	24 Aug 2003 16:26:00 -0000	1.3
+++ bin/sh/redir.c	22 Mar 2004 17:21:28 -0000
@@ -178,8 +178,7 @@
 			error("cannot open %s: %s", fname, errmsg(errno, E_OPEN));
 movefd:
 		if (f != fd) {
-			close(fd);
-			copyfd(f, fd);
+			dup2(f, fd);
 			close(f);
 		}
 		break;
@@ -239,12 +238,11 @@
 		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:
@@ -316,8 +314,10 @@
                                 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);
 			}
 		}
 	}
@@ -372,26 +372,3 @@
 		}
 	}
 }
-
-
-
-/*
- * 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;
-}
Attachment:
pgp00012.pgp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pgp00012.pgp
Type: application/octet-stream
Size: 187 bytes
Desc: "Description: PGP signature"
URL: <http://lists.dragonflybsd.org/pipermail/submit/attachments/20040322/1393a5cc/attachment-0018.obj>


More information about the Submit mailing list