Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 10 Feb 2000 13:20:02 -0800 (PST)
From:      Shawn Halpenny <malachai@iname.com>
To:        freebsd-bugs@FreeBSD.org
Subject:   [PATCH] Re: bin/16393: /bin/sh doesn't strip comments on shebang line
Message-ID:  <200002102120.NAA69877@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help
The following reply was made to PR bin/16393; it has been noted by GNATS.

From: Shawn Halpenny <malachai@iname.com>
To: freebsd-gnats-submit@FreeBSD.org, ryand@amazon.com
Cc:  
Subject: [PATCH] Re: bin/16393: /bin/sh doesn't strip comments on shebang line
Date: Thu, 10 Feb 2000 16:00:30 -0500

 I've run into this, too.  The problem seems to have two parts.
 
 First, the kernel parses the shebang line into white-space-separated
 tokens without any regard to the presence of a '#' character (which in
 the case we're interested in, denotes a comment).  The first patch
 makes the parsing slurp up everthing from the '#' to the end-of-line
 and store it as a single word.  This is necessary so that /bin/sh
 knows where the comment ends (otherwise (as it does currently),
 /bin/sh would receive the '#' and any comment-words as separate
 arguments and not know where the comment ended), because when the
 interpreter is started, the name of the script is tacked on as the the
 last argument.
 
 Second, /bin/sh will take the first non-option as a file name
 (according to sh(1)), which means it starts looking for a file named
 the first word of the comment on the shebang line.  So, I modified
 (see second patch) /bin/sh to ignore any command line words that begin
 with '#' when searching for a file to interpret.  This continues to
 allow things like:
 
     sh -c '# this is a nop'
 
 and also preservers the original command line for the interpreter.
 
 I'm not sure if this is the best way to fix things, but it appears to
 be consistent with current behavior and address the problem.
 
 Patches below.
 
 
 -- 
 Shawn Halpenny |        Maniacal@I Ache, Ohm    |  "Universal Danger!"
                +- - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - \
                | vi:G3kfM~lxfAPXh~l~2x2FirllpfcxlrifaprmfOX~Xp2hr.lrcelyl2p
 - - - - - - - -|    fU~X~refsPprnlxppri2lxlpr,pFrpprrfaPlpfiprgllxp~3Xlpfndw
 
 
 
 --- /usr/src/sys/kern/imgact_shell.c~	Wed Feb  9 17:14:09 2000
 +++ /usr/src/sys/kern/imgact_shell.c	Wed Feb  9 17:14:13 2000
 @@ -51,6 +51,7 @@
  exec_shell_imgact(imgp)
  	struct image_params *imgp;
  {
 +	const char *comment = NULL;
  	const char *image_header = imgp->image_header;
  	const char *ihp, *line_endp;
  	char *interp;
 @@ -112,7 +113,15 @@
  			 *	because this is at the front of the string buffer
  			 *	and the maximum shell command length is tiny.
  			 */
 -			while ((ihp < line_endp) && (*ihp != ' ') && (*ihp != '\t')) {
 +			while ((ihp < line_endp) &&
 +			    ((*ihp != ' ') && (*ihp != '\t') || comment)) {
 +
 +				/* Shell comment characters at the start of a token cause
 +				 *	everything to EOL to be one token.
 +				 */
 +				if (*ihp == '#')
 +					comment = ihp;
 +
  				*imgp->stringp++ = *ihp++;
  				imgp->stringspace--;
  			}
 
 
 --- /usr/src/bin/sh/options.c~	Thu Feb 10 11:02:38 2000
 +++ /usr/src/bin/sh/options.c	Thu Feb 10 13:41:10 2000
 @@ -108,6 +108,15 @@
  			optlist[i].val = 0;
  	arg0 = argv[0];
  	if (sflag == 0 && minusc == NULL) {
 +		/* Skip any arguments that start with shell-comment character
 +		 *	since it is unlikely the filename of a script given on
 +		 *	the command line will start with one.
 +		 */
 +		while (*argptr && **argptr == '#')
 +		{
 +			argptr++;
 +		}
 +
  		commandname = arg0 = *argptr++;
  		setinputfile(commandname, 0);
  	}
 


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200002102120.NAA69877>