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