61d8f4
diff -up ksh-20120801/src/cmd/ksh93/include/io.h.macro ksh-20120801/src/cmd/ksh93/include/io.h
61d8f4
--- ksh-20120801/src/cmd/ksh93/include/io.h.macro	2012-07-18 16:12:38.000000000 +0200
61d8f4
+++ ksh-20120801/src/cmd/ksh93/include/io.h	2013-07-04 16:14:05.809595966 +0200
61d8f4
@@ -81,6 +81,7 @@ extern void 	sh_iosave(Shell_t *, int,in
61d8f4
 extern int 	sh_iovalidfd(Shell_t*, int);
61d8f4
 extern int 	sh_inuse(Shell_t*, int);
61d8f4
 extern void 	sh_iounsave(Shell_t*);
61d8f4
+extern void	iounpipe(Shell_t*);
61d8f4
 extern int	sh_chkopen(const char*);
61d8f4
 extern int	sh_ioaccess(int,int);
61d8f4
 extern int	sh_devtofd(const char*);
61d8f4
diff -up ksh-20120801/src/cmd/ksh93/sh/subshell.c.macro ksh-20120801/src/cmd/ksh93/sh/subshell.c
61d8f4
--- ksh-20120801/src/cmd/ksh93/sh/subshell.c.macro	2013-07-04 16:14:05.783595751 +0200
61d8f4
+++ ksh-20120801/src/cmd/ksh93/sh/subshell.c	2013-07-04 16:15:46.673432991 +0200
61d8f4
@@ -171,7 +171,7 @@ void sh_subfork(void)
61d8f4
 {
61d8f4
 	register struct subshell *sp = subshell_data;
61d8f4
 	Shell_t	*shp = sp->shp;
61d8f4
-	int	curenv = shp->curenv;
61d8f4
+	int	curenv = shp->curenv, comsub=shp->comsub;
61d8f4
 	pid_t pid;
61d8f4
 	char *trap = shp->st.trapcom[0];
61d8f4
 	if(trap)
61d8f4
@@ -204,7 +204,7 @@ void sh_subfork(void)
61d8f4
 		shp->comsub = 0;
61d8f4
 		SH_SUBSHELLNOD->nvalue.s = 0;
61d8f4
 		sp->subpid=0;
61d8f4
-		shp->st.trapcom[0] = trap;
61d8f4
+		shp->st.trapcom[0] = (comsub==2?NULL:trap);
61d8f4
 		shp->savesig = 0;
61d8f4
 	}
61d8f4
 }
61d8f4
@@ -743,7 +743,6 @@ Sfio_t *sh_subshell(Shell_t *shp,Shnode_
61d8f4
 		fchdir(shp->pwdfd);
61d8f4
 	}
61d8f4
 	shp->subshare = sp->subshare;
61d8f4
-	shp->comsub = sp->comsub;
61d8f4
 	shp->subdup = sp->subdup;
61d8f4
 #if SHOPT_COSHELL
61d8f4
 	shp->coshell = sp->coshell;
61d8f4
@@ -773,7 +772,12 @@ Sfio_t *sh_subshell(Shell_t *shp,Shnode_
61d8f4
 	if(nsig>0)
61d8f4
 		kill(getpid(),nsig);
61d8f4
 	if(sp->subpid)
61d8f4
+	{
61d8f4
 		job_wait(sp->subpid);
61d8f4
+		if(comsub>1)
61d8f4
+			iounpipe(shp);
61d8f4
+	}
61d8f4
+	shp->comsub = sp->comsub;
61d8f4
 	if(comsub && iop && sp->pipefd<0)
61d8f4
 		sfseek(iop,(off_t)0,SEEK_SET);
61d8f4
 	if(shp->trapnote)
61d8f4
diff -up ksh-20120801/src/cmd/ksh93/sh/xec.c.macro ksh-20120801/src/cmd/ksh93/sh/xec.c
61d8f4
--- ksh-20120801/src/cmd/ksh93/sh/xec.c.macro	2013-07-04 16:14:05.800595891 +0200
61d8f4
+++ ksh-20120801/src/cmd/ksh93/sh/xec.c	2013-07-04 16:14:05.810595975 +0200
61d8f4
@@ -102,11 +102,11 @@ struct funenv
61d8f4
  * temp file.
61d8f4
  */
61d8f4
 static int      subpipe[3],subdup,tsetio,usepipe;
61d8f4
-static void iounpipe(Shell_t*);
61d8f4
+void iounpipe(Shell_t*);
61d8f4
 
61d8f4
-static int iousepipe(Shell_t *shp)
61d8f4
+int iousepipe(Shell_t *shp)
61d8f4
 {
61d8f4
-	int i;
61d8f4
+	int fd=sffileno(sfstdout),i,err=errno;
61d8f4
 	if(usepipe)
61d8f4
 	{
61d8f4
 		usepipe++;
61d8f4
@@ -115,13 +115,18 @@ static int iousepipe(Shell_t *shp)
61d8f4
 	if(sh_rpipe(subpipe) < 0)
61d8f4
 		return(0);
61d8f4
 	usepipe++;
61d8f4
-	fcntl(subpipe[0],F_SETFD,FD_CLOEXEC);
61d8f4
-	subpipe[2] = sh_fcntl(1,F_DUPFD,10);
61d8f4
-	fcntl(subpipe[2],F_SETFD,FD_CLOEXEC);
61d8f4
+	if(shp->comsub!=1)
61d8f4
+	{
61d8f4
+		subpipe[2] = sh_fcntl(subpipe[1],F_DUPFD,10);
61d8f4
+		sh_close(subpipe[1]);
61d8f4
+		return(1);
61d8f4
+	}
61d8f4
+	subpipe[2] = sh_fcntl(fd,F_dupfd_cloexec,10);
61d8f4
 	shp->fdstatus[subpipe[2]] = shp->fdstatus[1];
61d8f4
-	close(1);
61d8f4
-	fcntl(subpipe[1],F_DUPFD,1);
61d8f4
-	shp->fdstatus[1] = shp->fdstatus[subpipe[1]];
61d8f4
+	while(close(fd)<0 && errno==EINTR)
61d8f4
+		errno = err;
61d8f4
+	fcntl(subpipe[1],F_DUPFD,fd);
61d8f4
+	shp->fdstatus[1] = shp->fdstatus[subpipe[1]]&~IOCLEX;
61d8f4
 	sh_close(subpipe[1]);
61d8f4
 	if(subdup=shp->subdup) for(i=0; i < 10; i++)
61d8f4
 	{
61d8f4
@@ -135,14 +140,23 @@ static int iousepipe(Shell_t *shp)
61d8f4
 	return(1);
61d8f4
 }
61d8f4
 
61d8f4
-static void iounpipe(Shell_t *shp)
61d8f4
+void iounpipe(Shell_t *shp)
61d8f4
 {
61d8f4
-	int n;
61d8f4
+	int fd=sffileno(sfstdout),n,err=errno;
61d8f4
 	char buff[SF_BUFSIZE];
61d8f4
-	close(1);
61d8f4
-	fcntl(subpipe[2], F_DUPFD, 1);
61d8f4
-	shp->fdstatus[1] = shp->fdstatus[subpipe[2]];
61d8f4
+	if(!usepipe)
61d8f4
+		return;
61d8f4
 	--usepipe;
61d8f4
+	if(shp->comsub>1)
61d8f4
+	{
61d8f4
+		sh_close(subpipe[2]);
61d8f4
+		while(read(subpipe[0],buff,sizeof(buff))>0);
61d8f4
+		goto done;
61d8f4
+	}
61d8f4
+	while(close(fd)<0 && errno==EINTR)
61d8f4
+		errno = err;
61d8f4
+	fcntl(subpipe[2], F_DUPFD, fd);
61d8f4
+	shp->fdstatus[1] = shp->fdstatus[subpipe[2]];
61d8f4
 	if(subdup) for(n=0; n < 10; n++)
61d8f4
 	{
61d8f4
 		if(subdup&(1<
61d8f4
@@ -174,6 +188,7 @@ static void iounpipe(Shell_t *shp)
61d8f4
 		else if(errno!=EINTR)
61d8f4
 			break;
61d8f4
 	}
61d8f4
+done:
61d8f4
 	sh_close(subpipe[0]);
61d8f4
 	subpipe[0] = -1;
61d8f4
 	tsetio = 0;
61d8f4
@@ -725,7 +740,7 @@ static void unset_instance(Namval_t *nq,
61d8f4
 }
61d8f4
 
61d8f4
 #if SHOPT_COSHELL
61d8f4
-uintmax_t	coused;
61d8f4
+static uintmax_t	coused;
61d8f4
 /*
61d8f4
  * print out function definition
61d8f4
  */
61d8f4
@@ -1619,10 +1634,14 @@ int sh_exec(register const Shnode_t *t,
61d8f4
 			if(shp->subshell)
61d8f4
 			{
61d8f4
 				sh_subtmpfile(shp);
61d8f4
-				if(shp->comsub==1 && !(shp->fdstatus[1]&IONOSEEK))
61d8f4
-					unpipe=iousepipe(shp);
61d8f4
 				if((type&(FAMP|TFORK))==(FAMP|TFORK))
61d8f4
-					sh_subfork();
61d8f4
+				{
61d8f4
+					if(shp->comsub && !(shp->fdstatus[1]&IONOSEEK))
61d8f4
+					{
61d8f4
+						unpipe = iousepipe(shp);
61d8f4
+						sh_subfork();
61d8f4
+					}
61d8f4
+				}
61d8f4
 			}
61d8f4
 			no_fork = !ntflag && !(type&(FAMP|FPOU)) && !shp->subshell &&
61d8f4
 			    !(shp->st.trapcom[SIGINT] && *shp->st.trapcom[SIGINT]) &&
61d8f4
@@ -3495,8 +3514,7 @@ static void sh_funct(Shell_t *shp,Namval
61d8f4
 	struct funenv fun;
61d8f4
 	char *fname = nv_getval(SH_FUNNAMENOD);
61d8f4
 	struct Level	*lp =(struct Level*)(SH_LEVELNOD->nvfun);
61d8f4
-	int		level, pipepid=shp->pipepid, comsub=shp->comsub;
61d8f4
-	shp->comsub = 0;
61d8f4
+	int		level, pipepid=shp->pipepid;
61d8f4
 	shp->pipepid = 0;
61d8f4
 	sh_stats(STAT_FUNCT);
61d8f4
 	if(!lp->hdr.disc)
61d8f4
@@ -3539,7 +3557,6 @@ static void sh_funct(Shell_t *shp,Namval
61d8f4
 	lp->maxlevel = level;
61d8f4
 	SH_LEVELNOD->nvalue.s = lp->maxlevel;
61d8f4
 	shp->last_root = nv_dict(DOTSHNOD);
61d8f4
-	shp->comsub = comsub;
61d8f4
 #if 0
61d8f4
 	nv_putval(SH_FUNNAMENOD,shp->st.funname,NV_NOFREE);
61d8f4
 #else