[comp.bugs.4bsd] Getopt

gww@marduk.UUCP (Gary Winiger) (09/05/87)

Subject: Getopt(3) does not return '?' when opterr == 0 and an illegal option 
	is encountered +Fix
Index:	libc/compat-sys5/getopt.c 4.3BSD +Fix

Description:
	Getopt(3) should return '?' when either an illegal option or an
	option requiring an argument without on is encountered.  Instead,
	it gets a segmentation violation on systems which do not permit
	dereferencing of null pointers.
Repeat-By:
	Write a program which sets opterr to zero (0).
	Call that program with an illegal option.
Fix:
	The marco ``tell'' should always cause ``return(BADCH)'' to be
	executed, regardless of printing a diagnostic message.
	The attached code solves this problem at Elxsi.

Gary..
{ucbvax!sun,lll-lcc!lll-tis,amdahl!altos86,bridge2}!elxsi!gww
--------- cut --------- snip --------- :.,$w diff -------------
*** /tmp/,RCSt1000299	Fri Jun 26 11:05:41 1987
--- getopt.c	Fri Jun 26 11:05:26 1987
***************
*** 1,5 ****
--- 1,9 ----
  /*
   * $Log:	getopt.c,v $
+  * Revision 1.2  87/06/26  11:04:53  gww
+  * Return '?' when opterr == 0 and illegal option, or option requires an
+  * argument is encountered.
+  * 
   * Revision 1.1  87/01/07  18:25:01  gww
   * Initial revision
   * 
***************
*** 11,17 ****
   */
  
  #if defined(LIBC_SCCS) && !defined(lint)
! static char *ERcsId = "$Header: getopt.c,v 1.1 87/01/07 18:25:01 gww Exp $ ENIX BSD";
  static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
  #endif LIBC_SCCS and not lint
  
--- 15,21 ----
   */
  
  #if defined(LIBC_SCCS) && !defined(lint)
! static char *ERcsId = "$Header: getopt.c,v 1.2 87/06/26 11:04:53 gww Exp $ ENIX BSD";
  static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
  #endif LIBC_SCCS and not lint
  
***************
*** 27,34 ****
  
  #define BADCH	(int)'?'
  #define EMSG	""
! #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
! 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
  
  getopt(nargc,nargv,ostr)
  int	nargc;
--- 31,38 ----
  
  #define BADCH	(int)'?'
  #define EMSG	""
! #define tell(s)	{if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
! 		fputc(optopt,stderr);fputc('\n',stderr);}return(BADCH);}
  
  getopt(nargc,nargv,ostr)
  int	nargc;