From bob@luke.pmr.com  Wed Feb 25 09:48:37 1998
Received: from luke.pmr.com (luke.pmr.com [207.170.114.132])
          by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id JAA04934
          for <FreeBSD-gnats-submit@freebsd.org>; Wed, 25 Feb 1998 09:48:34 -0800 (PST)
          (envelope-from bob@luke.pmr.com)
Received: (from bob@localhost)
	by luke.pmr.com (8.8.8/8.8.8) id LAA06639;
	Wed, 25 Feb 1998 11:48:31 -0600 (CST)
	(envelope-from bob)
Message-Id: <199802251748.LAA06639@luke.pmr.com>
Date: Wed, 25 Feb 1998 11:48:31 -0600 (CST)
From: bob@pmr.com
Reply-To: bob@pmr.com
To: FreeBSD-gnats-submit@freebsd.org
Subject: in sh, set -- `getopt ...` always returns zero
X-Send-Pr-Version: 3.2

>Number:         5845
>Category:       bin
>Synopsis:       in sh, set -- `getopt ...` always returns zero
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Feb 25 09:50:01 PST 1998
>Closed-Date:    Sun Apr 4 00:26:22 MEST 1999
>Last-Modified:  Sun Apr  4 00:28:26 MEST 1999
>Originator:     Bob Willcox
>Release:        FreeBSD 2.2.5-STABLE i386
>Organization:
Power Micro Research
>Environment:

FreeBSD 2.2.5-STABLE (as of Feb 22, 1998).  Also fails on -current
(as of Feb 4, 1998).  Failure appears to be in sh.  Note that pdksh
(vv5.2.13) works with getopt as documented and bash (v1.14.7) behaves
the same as sh.

>Description:

The sh always returns a zero return code from the set command used with
getopt.  E.g., the example in the getopt man page does not work since
errors in flags/options will not result in a non zero return code from
the set command in:

    set -- `getopt abo: $*`
    if test $? != 0
    then
	echo 'Usage: ...'
	exit 2
    fi
    ...

Consequently, the subsequent if will never be true.

Seems either the sh set command should be fixed to work as documented in
the getopt manpage or the example should be updated to something that
does work.

>How-To-Repeat:

Run this

    #!/bin/sh

    set -- `getopt d:v $*`
    echo "set return code = $?"

and notice that even with unrecognized flags the return code from the
set statement is zero.

>Fix:
	

>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->closed 
State-Changed-By: cracauer 
State-Changed-When: Sun Apr 4 00:26:22 MEST 1999 
State-Changed-Why:  
Fixed in 4.0-current from today. Will be fixed in 3.x in a few 
days. Releases 3.2 and 4.0 should have this change. 

The problem wasn't with getopt, but with the example in the manpage. 

Here's the log: 

1) Fix the case where a shellscript using getopt is called with a 
parameter that has space in it, both in getopt.c and in the manpage 
example.  

2) Fix the example in the manpage. The set(1) command is required to 
return 0 (POSIX 1003.2, section 3.14.11), so you can't test for 
getopt's exit status like the example did: 

#! /bin/sh 
set -- `getopt abo: $*` 
if test $? != 0  # wrong, tests for set's exit status, which is 
# always zero, no for getopt(1)'s. 

Fixes PR bin/5845, which thought it was getopt's fault, but in fact 
the manpage was wrong. 

I also updated the example to be more useful and updated the BUGS 
section. 
>Unformatted:
