getopt reinvocation/reentrancy

Jeff Johnston
Mon Mar 3 21:48:00 GMT 2008

Resending as I hit the text/html button accidentally and sourceware 
bounced it.  Also I have changed the content slightly.

Ralf Corsepius wrote:
> On Thu, 2008-02-28 at 19:21 -0500, Gregory Pietsch wrote:
>> I believe that I caught the problem. In your test program, the -a and -b 
>> switches weren't defined in your list of short options, so getopt was 
>> returning a question mark. Fixing the testing program and applying the 
>> latest Bug-Free(tm) getopt should work. -- Gregory
> I think, some defines, you added to getopt.h with your most recent patch
> should be removed. In particular:
> /* macros defined by this include file */
> #define NO_ARG                  0
> #define REQUIRED_ARG            1
> #define OPTIONAL_ARG            2
>   /* The GETOPT_DATA_INITIALIZER macro is used to initialize a
> statically-
>      allocated variable of type struct getopt_data.  */
> #define GETOPT_DATA_INITIALIZER {0,0,0,0,0}
> Rationale:
> - The *_ARG macros are not name-space safe and likely to clash with
> other macros. If they really should be kept, then they should be
> prefixed with __GETOPT_.
> - All of these macros are not standardized anywhere nor am I aware about
> any system providing them. I vote for removing them.
To be fair, the header file itself is non-standard; it is a glibc 
extension and any application that includes it will expect at least some 
non-clean macros since the glibc version of <getopt.h> declares struct 
option, includes 3 macros: no_argument, required_argument, 
optional_argument that are unprotected and adds the functions 
getopt_long and getopt_long_only.  The sys/linux version of getopt.h 
defines the 3 glibc macros for compatibility and so should the 
libc/include version.  I'd be happy with only having the glibc names.  
We could also optionally add a single underscore to the initializer 
macro and two underscores to the special struct added for reentrancy to 
make us essentially namespace-clean-equivalent to the glibc version.   
Joel, as one of probably few getopt.h/getopt users, what do you think of 
removing the old argument macros?

I have just added the glibc macros to getopt.h.

-- Jeff J.

More information about the Newlib mailing list