1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-02-13 19:52:20 +00:00

sh_subshell(): remove nofork flag and some dead code

Part of the *subshell_data/*sp struct:
92:	int		nofork;

In subshell.c, sh_subshell(), before entering a virtual subshell:
628:			if(!(sp->nofork = sh_state(SH_NOFORK)))
629:				sh_onstate(SH_NOFORK);
...and upon restoring when leaving the subshell:
696:		if(!sp->nofork)
697:			sh_offstate(SH_NOFORK);

This code is clearly nonsense, because 'sh_state(SH_NOFORK)' is
a static expression that always has the value 1. This can be seen
by looking at the definitions in defs.h:
194:	#define sh_state(x)	( 1<<(x))
...and in shell.h:
68:	#define SH_NOFORK	0	/* set when fork not necessary */

So, sh_state(SH_NOFORK) == 1<<(0) == 1, meaning sp->nofork is
always 0, meaning the statements conditional on it are never
executed. We can get rid of all of this; it's dead code.

In subshell.c on line 628, perhaps sh_isstate(SH_NOFORK) was meant
instead of sh_state(SH_NOFORK) -- but that state flag is supposed
to signal that forking is not necessary when running an external
command, that we can potentially 'exec' it directly if it's the
last command in the subshell -- which makes no sense at all when
being in a virtual subshell which does not have its own process.

This dead code was added in version 2008-09-21 ksh93t.

The subsequent 'flags |= sh_state(SH_NOFORK)' (subshell.c line 630)
probably makes more sense, since this sets execflg in sh_exec(),
which is also used for optimising redirections (avoids bothering to
store previous state if it's the last command in the subshell).
This commit is contained in:
Martijn Dekker 2022-06-18 19:17:44 +01:00
parent 40245e088d
commit 4be7e53550

View file

@ -89,7 +89,6 @@ static struct subshell
pid_t cpid;
int coutpipe;
int cpipe;
int nofork;
int subdup;
char subshare;
char comsub;
@ -625,8 +624,6 @@ Sfio_t *sh_subshell(Shnode_t *t, volatile int flags, int comsub)
sfswap(iop,sfstdout);
sfset(sfstdout,SF_READ,0);
sh.fdstatus[1] = IOWRITE;
if(!(sp->nofork = sh_state(SH_NOFORK)))
sh_onstate(SH_NOFORK);
flags |= sh_state(SH_NOFORK);
}
else if(sp->prev)
@ -693,8 +690,6 @@ Sfio_t *sh_subshell(Shnode_t *t, volatile int flags, int comsub)
sigrelease(SIGTSTP);
#endif
/* re-enable job control */
if(!sp->nofork)
sh_offstate(SH_NOFORK);
job.jobcontrol = sp->jobcontrol;
if(sp->monitor)
sh_onstate(SH_MONITOR);