[comp.os.minix] Shell bugs

msm@killer.DALLAS.TX.US (Michael Mattone) (03/04/89)

I've noticed a few problems with the shell.  Included below are cdiffs
for patching the shell source.

(1) The "trap" built-in only recognizes the first signal number.
    So, the following only sets a trap for signal 1:

	trap 'exit 1' 1 2 3

    The solution is to modify the function dotrap() in sh3.c.

(2) A command like

	set - `date`

    to set the positional parameters $1, $2, $3 etc., seems to put
    garbage there instead.  The problem seems to be with the statement
    t->words++ in the doset() function in file sh3.c.  Removing that
    and inserting a for() loop to copy the elements of t->words[] seems
    to work better.

(3) The value of "$#" should be 0 not -1 if no arguments are found when
    the shell starts up.  This can be fixed in file sh1.c near the
    end of the main() function where "$#" gets set.

(4) The shell variable "$!" is supposed to be set to the process id
    of the last process run in the background.  Currently it gets set
    to the process id of each new command.

echo x - sh1.c.cdif
sed '/^X/s///' > sh1.c.cdif << '/'
X*** oldsh/sh1.c	Fri Feb 24 00:22:28 1989
X--- newsh/sh1.c	Fri Feb 24 00:23:34 1989
X***************
X*** 131,137 ****
X  			setval(cprompt, "");
X  			prompt->status &= ~EXPORT;
X  			cprompt->status &= ~EXPORT;
X! 			if (newfile(*++argv))
X  				exit(1);
X  		}
X  	}
X--- 131,137 ----
X  			setval(cprompt, "");
X  			prompt->status &= ~EXPORT;
X  			cprompt->status &= ~EXPORT;
X! 			if (newfile(name = *++argv))
X  				exit(1);
X  		}
X  	}
X***************
X*** 162,168 ****
X  				dolc--;	/* keyword */
X  			else
X  				ap++;
X! 	setval(lookup("#"), putn(--dolc));
X  
X  	for (;;) {
X  		if (talking && e.iop <= iostack)
X--- 162,168 ----
X  				dolc--;	/* keyword */
X  			else
X  				ap++;
X! 	setval(lookup("#"), putn((--dolc < 0) ? (dolc = 0) : dolc));
X  
X  	for (;;) {
X  		if (talking && e.iop <= iostack)
/
echo x - sh3.c.cdif
sed '/^X/s///' > sh3.c.cdif << '/'
X*** oldsh/sh3.c	Fri Feb 24 00:22:32 1989
X--- newsh/sh3.c	Fri Feb 24 01:07:45 1989
X***************
X*** 85,90 ****
X--- 85,91 ----
X  		i = parent();
X  		if (i != 0) {
X  			if (i != -1) {
X+ 				setval(lookup("!"), putn(i));
X  				if (pin != NULL)
X  					closepipe(pin);
X  				if (talking) {
X***************
X*** 314,320 ****
X  	if (i != 0) {
X  		if (i == -1)
X  			warn("try again");
X- 		setval(lookup("!"), putn(i));
X  	}
X  	return(i);
X  }
X--- 315,320 ----
X***************
X*** 801,807 ****
X  register struct op *t;
X  {
X  	register char *s;
X! 	register n, i;
X  
X  	if (t->words[1] == NULL) {
X  		for (i=0; i<NSIG; i++)
X--- 801,808 ----
X  register struct op *t;
X  {
X  	register char *s;
X! 	register int  n, i;
X! 	register int  resetsig;
X  
X  	if (t->words[1] == NULL) {
X  		for (i=0; i<NSIG; i++)
X***************
X*** 813,835 ****
X  			}
X  		return(0);
X  	}
X! 	n = getsig((s = t->words[2])!=NULL? s: t->words[1]);
X! 	xfree(trap[n]);
X! 	trap[n] = 0;
X! 	if (s != NULL) {
X! 		if ((i = strlen(s = t->words[1])) != 0) {
X! 			trap[n] = strsave(s, 0);
X! 			setsig(n, sig);
X! 		} else
X! 			setsig(n, SIG_IGN);
X! 	} else {
X! 		if (talking)
X! 			if (n == SIGINT)
X! 				setsig(n, onintr);
X! 			else
X! 				setsig(n, n == SIGQUIT ? SIG_IGN : SIG_DFL);
X! 		else
X! 			setsig(n, SIG_DFL);
X  	}
X  	return(0);
X  }
X--- 814,840 ----
X  			}
X  		return(0);
X  	}
X! 	resetsig = digit(*t->words[1]);
X! 	for (i = resetsig ? 1 : 2; t->words[i] != NULL; ++i) {
X! 		n = getsig(t->words[i]);
X! 		xfree(trap[n]);
X! 		trap[n] = 0;
X! 		if (!resetsig) {
X! 			if (*t->words[1] != '\0') {
X! 				trap[n] = strsave(t->words[1], 0);
X! 				setsig(n, sig);
X! 			} else
X! 				setsig(n, SIG_IGN);
X! 		} else {
X! 			if (talking)
X! 				if (n == SIGINT)
X! 					setsig(n, onintr);
X! 				else
X! 					setsig(n, n == SIGQUIT ? SIG_IGN 
X! 							       : SIG_DFL);
X! 			else
X! 				setsig(n, SIG_DFL);
X! 		}
X  	}
X  	return(0);
X  }
X***************
X*** 977,983 ****
X  		return(0);
X  	}
X  	if (*cp == '-') {
X! 		t->words++;
X  		if (*++cp == 0)
X  			flag['x'] = flag['v'] = 0;
X  		else
X--- 982,990 ----
X  		return(0);
X  	}
X  	if (*cp == '-') {
X! 		/* bad: t->words++; */
X! 		for(n = 0; (t->words[n]=t->words[n+1]) != NULL; n++)
X! 			;
X  		if (*++cp == 0)
X  			flag['x'] = flag['v'] = 0;
X  		else
X***************
X*** 1016,1021 ****
X--- 1023,1049 ----
X  	}
X  }
X  
X+ #include <sys/types.h>
X+ #include <sys/times.h>
X+ 
X+ #define	SECS	60L
X+ #define	MINS	3600L
X+ 
X+ dotimes()
X+ {
X+ 	struct tms tbuf;
X+ 
X+ 	times(&tbuf);
X+ 
X+ 	prn((int)(tbuf.tms_cutime / MINS));
X+ 	prs("m");
X+ 	prn((int)((tbuf.tms_cutime % MINS) / SECS));
X+ 	prs("s ");
X+ 	prn((int)(tbuf.tms_cstime / MINS));
X+ 	prs("m");
X+ 	prn((int)((tbuf.tms_cstime % MINS) / SECS));
X+ 	prs("s\n");
X+ }
X  
X  struct	builtin {
X  	char	*command;
X***************
X*** 1040,1045 ****
X--- 1068,1074 ----
X  	"umask",	doumask,
X  	"login",	dologin,
X  	"newgrp",	dologin,
X+ 	"times",	dotimes,
X  	0,
X  };
X  
/
exit 0