This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: trivial mkpasswd defect


> On Wed, 29 May 2002 17:45:35 -0400 Bernard A Badger <bab@vx.com> wrote:
[SNIP] 
> I don't see anything there that says that POSIX endorses '-?' for
> displaying the usage message.  It just shows how to handle invalid option
> characters.  If getopt sees an invalid character after '-' it returns 
> '?' and puts the
> invalid character ("?" in your example) in optopt.  In that example, 
> I'd expect to see
> 
> Unrecognized option -?
> <<usage message>>
> 
> >From 'man bash':
> 
> getopts optstring name [args]
>        getopts is used by shell procedures to parse positional 
> parameters.   optstring  contains  the
>        option  characters  to  be  recognized;  if  a character is 
> followed by a colon, the option is
>        expected to have an argument, which should be separated from 
> it by white space.  The colon and
>        question  mark  characters  may  not  be  used as option 
> characters. . . .
> 
> I don't have the man page for getopt() handy, but I think it contains the
> same restriction.

Oh, I didn't understand what you meant.  You think that '-?' cannot be used
as an option.

Yes, it's true that you cannot have '?' as an legal option character.  
This is because getopt returns the option character that was used, 
and '?' is used as an error indication.  Whenever the user calls the 
command with an error, '?' is returned instead of a legal option character.  
Thus, all illegal options -- and options with missing arguments ---
conveniently come back as a single character '?'.  

But so does '-?' !  That means that '-?' is _always_ illegal.  
The programmer cannot make -? a legal option, 
but the user can deliberately invoke it as an illegal option syntax.
The user can _rely_ on it being erroneous!

If you provide an illegal option, the command is supposed to 
print a usage message to stderr and fail (return a non-zero error code).
That's exactly what the example did.

Since this is hard-coded into the getopt() function, many commands are
written with '-?' in mind.  Since the user can get a usage this way, 
it's not necessary, in POSIX, to have -h as an option.  
(But see my other mail about GNU and --help requirement.)

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]