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