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